我有一个从Ajax接收数据的PHP文件但是在收到数据后我使用json_encode
从php发送成功消息,但是即使SQL查询成功,也会收到消息error is hit
。这个过程适用于localhost,但是当我将它上传到主服务器时出现错误。
这是我的JS文件: $(function(){ $(“#userReg_btn”)。click(function(e){ //用户注册
var array = [];
var flag = false;
var firstName = $("#uFn").val();
var lastName = $("#uLn").val();
var email = $("#uEa").val();
var pass = $("#uPn").val();
var mobile = $("#uMn").val();
var nID = $("#uNm").val();
var age = $("#uAn").val();
var prof = $("#uPc").val();
if (firstName == "" || lastName == "" || email == "" || pass == "" || mobile == "" || nID == "" || age == "" || prof == "") {
e.preventDefault();
alert("Please provide some input");
flag = false;
} else if (mobile.length != 11 || nID.length != 17) {
e.preventDefault();
alert("Please provide correct input");
flag = false;
} else {
array.push(firstName);
array.push(lastName);
array.push(email);
array.push(pass);
array.push(mobile);
array.push(nID);
array.push(age);
array.push(prof);
alert(array);
console.log(array);
flag = true;
}
if (flag == true) {
$.ajax({
url: "http://demoname.co/CustomerRegistration.php",
data: {
firstName: array[0],
lastName: array[1],
email: array[2],
pass: array[3],
mobile: array[4],
nID: array[5],
age: array[6],
prof: array[7]
},
type: "POST",
dataType: "json",
success: function(suc) {
alert("in success");
alert(suc);
console.log("success");
},
error: function(err) {
alert("error is hit");
alert(err);
console.log(err);
}
});
} else {
alert("Form error");
}
alert("USer Reg");
});
这是我的PHP文件:
<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 01 Jan 2016 00:00:00 GMT');
// The JSON standard MIME header.
header('Content-type: application/json');
$con=mysql_connect("localhost","username", "password");
mysql_select_db("database name",$con);
$fname = $_POST['firstName'];
$lname=$_POST['lastName'];
$username=$_POST['email'];
$password=$_POST['pass'];
$phone=$_POST['mobile'];
$nid=$_POST['nID'];
$age=$_POST['age'];
$profession=$_POST['prof'];
$query="INSERT INTO customerregistration(FirstName,LastName,Username,password,PhoneNumber,NID,Age,Profession) VALUES('$fname','$lname','$username','$password','$phone','$nid','$age','$profession');";
if(mysql_query($query))
{
$suc= 'success';
echo json_encode($suc);
}
else
{
echo mysql_error();
}
?>