JQuery php post values ​​and get result corresponding to result

Here is my Jquery and Php file where I will post the values ​​in the php file via jquery,

In simple:

    <script type="text/javascript" src="../jquery.min.js"></script>
   <script type="text/javascript" language="javascript">
  $(document).ready(function() {
      $("#driver").click(function(event){
       var name=$("#txt").val();   
       $.post("test_post.php",
             {name: name}).done(function(data){
              $('#stage').html(data);
               }

          );
      });
   });
   </script>
</head>
<body>
<input type="text" id="txt" value="Load Data" />
<input type="button" id="driver" value="Load Data" />
</body>

      

Here is the .php file. If the insert was successful, I should receive a warning as a success, otherwise I should receive a warning as a failure.

    <?php

 include ('conn.php');
 if( $_REQUEST["name"] )
{
$q = "INSERT INTO text (name) VALUES ($name)";
if(mysql_query($q))
{
echo 'success'.$q;
}
else
{
echo 'fail'.$q;
}
}
?>

      

And the echo should show up on the homepage.

How can i do this?

+3


source to share


1 answer


if( $_REQUEST["name"] )
{
$q = "INSERT INTO text (name) VALUES ($name)";

      

You are not assigning a variable here $name

. So the request will definitely fail.



Change it to:

if( $_REQUEST["name"] )
{
  $name=$_REQUEST['name'];
  $q = "INSERT INTO text (name) VALUES ($name)";
  -------------------------
}

      

0


source







All Articles