Jump to content

onsubmit help


thefortrees

Recommended Posts

Hi all -

 

I am having a problem with the onsubmit event handler. Onsubmit, I call the function below. The problems I am having are:

1) If the confirm dialog is used, the redirect does not occur.

2) If the alert dialog is used, even though the function returns false after the dialog, the form still submits! I have been wrestling with this for a while now.

Thanks for your help.

 

function enterInfo() {
		if (validate() == 1) { 
			if (confirm("Would you like to enter our monthly prize drawing? (OK = Yes, Cancel = No)")) {
				window.location = "enter_info.php";
				return true;
			}
			else {
				window.location = "thank_you.php";
				return true;
			}
		}

		else if (validate() == -1) { 
			alert('Please respond to all questions.'); 
			return false;
		}
	}

Link to comment
Share on other sites

How you call your enterInfo() function?

 

You should use this:

<form onsubmit="return enterInfo();">

 

So if your function return false, the form will not submit, else it does.

 

I don't think that will help. Where is the onsubmit event going to return to? There's no caller on the call stack...

 

I would do it in PHP. Make the file your JS code is in have some thing like:

 

<?php
if(one of the POST data == "") {// They didn't respond to a question...
  echo "You didn't respond to all of the questions. Click the back button in your browser and finish filling out the form.";
  exit;
} else {
  // Process the POST data from the form here, then
  echo "Would you like to enter our monthly prize drawing? ";
  echo "<a href='enter_info.php'>Yes</a> <a href='thank_you.php'>No</a>
}
?>

 

This also has the advantage of browsers without JS can still use it.

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.