我无法通过ajax插入数据,我想插入它并获得JSON响应。实际上最后我想创建这个总代码的API。
<?php
error_reporting(1);
mysql_connect("localhost","root","");
mysql_select_db("behindtherock");
?>
<!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>Untitled Document</title>
</head>
<body>
<form >
<label>Country</label><br />
<input type="text" id="country" />
<br />
<label>First name</label><br />
<input type="text" id="first_name" />
<br />
<label>Last Name</label><br />
<input type="text" id="last_name" />
<br />
<input type="submit" id="save" value="post" />
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#save").click(function(){
var country=$("#country").val();
var first_name=$("#first_name").val();
var last_name=$("#last_name").val();
var address_line_1=$("#address_line_1").val();
var address_line_2=$("#address_line_2").val();
var city=$("#city").val();
var state=$("#state").val();
var zip=$("#zip").val();
var user_phone=$("#user_phone").val();
var user_email=$("#user_email").val();
var u_stream=$("#u_stream").val();
var referring_member=$("#referring_member").val();
var promotion_code=$("#promotion_code").val();
$.ajax({
url:"add.php",
type:"post",
async:false,
data:{
"done":1,
"country":country,
"first_name":first_name,
"last_name":last_name,
"address_line_1":address_line_1,
"address_line_2":address_line_2,
"city":city,
"state":state,
"zip":zip,
"user_phone":user_phone,
"user_email":user_email,
"u_stream":u_stream,
"referring_member":referring_member,
"promotion_code":promotion_code,
};
success:function(data){
}
});
});
});
</script>
</body>
</html>
<?php
error_reporting(1);
include("db.php");
if(isser($_POST['done'])){
$country=mysql_escape_string($_POST['country']);
$first_name=mysql_escape_string($_POST['first_name']);
$last_name=mysql_escape_string($_POST['last_name']);
$address_line_1=mysql_escape_string($_POST['address_line_1']);
$address_line_2=(int)$_POST['address_line_2'];
$city=mysql_escape_string($_POST['city']);
$state=mysql_escape_string($_POST['state']);
$zip=mysql_escape_string($_POST['zip']);
$user_phone=mysql_escape_string($_POST['user_phone']);
$user_email=mysql_escape_string($_POST['user_email']);
$u_stream=mysql_escape_string($_POST['u_stream']);
$referring_member=mysql_escape_string($_POST['referring_member']);
$promotion_code=mysql_escape_string($_POST['promotion_code']);
mysql_query("INSERT INTO user_details(country,first_name,last_name,address_line_1,address_line_2,city,state,zip,user_phone,user_email,u_stream,referring_member,promotion_code)
VALUES ('('', '$country','$first_name','$last_name','$address_line_1','$address_line_2','$city','$state','$zip','$user_phone','$user_email','$u_stream','$referring_member','$promotion_code')')");
exit();
}
?>
我无法通过ajax插入数据,我想插入它并获得JSON响应。实际上最后我想创建这个总代码的API。