Need to stop post/PHP from executing...

TechNoir

New Member
Hi there!

I'm working on a form for our company website, and despite having reCaptcha successfully installed on it, my boss insisted he wanted one of those "2+2=4" boxes. So I resorted to a simple Javascript. The problem is, I have formmail set up... so when the wrong number is entered, an alert comes up telling the user such, but then it goes through with the POST action and sends the email anyway. I can catch it when the user leaves the field blank, due to the required field tag, but... is it possible to stop the page in the above instance?

code (edited):

<script type="text/javascript">

function addNums(){

var answer = document.getElementById("answer").value;

var digit1 = parseInt(document.getElementById("digit1").innerHTML);

var digit2 = parseInt(document.getElementById("digit2").innerHTML);

var sum = digit1 + digit2;

if(answer == ""){

alert("Please add the numbers.");

}else if(answer != sum){

alert("The numbers were added incorrectly.");

}else{

// all good now! //

document.getElementById("status").innerHTML = "Correct, it is now safe to submit the form";

document.getElementById("answer").value = "";

}





}

function randomNums(){

var rand_num1 = Math.floor(Math.random() * 10) + 1;

var rand_num2 = Math.floor(Math.random() * 10) + 1;

document.getElementById("digit1").innerHTML = rand_num1;

document.getElementById("digit2").innerHTML = rand_num2;

}

</script>

....

<body onload="randomNums();">

....

<form method="post" action=".....formmail.php" name="ContactForm" onSubmit="stamp()">
<input type="hidden" name="required" value="FullName:Your Name,email:Your E-mail Address,PhoneNumber:Your Phone
Number,number:The numbers were not added properly" />

....

<td colspan="2"><br><span>Please verify you are human:</span>&nbsp;&nbsp;
<span id="digit1"></span> + <span id="digit2"></span> =&nbsp;&nbsp;<input type="text" name="answer" id="answer"

style="background-color:#ff9966" />&nbsp;&nbsp;<span style="font-size:12px">*required</span>

<div id="status"></div></td></tr>

<tr><td width="300"></td>
<td width="75"><br>

<input value="Submit" name="submit" id="submit" type="submit" onclick="addNums();" />
If someone could help, I'd really appreciate it.
 

TechNoir

New Member
Ahhh... where do I put that code into the "all good" part? Do I replace it? List it after what's there? Do I need to remove POST? I tried several different ways but the form won't submit.

<script type="text/javascript">

function addNums(){

var answer = document.getElementById("answer").value;

var digit1 = parseInt(document.getElementById("digit1").innerHTML);

var digit2 = parseInt(document.getElementById("digit2").innerHTML);

var sum = digit1 + digit2;

if(answer == ""){

alert("Please add the numbers.");

}else if(answer != sum){

alert("The numbers were added incorrectly.");

}else{

// all good now! //


function formSubmit()
{
document.getElementById("ContactForm").submit();
}

}

function randomNums(){

var rand_num1 = Math.floor(Math.random() * 10) + 1;

var rand_num2 = Math.floor(Math.random() * 10) + 1;

document.getElementById("digit1").innerHTML = rand_num1;

document.getElementById("digit2").innerHTML = rand_num2;

}

</script>

....

<form method="post" action="....formmail.php" name="ContactForm" id="ContactForm" onSubmit="stamp()">

....

<input value="Submit" name="submit" id="submit" type="button" onclick="addNums(); formSubmit()" />
Thanks again for your help!
 

chrishirst

Well-Known Member
Staff member
type="button" onclick="addNums(); formSubmit()" />


This will run the script AND submit without waiting for the script to ok the answer, the javascript has to send the submit ONLY when the match is made correctly.
 

TechNoir

New Member
Hi... I'm sure this seems really simple to you, but it makes no sense to me. I don't know Javascript. The submit button is now a "button" type and I took formSubmit() out, but now the form will not submit at all even when the answer's correct. I assume I'm supposed to place this code somewhere that you linked me to? (http://www.w3schools.com/jsref/met_form_submit.asp) ...But I don't know where. I don't know how to make this work or I wouldn't be desperately seeking help on a forum.

Thanks...
 
Hi, Are you still in need of assistance in getting this working? I see Chris was helping you, but sounds like you are not understanding what needs to be done. You actually need to keep the formsubmit() in there so this way when you do the onclick, it will first validate the answer, and then after validating then it will submit the form. If you need further assistance, let us know.
 
Top