Switch to full style
JQuery Examples
Post a reply

Post parameters using JQuery

Tue Jan 01, 2013 5:00 pm

Post parameters (HTTP Request) , insert to database and return database content with status:
Code:

<html>
<
head>
<
title>Post parameters using JQuery</title>

<!--
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
 Note you can download the JQuery package instead of using Google version. --> 
<script src="jquery-1.8.3.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.post("JQuery.php",
    {
      name:"Dom adnos",
      age:"43"
    },
    function(data,status){
  
       $("div#div1").html("Data: " + data + "\nStatus: " + status);
    });
  });
});
</script>
</head>
<body>

<button>Send  POST and return status with DB content</button>
<div id="div1"></div>
</body>
</html>


JQuery.php
Code:

<?php

   if
(isset($_POST['name'])){
   $name=$_POST['name'];
   }
   
    if
(isset($_POST['age'])){
   $age=$_POST['age'];
   }
    
    $servername
="localhost";
    $dbuser="root";
    $dbpassword="";
    $dbname="JQuery";
    $table="user";
    
    $link
=mysql_connect ("$servername","$dbuser","$dbpassword");
    if(!$link){die("Could not connect to MySQL");}
    mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
 
       $query 
="insert into   $table values('$name', $age ) ";
       $result=mysql_query($query) ;
       echo mysql_error();
       echo "<br> Affected rows :".mysql_affected_rows();
      //$date = date("Y-m-d H:i:s"); 
       $query ="select * from   $table ";
       $result=mysql_query($query) ;

?>
  <table border="1" >
  <tr>
  <th>Name</th>
  <th>Age</th>
  </tr>
<?php       
 while
($row = mysql_fetch_array($result))
  {
       echo "<tr>"; 
       echo 
"<td>".$row['name']."</td>" ;
       echo "<td>".$row['age']."</td>" ;
       echo "</tr>";
   }
?>
   </table>


MYSQL database :
Code:
--
-- Table structure for table `user`
--

CREATE TABLE IF NOT EXISTS `user` (
  `name` varchar(50) NOT NULL DEFAULT '',
  `age` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;




Post a reply
  Related Posts  to : Post parameters using JQuery
 Extracting Parameters from GET or POST Request     -  
 get url, title and url sent parameters with JQuery     -  
 Get values of Form parameters using JQuery     -  
 Project Post Example     -  
 Here how to add ads of google after first post     -  
 Reference Parameters     -  
 Encoding parameters in URL     -  
 Important : Read before you post .     -  
 how to post a finished project     -  
 check request was GET or POST     -  

Topic Tags

JQuery GET/POST