Jump to content

[SOLVED] Stop Javascript Form Submit


trampolinejoe

Recommended Posts

Hello People,

 

I am starting with some basic form validation with js. How do I stop this posting to the next page if the error occurs?

 

No matter what I do even if the error occurs it seems to post to the next page.

 

 

My JS Function:

  
function empty(){
  
  if ( document.form1.products.value == "empty" )
    {
        alert ( "You have not selected a product, please select a product." );
        valid = false;

    }

  }

 

Drop Down Menu:

  <select name="productArray" id="productArray" onChange="change()">
           <option value="empty">--Please Select--</option>
           <option value="8fttrampoline2009">8ft Trampoline <?php echo'$'."$eightfttrampoline".'.00'; ?></option>
           <option value="10fttrampoline2009">10ft Trampoline <?php echo'$'."$tenfttrampoline".'.00'; ?></option>
        </select> 

 

 

Submit Button:

  <input name="next" type="submit" onClick="return empty()" value="Next >>">

Link to comment
Share on other sites

Works Fine For me:

 

<script type = "text/javascript" language = "Javascript">
function empty(){

  if ( document.form1.productArray.value == "empty" )
    {
        alert("You have not selected a product, please select a product.");
        valid = false;

    }

  }
</script>


<form name="form1">

<select name="productArray" id="productArray">
           <option value="empty">--Please Select--</option>
           <option value="8fttrampoline2009">8ft Trampoline </option>
           <option value="10fttrampoline2009">10ft Trampoline </option>
        </select>
<input name="next" type="submit" onClick="return empty()" value="Next >>">
</form>

Link to comment
Share on other sites

Hello,

 

Thanks for the fast response!

 

I should have posted it as a single thing from the 1st post. I have included the html I used got my form, other then that it's pretty much the same.

 

Cheers.

 

<script type = "text/javascript" language = "Javascript">
function empty(){

  if ( document.form1.productArray.value == "empty" )
    {
        alert("You have not selected a product, please select a product.");
        valid = false;

    }

  }
</script>


<form name="form1" method="post"  action="onePost.php" >

<select name="productArray" id="productArray">
           <option value="empty">--Please Select--</option>
           <option value="8fttrampoline2009">8ft Trampoline </option>
           <option value="10fttrampoline2009">10ft Trampoline </option>
        </select>
<input name="next" type="submit" onClick="return empty()" value="Next >>">
</form>

Link to comment
Share on other sites

Well,

 

onePost.php is a blank page atm, I havn't started coding it because I am not up to that yet. I was under the impression that the form validation could be done on the same page with js and that the contents of the page which posts is not important at this stage because we shouldn't even be posting to that page under the condition that the 'empty' option is selected we should get the alert and then stay on the same page.

 

Please correct me if I am wrong.

 

Cheers,

Joe.

Link to comment
Share on other sites

I am sure you could post to the same page, or you could even point to a page that you do not have either doesn't make a difference to the fact that it is still submitting the page.

 

However, I have noticed why it is doing this, I forgot to make the function 'return false' and that was the cause of the issue.

 

I thank you for your time anyways.

 

Cheers,

Joe.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.