Use the 'onsubmit' handler for the form:
<form action="..." method="post" onsubmit="return doStuff()">
The "return doStuff" is important: if your method returns "false", the form is not submitted. Hence, when you prompt the user for input, if they answer in such a way that means the form is not to be posted, you simply return false and the form is not submitted.
If the question you are asking the user is a simple OK/Cancel question, use a "confirm" dialog, which returns either true or false (for OK and Cancel respectively). Otherwise use, "prompt".
Before the form is submitted, set the data that you want passed to your next page (for the PHP to process) to the value of a hidden input element within your form. The following examples illustrate this:
First, using a 'confirm' dialog:
<html>
<head>
<script language="JavaScript">
function doStuff() {
var data = "this is the data you want passed to the processing page";
var answer = confirm("A Question?");
if(answer) {
document.getElementById("user_answer").value = data;
return true;
}
else
return false;
}
</script>
</head>
<body>
<form action="http://www.google.com" method="post" onsubmit="return doStuff()">
<input type="hidden" id="user_answer" value="" />
<input type="submit" value="Click me" />
</form>
</body>
</html>
- <html>
- <head>
- <script language="JavaScript">
- function doStuff() {
- var data = "this is the data you want passed to the processing page";
- var answer = confirm("A Question?");
- if(answer) {
- document.getElementById("user_answer").value = data;
- return true;
- }
- else
- return false;
- }
- </script>
- </head>
- <body>
- <form action="http://www.google.com" method="post" onsubmit="return doStuff()">
- <input type="hidden" id="user_answer" value="" />
- <input type="submit" value="Click me" />
- </form>
- </body>
- </html>
Or using a 'prompt' dialog:
<html>
<head>
<script language="JavaScript">
function doStuff() {
var data = "this is the data you want passed to the processing page";
var answer = prompt("A Question?");
if(answer || answer == "") { // answer = false if "cancel" pressed. return false if no input entered, i.e. ""
document.getElementById("user_answer").value = data;
return true;
}
else
return false;
}
</script>
</head>
<body>
<form action="http://www.google.com" method="post" onsubmit="return doStuff()">
<input type="hidden" id="user_answer" value="" />
<input type="submit" value="Click me" />
</form>
</body>
</html>
- <html>
- <head>
- <script language="JavaScript">
- function doStuff() {
- var data = "this is the data you want passed to the processing page";
- var answer = prompt("A Question?");
- if(answer || answer == "") { // answer = false if "cancel" pressed. return false if no input entered, i.e. ""
- document.getElementById("user_answer").value = data;
- return true;
- }
- else
- return false;
- }
- </script>
- </head>
- <body>
- <form action="http://www.google.com" method="post" onsubmit="return doStuff()">
- <input type="hidden" id="user_answer" value="" />
- <input type="submit" value="Click me" />
- </form>
- </body>
- </html>
Hope this helps
Why do geeks get Halloween and Christmas confused?
Because 31 Oct == 25 Dec
www.darren-king.co.uk