在php中使用jquery post方法插入和获取数据 [英] insert and fetch data with jquery post method in php

查看:68
本文介绍了在php中使用jquery post方法插入和获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在php中使用jquery post方法插入和获取数据。我需要你的帮助来纠正我的代码朋友

我的代码是



 <  !DOCTYPE     html     PUBLIC     -  // W3C // DTD     XHTML  < span class =code-attribute>   1.0     Transitional // EN   http://www.w3.org/TR/xhtml1/ DTD / xhtml1-transitional.dtd >  
< html xmlns = http://www.w3.org/1999/xhtml < span class =code-keyword>>
< head >
< meta http-equiv = 内容类型 内容 = text / html; charset = utf-8 / > ;
< title > Jquery ajax < / title >
< script type = text / javascript src = < span class =code-keyword> JQuery / jquery-1.4.2.min.js > < / script >
< script 类型 = text / javascript src = JQuery / jquery.min.js > < / script >
< script < span class =code-attribute> type = text / javascript src = JQuery / jquery.form.js > < / script >
< / head >
< script >

$( document )。ready( function (){
$( #btn1)。click(函数(){

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

$( #div1)。html(data);

});

});

});

< / script >




< body >

< div id = div1 >
< 表格 id = frm < span class =code-attribute> method = post >
名字:< 输入 type = text name = 名字 / >
姓氏:< 输入 type = text name = 姓氏 / >
年龄:< 输入 type = text 名称 = age / >
家乡: < 输入 type = text name = hometown / >
工作:< 输入 类型 = text 名称 = job / >
< 输入 类型 = 提交 value = submit <跨度lass =code-attribute> id = btn1 / >
< / form >
< / div >
< / body >
< / html >





my testin.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(名字,姓氏,年龄,籍贯,工作)VALUES( '$第一', '$最后', '$岁', '$ HOME', '$工作'),;
$ query1 = mysqli_query($ con,$ sql1);

if (!mysqli_query($ con,$ sql1))
{
die(' 错误:' .mysqli_error($ con));
}
echo 1记录添加;
$ sql = SELECT * FROM user ;
$ query = mysqli_query($ con,$ sql);
echo < table border =' 1'>
< tr>
< th> Id< / th>
< th> FirstName< / th>
< th> LastName< / th> ;
< th>年龄< / th>
< th> HomeTown< / th>
< th>工作< / 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 [' 年龄'];
echo ' < td>'.$result [' HomeTown'];
echo ' < td>'.$result [' Job'];
echo ' < / tr>' ;

}
echo ' < /表>';

?>

解决方案

document )。ready( function (){


(< span class =code-string> #btn1)。click( function (){


.post( testin.php

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

<!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

 <?
$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>';

?>

解决方案

(document).ready(function() {


("#btn1").click(function(){


.post("testin.php",


这篇关于在php中使用jquery post方法插入和获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆