Total members 11718 |It is currently Thu Jun 21, 2018 5:56 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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;





Author:
Mastermind
User avatar Posts: 2707
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  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     -  
 Reference Parameters     -  
 Encoding parameters in URL     -  
 Method Parameters,,,is it by value or reference???     -  
 Function with default parameters     -  
 Send parameters using HTTP GET     -  
 Project Post Example     -  
 Here how to add ads of google after first post     -  



Topic Tags

JQuery GET/POST






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com