[英]php ajax jquery responseText not returned
I am trying to write an insert query with jquery, ajax and php.我正在尝试使用 jquery、ajax 和 php 编写插入查询。 The record is getting inserted but returns a status error.正在插入记录但返回状态错误。 First I tried to echo
the message in php as it didn't work I tried it with print json_encode
but both returned the status as error.首先,我尝试在 php 中echo
显消息,因为它不起作用我尝试使用print json_encode
但都将状态返回为错误。 Why doesn't it return the responseText?为什么它不返回 responseText?
{readyState: 0, responseText: "", status: 0, statusText: "error"} {readyState: 0, responseText: "", status: 0, statusText: "error"}
This is the addmember.php file这是 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()
?>
This is the .js file with the ajax function这是带有 ajax 函数的 .js 文件
$(document).ready(function(){
$('#addmember').click(function(){
console.log("addmember");
var adminno=$("#adminNo").val();
var nic=$("#nic").val();
var batch=$("#batchText").val();
var initials=$("#initialName").val();
var fullname=$("#fullName").val();
var address=$("#address").val();
var telephone=$("#contact").val();
var email=$("#email").val();
var dob=$("#birthDate").val();
var priv=$("#memberType").val();
//$("#result").html("<img alt='ajax search' src='ajax-loader.gif'/>");
$.ajax({
type:"POST",
url:"../ajax/addmember.php",
dataType: "json",
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);
},
error:function(response){
console.log(response);
}
});
});
});
Status zero normally means the page is navigating away.状态零通常意味着页面正在导航。 Stop it from happening.阻止它发生。
$('#addmember').click(function(evt){ //<--add the evt
evt.preventDefault(); //cancel the click
You are not returning valid JSON from the server.您没有从服务器返回有效的 JSON。 You're json encoding a string, but valid JSON requires an object, or array to encapsulate the day coming back.您正在对字符串进行 json 编码,但有效的 JSON 需要一个对象或数组来封装回来的那一天。
So at the very least:所以至少:
echo json_encode(array($message));
No need for the JSON response.不需要 JSON 响应。 Simply return the message from your PHP script as shown below (note the use of echo
and the semicolon following close()
):简单地从您的 PHP 脚本返回消息,如下所示(注意使用echo
和close()
后面的分号):
$con->close();
echo $message;
Also, remove the JSON filetype from your AJAX call and instead append response.responseText
rather than response
:此外,从您的 AJAX 调用中删除 JSON 文件类型,并附加response.responseText
而不是response
:
$.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);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.