Javascript 未返回php ajax jquery responseText
我正在尝试用jquery、ajax和php编写一个插入查询。正在插入记录,但返回状态错误。首先,我尝试在php中Javascript 未返回php ajax jquery responseText,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我正在尝试用jquery、ajax和php编写一个插入查询。正在插入记录,但返回状态错误。首先,我尝试在php中echo该消息,因为它不起作用,我尝试使用print json_encode进行响应,但都返回了错误状态。为什么它不返回responseText {readyState:0,responseText:,状态:0,状态文本:“错误”} 这是addmember.php文件 <?php require '../database.php'; function rand
echo
该消息,因为它不起作用,我尝试使用print json_encode
进行响应,但都返回了错误状态。为什么它不返回responseText
{readyState:0,responseText:,状态:0,状态文本:“错误”}
这是addmember.php文件
<?php
require '../database.php';
function random_password( $length = 8 ) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
$password = substr( str_shuffle( $chars ), 0, $length );
return $password;
}
$password = random_password(8);
//$regno=$_POST['regNo'];
$adminno=$_POST['adminNo'];
$batch=$_POST['batchText'];
$type=$_POST["memberType"];
$initials=$_POST["initialName"];
$fullname=$_POST["fullName"];
$address=$_POST["address"];
$telephone=$_POST["contact"];
$email=$_POST["email"];
$nic=$_POST["nic"];
$dob=$_POST["birthDate"];
$priv=$_POST["memberType"];
$userid="";
$sql="select username from memberinfo where username='$adminno'";
$result=mysqli_query($con,$sql);
if(mysqli_num_rows($result)==0){
$sql="insert into memberinfo(username,nic_no,class,name_initial,full_name,address,telephone,email,date_of_birth) VALUES ('$adminno','$nic','$batch','$initials', '$fullname', '$address', '$telephone','$email','$dob')";
$result1=mysqli_query($con,$sql);
$sql = "select * from memberinfo where username='$adminno'";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$userid = $row['user_id'];
}
}
$sql="insert into userlogin(user_id,username,privilege,password) VALUES ('$userid','$adminno','$priv','$password')";
$result2=mysqli_query($con,$sql);
if ($result1 && $result2) {
$message = "<p>New record created successfully</p>";
} else {
$message = "<p>Error: " . $sql . "<br>" . $con->error.".</p>";
}
} else{
$message = "<p>Admission no already exists.</p>";
}
print json_encode($message);
$con->close()
?>
您没有从服务器返回有效的JSON。您正在对字符串进行json编码,但有效的json需要一个对象或数组来封装返回的日期
因此,至少:
echo json_encode(array($message));
状态为零通常意味着页面正在离开。阻止它发生
$('#addmember').click(function(evt){ //<--add the evt
evt.preventDefault(); //cancel the click
$('#addmember')。单击(函数(evt){/不需要JSON响应。只需从PHP脚本返回消息,如下所示(注意echo
的使用以及close()
后面的分号):
PHP
另外,从AJAX调用中删除JSON文件类型,而是附加response.responseText
而不是response
:
JS
@AsmazinnerAjabir您可以打开web检查器,转到“网络”选项卡,检查网络请求以查看是否可以获得更多信息。一切似乎正常。猜测它不会显示,因为成功消息不是来自服务器。是否有方法显示自定义消息?完成,感谢您指出。修复了一个错误此外,ajax中的nge。必须附加response.responseText而不是response
$('#addmember').click(function(evt){ //<--add the evt
evt.preventDefault(); //cancel the click
$con->close();
echo $message;
$.ajax({
type:"POST",
url:"../ajax/addmember.php",
data:{'adminNo':adminno,'nic':nic,'batchText':batch,'initialName':initials, 'fullName':fullname, 'address':address, 'contact':telephone,'email':email,'birthDate':dob,'memberType':priv},
success:function(response){
console.log(response);
$("#result").append(response.responseText);
},
error:function(response){
console.log(response);
}
});