我正在使用ajax
与PHP
进行通信。当获取的数据显示数据库中存在已键入的电子邮件时,我使用了echo
的消息'已经收到电子邮件,尝试另一封电子邮件'。但是ajax responseText
正在返回header file and echoed content
。这是一张图片
如何将回显的消息作为ajax responseText?
这是我的代码
的Javascript
function signup(){
var f=document.getElementById("firstname").value;
var l=document.getElementById("lastname").value;
var p=document.getElementById("password1").value;
var p2=document.getElementById("password2").value;
var e=document.getElementById("email").value;
var status=document.getElementById("status");
if(f=="" || l=="" || p=="" || p2=="" || e==""){
status.innerHTML="All fields are required";
}
else if(p!=p2){
status.innerHTML="passwords didn't match";
}
else{
document.getElementById("submitX").style.display = "none";
//status.innerHTML = 'please wait ...';
var x=new XMLHttpRequest();
x.open("POST","signup.php",true);
x.setRequestHeader("content-type","application/x-www-form-urlencoded");
x.onreadystatechange=function(){
if(x.readyState == 4 && x.status == 200){
if(x.responseText != "success"){ //this condition is not met at any circumstances
document.getElementById("submitX").style.display = "block";
status.innerHTML="";
//alert(x.responseText);
status.innerHTML = x.responseText;
}
else{
window.scrollTo(0,0);
status.innerHTML ="";
document.getElementById("diff_for").innerHTML = "";
document.getElementById("form1").innerHTML = "";
document.getElementById("form1").innerHTML = "Thank you for creating an account.A Welcome email has been sent to your email.<br/><br/>Go to <a href='login.php'>Login</a>";
}
}
}
x.send("F="+f+"&L="+l+"&E="+e+"&P="+p+"&P2="+p2);
}
}
PHP
if(isset($_POST['F'])){
$firstname=$_POST['F'];
$lastname=$_POST['L'];
$password=$_POST['P'];
$passmatch=$_POST['P2'];
$pass=md5($password);
$email=$_POST['E'];
$sqlx1="SELECT id FROM user_det WHERE email=? LIMIT 1";
mysqli_stmt_prepare($stmt30, $sqlx1);
mysqli_stmt_bind_param($stmt30, "s", $email);
mysqli_stmt_execute($stmt30);
mysqli_stmt_store_result($stmt30);
mysqli_stmt_bind_result($stmt30, $idx1);
$num_row1x=mysqli_stmt_num_rows($stmt30);
if($num_row1x>0){
//ob_end_clean();
//ob_start();
$abul="<br>This email is already registered.Please use another email";
echo $abul;
exit();
}
else{
$sql11x="INSERT INTO user_det(first_name,last_name,password,email,signup_date)
VALUES(?,?,?,?,now())";
mysqli_stmt_prepare($stmt30, $sql11x);
mysqli_stmt_bind_param($stmt30, "ssss", $firstname, $lastname, $pass, $email);
mysqli_stmt_execute($stmt30);
echo "success";
}
我真的很感谢你的帮助。
感谢您的时间。
答案 0 :(得分:0)
随着你的额外评论,现在我看到发生了什么。
仅使用您发布的部分PHP代码不可见,如果不是太重,您应该发布整个代码。
但是非常确定您的错误位于PHP文件顶部附近:当您测试if(isset($_POST['F'])){
时,您已经输出了原始页面的开头,该页面由同一个PHP文件生成。
顺便说一句,你应该在回复响应Ajax调用的消息后确保exit;
。