I am using JavaScript to validate my form. It's basic validation. The step I am having problems with it sending the synum variable via post from java script to my php page valid_synum.php where I do a pdo look up to check if that synum exists or not then I echo a 1 or a 0 based on the result. I want the java script to take that result and either send an alert error or if ok allow the form to submit. Here is the javascript I have. As far as I can tell the POST call
$.post("valid_synum.php", { synum: synum },
is never called up. I placed a test in the php page and the test never gets activated from the script. It works fine if I load the php directly in a browser.
Any help would be greatly appreciated.
<script>
function isNumber(input)
{
return (input - 0) == input && (input+'').replace(/^\s+|\s+$/g, "").length > 0;
}
function checkForm(f)
{
if (f.elements['worknum'].value != "")
{
if ((isNumber(f.elements['worknum'].value)) == false)
{
alert("The Work Order Number is not valid")
return false;
}
var chars = 6;
if((f.elements['worknum'].value).length != 6)
{
alert("The Work Order Number is not 6 digits")
return false;
}
}
if (f.elements['synum'].value == "")
{
alert("Please Enter the Store Number");
return false;
}
if (f.elements['worknum'].value == "")
{
alert("Please Enter the Work Order Number");
return false;
}
if (f.elements['synum'].value != "")
{
//get the username
var synum = (f.elements['synum'].value);
$.post("valid_synum.php", { synum: synum },
function(result){
//if the result is 1
if(result == 1){
//do nothing valid synum
alert("Good")
return false;
}else{
//show that the username is NOT available
alert("The store number is NOT valid. Please Enter full store number")
return false;
}
});
}
else
{
f.submit();
return false;
}
}
</script>
Here is my php page code:
$id=$_POST['synum'];
try{
$conn = new PDO( "sqlsrv:Server= $eautoserver ; Database = $eautodb ", $eautouser, $eautopwd);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$stmt = $conn->prepare('SELECT * FROM vw_DCR_Web_Customer_Query WHERE CustomerNumber= :id');
$stmt->execute(array('id' => $id));
$row = $stmt->fetch();
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
if ($row){
echo 1;
$email_body="1";
}else{
echo 0;
$email_body="0";
}
Maybe this blog post will help you: http://blog.secull.org/php-functions-in-javascript/ There is an similar problem and it is shown, how one can solve it.
mate
I should check if open/close square brackets are OK to know why your code is not entering in the desired condition.
By the way, you can test if a string is a number and validate if it's empty in a cleaner way.
function isNumber(number){
return !isNaN(+number) && number !== ""
}
I changed how I setup the code and removed
$(document).ready(function(){
$("#synum").change(function() {
$.post("valid_synum.php", { synum: synum },
function(result){
//if the result is 1
if(result == 1){
//do nothing valid synum
alert("Good")
return false;
}else{
//show that the username is NOT available
alert("The store number is NOT valid. Please Enter full store number")
return false;
}
});
from within my checkform function once it was a separate function it started to work on the fly correctly.