sg552
sg552

Reputation: 1543

Post multiple variable with jQuery

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";
           }

?>

Upvotes: 0

Views: 109

Answers (1)

Insyte
Insyte

Reputation: 2236

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));

?>

Upvotes: 1

Related Questions