Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to insert and fetch data with jquery post method in php. I need your help to correct my code friends
my code is

HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Jquery ajax</title>
<script type="text/javascript" src="JQuery/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="JQuery/jquery.min.js"></script>
<script type="text/javascript" src="JQuery/jquery.form.js"></script>
</head>
<script>

$(document).ready(function() {
$("#btn1").click(function(){

$.post("testin.php",$("#frm").serialize(),function(data){

$("#div1").html(data);
	
});
	
});

});

</script>




<body>

<div id="div1">
<form id="frm" method="post">
First Name:<input type="text" name="firstname" />
Last Name:<input type="text" name="lastname" />
Age: <input type="text" name="age" />
Home Town:<input type="text" name="hometown" />
Job: <input type="text" name="job" />
<input type="submit" value="submit" id="btn1" />
</form>
</div>
</body>
</html>


my testin.php

PHP
 <?
$con = mysqli_connect("localhost","root");
mysqli_select_db($con,"ajaxdatabase");
$first = $_POST["firstname"];
$last = $_POST["lastname"];
$age = $_POST["age"];
$home = $_POST["hometown"];
$job = $_POST["job"];
$sql1 = "INSERT INTO user(FirstName,LastName,Age,HomeTown,Job)VALUES('$first','$last','$age','$home','$job')";
$query1 = mysqli_query($con,$sql1);

if (!mysqli_query($con,$sql1))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";
$sql = "SELECT * FROM user";
$query = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Id</th>
<th>FirstName</th>
<th>LastName</th>
<th>Age</th>
<th>HomeTown</th>
<th>Job</th>
</tr>";
while ($result = mysqli_fetch_array($query))
{
	echo '<tr>';
	echo '<td>'.$result['Id'];
	echo '<td>'.$result['FirstName'];
	echo '<td>'.$result['LastName'];
	echo '<td>'.$result['Age'];
	echo '<td>'.$result['HomeTown'];
	echo '<td>'.$result['Job'];
	echo '</tr>';
		
}
echo'</table>';

?>
Posted
Updated 4-Oct-13 13:14pm
v2

1 solution

What exactly is the error report?

here is a simple PHP/MySql code to insert/display user data..


PHP
<?php
								mysql_connect("localhost", "root","") or die (mysql_error());
mysql_select_db("ajaxdatabase") or die(mysql_error());
//This code runs if the form has been submitted

 if (isset($_POST['submit'])) { 



 	$insert = "INSERT INTO user (firstname, lastname, age, hometown, job)

 			VALUES ('".$_POST['firstname']."', '".$_POST['lastname']."', '".$_POST['age']."', '".$_POST['hometown']."', '".$_POST['job']."', )";

 	$add_member = mysql_query($insert);

 	?>
SUCCESSFUL INSERT

<?php 
 } 

 else 
 {	
 ?>
<form  method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
First Name:<input type="text" name="firstname" />
Last Name:<input type="text" name="lastname" />
Age: <input type="text" name="age" />
Home Town:<input type="text" name="hometown" />
Job: <input type="text" name="job" />
<input type="submit" value="submit" name="submit"/>
</form>

 <?php
 }
 ?>


And to Display the data

PHP
<?php
		// Connect to database server
	mysql_connect("localhost", "user", "") or die (mysql_error ());

	// Select database
	mysql_select_db("ajaxdatabase") or die(mysql_error());

	// SQL query
	$strSQL = "SELECT * FROM users ORDER BY Id DESC";

	// Execute the query (the recordset $rs contains the result)
	$rs = mysql_query($strSQL);
	
	// Loop the recordset $rs
	// Each row will be made into an array ($row) using mysql_fetch_array
	while($row = mysql_fetch_array($rs)) {

	   // Write the value of the column FirstName (which is now in the array $row)
	  echo $row['Id'] . " " . $row['firstame'] . " " . $row['lastname'] . " " . $row['age'] . " " . $row['hometown'] . " " . $row['job'] . " " . $row['BirthDate'] . "<br />";

	  }

	// Close the database connection
	mysql_close();
	?>
 
Share this answer
 
v6
Comments
Md Jamaluddin Saiyed 5-Oct-13 2:29am    
Thanks for your answer but I have posted data with php post method successfully I want to do it with jquery ajax $.post method !!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900