[英]PHP adding unwanted line break to ajax result
Why is PHP adding a line break to my simple AJAX result?为什么 PHP 会在我的简单 AJAX 结果中添加换行符? This couldn't be much easier.
这再简单不过了。 Am I missing something?
我错过了什么吗?
Here is my JS:这是我的JS:
$(document).ready(function() {
$('#inputEmail').change(function(){
// Check to see if email exists
var email = $('#inputEmail').val();
//alert(email);
$.ajax({
url : "php/checkUserEmail.php",
type: "POST",
dataType: "text",
data: {email: email},
success: function(data){
alert(data);
if(data === "exists"){
alert(data);
}
},
error: function (jqXHR, textStatus, errorThrown)
{
alert("ajax error");
}
});
});
});
Here is my php:这是我的php:
<?php
include_once("db_connect.php");
// Catch results sent via $.post and assigns them to php variables.
$email = $_POST['email'];
// Check to see if email exists
$sql = "SELECT * FROM users WHERE email = '$email'";
$conn->prepare($sql);
$result = $conn->query($sql);
$rowCnt = $result->num_rows;
if($rowCnt == 0){
echo trim('new');
}else{
echo trim('exists');
}
For whatever reason, my result data string is returned as /r/nexists, rather than just exists, and thus never gets into my if block, even if I only use == to evaluate the condition.无论出于何种原因,我的结果数据字符串作为 /r/nexists 返回,而不是仅仅存在,因此永远不会进入我的 if 块,即使我只使用 == 来评估条件。 I tried adding the trim() function as you can see without result.
我尝试添加 trim() 函数,如您所见,没有结果。 Your help is much appreciated as this has taken me hours of time for a stupid if condition.
非常感谢您的帮助,因为这花了我几个小时的时间来处理一个愚蠢的 if 条件。
Check if there is an "empty" space after or before the php question marks, those line-breaks are also represented as part of the response.检查php问号之后或之前是否有“空”空格,这些换行符也表示为响应的一部分。
I mean here
<?php?>
And here
I found a solution but I still do not like or understand what is happening.我找到了一个解决方案,但我仍然不喜欢或不明白发生了什么。 You should be able to return a simple string for a result.
您应该能够为结果返回一个简单的字符串。 Anyway, what I did was to just echo the number of rows returned from the DB to my ajax success function.
无论如何,我所做的只是将从数据库返回的行数回显到我的 ajax 成功函数。 I then checked if the result was > 0 on the client and am finally in my IF block.
然后我检查了客户端上的结果是否 > 0,最后在我的 IF 块中。 Here is what I did:
这是我所做的:
JS: JS:
$(document).ready(function() {
$('#inputEmail').change(function(){
// Check to see if email exists
var email = $('#inputEmail').val();
//alert(email);
$.ajax({
url : "php/checkUserEmail.php",
type: "POST",
dataType: "text",
data: {email: email},
success: function(data){
console.log(data);
if(data > 0){
console.log("HURRAY! I'm in my IF");
}
},
error: function (jqXHR, textStatus, errorThrown)
{
alert("ajax error");
}
});
});
});
Here is the PHP:这是PHP:
// Catch results sent via $.post and assigns them to php variables.
$email = $_POST['email'];
// Check to see if email exists
$sql = "SELECT * FROM users WHERE email = '$email'";
$conn->prepare($sql);
$result = $conn->query($sql);
$rowCnt = $result->num_rows;
echo $rowCnt;
?>
This works, and is actually maybe alittle more elegant, but you should be able to return a string without issue.这有效,实际上可能更优雅,但您应该能够毫无问题地返回字符串。
John约翰
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.