0

this code is suppose to accept user value and then give a corresponding alert box but it's not working. May I know what is wrong with this code? I did a lot of google search (many point to this website) but I can't seems to find the solution? Please help and thanks in advance.


index.html

<html>
<head>
<title>the title</title>
   <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.0.min.js"></script>

<script type="text/javascript">

 function check() {
      var    one = $('#first').val();
       var   two = $('#second').val();
       var  three = $('#third').val();
       var  four = $('#fourth').val();  

    $.post("result.php", { 
    first: one,
    second: two,
    third: three,
    fourth: four
},function(data)
        {

         if (data=="yay") //for no input
         { alert("yay");
         }
         else 
         {
         alert("nay");
         }

         }


        } 

</script>

</head>
<body>

        <form name="form">
    <input type="text" id="first">
    <input type="text" id="second">
     <input type="text" id="third">
    <input type="text" id="fourth">
    <input type="button" value="get" onclick="check();">
    </form>


</body>
</html>

result.php

<?php

    $one = $_POST["first"];
        $two = $_POST["second"];
         $three =$_POST["third"];
         $four = $_POST["fourth"];
         if($one > 5)        
         {

         echo "yay";
         }
     elseif($two > 10 )        {

         echo "yay";  }

             elseif($three > 15 )        
             {

         echo "yay";  
             }

             elseif($four > 20 )        
             {

         echo "yay";  
             }

           else{
               echo "nay";
           }

?>
3
  • 2
    I would add dataType json and echo a json_encode array with the result Commented May 27, 2013 at 17:29
  • use .serialize() for the form man Commented May 27, 2013 at 17:30
  • Doesn't it output nothing at all? Before making if statements in the result of the AJAX, do an alert on the data: alert(data); to check its output. Commented May 27, 2013 at 17:30

1 Answer 1

1

The json dataType

    ///<script ....
     $.post("result.php", { 
    first: one,
    second: two,
    third: three,
    fourth: four
},function(data){
    if (data.response=="yay") { alert("yay"); }
    else { alert("nay"); }
},"json");

//</script>

<?php

    $one = $_POST["first"];
    $two = $_POST["second"];
    $three =$_POST["third"];
    $four = $_POST["fourth"];

   if($one > 5)        
         {

         $response = "yay";
         }
     elseif($two > 10 )        {

         $response = "yay";  }

             elseif($three > 15 )        
             {

         $response = "yay";  
             }

             elseif($four > 20 )        
             {

         $response = "yay";  
             }

           else{
               $response = "nay";
           }

           echo json_encode(array("response" => $response));

?>
Sign up to request clarification or add additional context in comments.

2 Comments

I add $lol = array('response' => $response); echo json_encode($lol); and it works. I appreciate your help. Anyway I confuse why some tutorial I see they didn't include json. Is json the same with serialize? Only use it if the variable you post is more than one? I appreciate your help again.
Serialize will work in exactly the same way as this, it just saves you some typing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.