Total members 11889 |It is currently Thu Mar 28, 2024 10:50 pm 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;




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
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     -  
 Project Post Example     -  
 Here how to add ads of google after first post     -  
 Reference Parameters     -  
 Encoding parameters in URL     -  
 Get value from HTTP POST VARS     -  
 Ad after first topic in a post in Skymiles_red     -  
 Function with default parameters     -  



Topic Tags

JQuery GET/POST
cron





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