0

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";
}
5
  • If you put an alert before the .post, does it display the alert? Meaning, does it even get to the post code? Commented Apr 4, 2013 at 18:39
  • Yes if I do alert(synum); just before the $.post it does show me what was entered. All the other error checking that the java script does works fine. It's just this one piece that doesn't seem to activate. I placed code on the php to send a email if it is accessed just to test if the page was being accessed and it is not. Commented Apr 4, 2013 at 18:53
  • Do you have any errors in the console? Commented Apr 4, 2013 at 18:58
  • No the page it's self and the program I use for code editing are not showing any error. Commented Apr 4, 2013 at 19:07
  • try changing the {synam : synam} to {synam : synamVal} and var synumVal = (f.elements['synum'].value); . might be the problem, might not Commented Apr 5, 2013 at 13:53

2 Answers 2

1

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 !== ""
}
Sign up to request clarification or add additional context in comments.

3 Comments

This way you don't need to create a Regex object each time you call the function...
Thanks I will adjust that.
with your statement " open/close square brackets " you mean to make sure that my var synum = (f.elements['synum'].value); actually has the data I am expecting? I have I placed an alert before the post to test the data to confirm it is what was entered in.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.