仅当我的数据变量不为空时才如何关注表单字段 [英] How to focus on a form field only if my data variable is not empty
问题描述
我一直在使用php和ajax来验证数据库中是否存在以表单形式插入的电子邮件.
I have been using php and ajax to validate if an email inserted in my form exists in my database.
我正在使用jquery将电子邮件值发送到我的php文件,如果找到了电子邮件,则返回一条消息.我的代码工作正常,但是我希望如果找到电子邮件,则光标将集中在#usu_email字段上,直到更改电子邮件为止.之后,它应该允许我继续下一个领域.
I am using jquery to send the email value to my php file and return a message if the email is found. My code is working fine but I want if an email is found the cursor be on focus on the #usu_email field until the email be changed. After this, it should allow me to continue to next field.
这是我正在使用的jquery代码:
This is the jquery code I am using:
function getemail(value) {
var usumail = $("#usu_email").val();
$.ajax({
type: "POST",
url: "ajax_email.php",
data: "usu_email=" + usumail,
success: function(data, textStatus) {
if (data !== null) {
$("#eresult").html(data);
$("#usu_email").focus();
}
},
});
};
我的问题是,如果数据库中不存在和电子邮件,则光标将继续关注#usu_email字段,并且不允许我继续下一个字段.
My problem is that if and email does not exist in my database the cursor keeps doing focus on my #usu_email field and does not allow me to continue to next field.
对于这个问题,我将不胜感激,因为我对jquery的了解很少.
I will appreciate any help about this problem because I know very little about jquery.
推荐答案
首先...您的条件if (data !== null)
始终将为true,因为将始终提供数据.一个空字符串.
First... Your condition if (data !== null)
always will be true since there always will be a data provided... Be it an empty string.
唯一不会出现data
的情况是Ajax错误...并且该条件甚至不会被评估,因为success
回调将不会执行.
The only case where there will be no data
is on Ajax error... And the condition won't even be evaluated because the success
callback won't execute.
接下来,我假设您的Ajax请求是在$("#usu_email")
模糊条件下触发的...否则,我不知道您如何实现«不允许我继续».
Next, I assume that your Ajax request is triggered on $("#usu_email")
blur... Else, I don't know how you achieve «does not allow me to continue».
以这种方式修改它以比较响应:
Modify it in this way to compare a response:
function getemail(value) {
var usumail = $("#usu_email").val();
$.ajax({
type: "POST",
url: "ajax_email.php",
data: "usu_email=" + usumail,
datatype: "json",
success: function(data) { // There is only one argument here.
// Display the result message
$("#eresult").html(data.message);
if (data.email_exist == "yes") {
$("#usu_email").focus();
}
if (data.email_exist == "no") {
// Something else to do in this case, like focussing the next field.
}
},
});
};
在PHP方面,您必须提供json响应.看起来像这样:
On the PHP side, you have to provide the json response. It would look like something like this:
<?php
// You have this variable to compare against the database
$email = $_POST[usu_email];
// You say it is working.
// ...
// Then, you certainly have a result... Say it's $found (true/false).
// Build an array of all the response param you want to send as a response.
if($found){
$result[email_exist] = "yes";
$result[message] = "The submitted email already exist.";
}else{
$result[email_exist] = "no";
$result[message] = "A success message about the email here.";
}
// Add this header to the returned document to make it a valid json that doesn't need to be parsed by the client-side.
header("Content-type:application/json");
// Encode the array as a json and print it. That's what is sent in data as an Ajax response.
echo json_encode($result);
?>
请注意不要回声其他任何内容.甚至没有空格或行返回.
Be carefull not to echo anything else. Not even a blank space or a line return.
这篇关于仅当我的数据变量不为空时才如何关注表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!