Web Analytics Made Easy -
StatCounter stop form from submitting - CodingForum

Announcement

Collapse
No announcement yet.

stop form from submitting

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • stop form from submitting

    I have a form with various text inputs
    and a link like this

    a href=\"javascript:submitform();\" to submit

    the function submitform() is like this
    function submitform(){

    if (document.shop.tot_cd1.selectedIndex==0&&document.shop.tot_cd2.selectedIndex==0&&document.shop.tot_d vd.selectedIndex==0) {

    alert(\"Please select an item..!\")

    }
    if(document.shop.location.selectedIndex==0){
    alert(\"Please select a location..!\")

    }
    if((document.shop.tot_cd1.selectedIndex+document.shop.tot_cd2.selectedIndex+document.shop.tot_dvd.se lectedIndex)>8){
    alert(\"For individual orders over 8 items contact sales..!\")

    }
    else{
    document.shop.action=\"index.php?state=summary\";
    document.shop.submit();
    }

    when i hot the link to submit..the formsubmit function seems to be ignored..
    and the form is submitted..
    any help?

  • #2
    Code:
    <form name=".." action=".." method=".." onsubmit="submitForm()">
    Code:
    function submitForm(){
    
    if (document.shop.tot_cd1.selectedIndex==0&&document.shop.tot_cd2.selectedIndex==0&&document.shop.tot_dvd.selectedIndex==0) {
    
    alert(\"Please select an item..!\")
    [COLOR=DarkSlateBlue]return false;[/COLOR]
    }
    if(document.shop.location.selectedIndex==0){
    alert(\"Please select a location..!\")
    [COLOR=DarkSlateBlue]return false;[/COLOR]
    }
    if((document.shop.tot_cd1.selectedIndex+document.shop.tot_cd2.selectedIndex+document.shop.tot_dvd.se lectedIndex)>8){
    alert(\"For individual orders over 8 items contact sales..!\")
    [COLOR=DarkSlateBlue]return false;[/COLOR]
    }
    
    document.shop.action=\"index.php?state=summary\";
    return true;
    }
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

    Comment


    • #3
      Thanks for your reply..
      I tried your suggestion.
      However if all fields are empty I get only the first alert..
      and the a blank screen with the word false..
      I think it has to do with my link

      a href="javascript:submitform();"
      clues??

      Comment


      • #4
        this will take care of showing all the alerts
        Code:
        function submitForm(){
        var retVal = true;
        if (document.shop.tot_cd1.selectedIndex==0&&document.shop.tot_cd2.selectedIndex==0&&document.shop.tot_dvd.selectedIndex==0) {
        
        alert(\"Please select an item..!\")
        retVal = false;
        }
        if(document.shop.location.selectedIndex==0){
        alert(\"Please select a location..!\")
        retVal = false;
        }
        if((document.shop.tot_cd1.selectedIndex+document.shop.tot_cd2.selectedIndex+document.shop.tot_dvd.se   lectedIndex)>8){
        alert(\"For individual orders over 8 items contact sales..!\")
        retVal = false;
        }
        if ( retVal ) {
        document.shop.action=\"index.php?state=summary\";
        }
        return retVal;
        }

        as for the link its gotta be like this
        Code:
        <a href="javascript:document.shop.submit()"..
        or
        Code:
        <a href="#" onclick="document.shop.submit()"..
        You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

        Comment


        • #5
          thanks again for your efforst..
          however now I get a javascript syntax error..

          Comment


          • #6
            whay not simply use a return condition attached to the submit action?:

            function submitForm(){
            if(...condition is fulfiled..){return true}
            else{return false}
            }

            ...

            <form onsubmit="return submitForm()"......>
            KOR
            Offshore programming
            -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

            Comment


            • #7
              oops! thats right...
              Originally posted by Kor
              whay not simply use a return condition attached to the submit action?:

              function submitForm(){
              if(...condition is fulfiled..){return true}
              else{return false}
              }

              ...

              <form onsubmit="return submitForm()"......>
              You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

              Comment


              • #8
                or, to shorten the code (as return true is the default):

                function submitForm(){
                if(...condition is not fulfiled..){return false}
                }
                KOR
                Offshore programming
                -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

                Comment


                • #9
                  I think the problem lies with the link..
                  have tried this
                  <a href=\"javascript: document.shop.submit()>add to cart</a>

                  and having this in the form tag
                  <form name=\"shop\" action=\"\" method=\"post\" onsubmit=\"return submitForm()\">
                  but this just submits the form without any validation

                  Comment


                  • #10
                    than try this

                    <a href="#" onclick="submitForm();return false">add to cart</a>

                    ...

                    function submitForm(){
                    if(...condition is fulfiled..){
                    document.forms['shop'].submit();
                    }
                    }
                    KOR
                    Offshore programming
                    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

                    Comment

                    Working...
                    X