简体   繁体   English

文本类型输入元素的默认值

[英]default value of text type input element

I'm trying to check whether the email user just entered is in the database or not and display message.its working fine. 我正在尝试检查刚输入的电子邮件用户是否在数据库中,并显示message.its正常。 but when I delete entered value and focus out then other option is displayed. 但是当我删除输入的值并聚焦时,会显示其他选项。 But I don't want to display neither.Here is my codes-ajax 但是我不想显示任何东西。这是我的代码-ajax

<script>
$(document).ready(function(){

  $("#email").focusout(function(){
  var emailVal = $('#email').val();
    $.ajax({
        type: 'post',
        url: 'check_email.php',
        data:  { 
         myparam:emailVal //set it with a parameter name
      },


        success: function( data ) {
        alert(data);
        if(data==1){
        document.getElementById('email_check').innerHTML="<p style='color:red;'>The email you have entered is already exist!</p>";
           }
            if(data==0){
            document.getElementById('email_check').innerHTML="<p style='color:green;'>The email you have entered is available!</p>";}
        }
    });
  });
});


</script>

Here is my check_email.php file, 这是我的check_email.php文件,

      <?php          
require_once("connnection.php"); 
$con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();

}

if (isset($_POST['myparam'])){
$email=mysqli_real_escape_string($con,$_POST['myparam']);
$sql = "SELECT * FROM user WHERE email='$email'";
$select=mysqli_query($con,$sql) or die("fail");
$row = mysqli_num_rows($select);

if ($row >0) {

    echo 1;
}else {

echo 0;
}
else
echo "post error";                                

After delete entered value and focus out (click another place), the following happens, using alert(data) I got 0 as data from server. 删除输入的值并聚焦(单击另一个位置)后,发生以下情况,使用alert(data)我从服务器得到0作为数据。 How could this happened. 这怎么可能发生。 If there is no value in the text field, how it entered in to sql query segment in php file. 如果文本字段中没有值,则它如何输入php文件中的sql查询段。 Is there any default value or something which pass behind the scene? 是否有任何默认值或在幕后传递的东西? please explain? 请解释?

Just check the value of emailVal either it exist or not. 只要检查emailVal的值是否存在即可。 Try this 尝试这个

$(document).ready(function(){
    $("#email").focusout(function(){
        var emailVal = $('#email').val();
        if($.trim(emailVal)){ //Added the new condition
            $.ajax({
                type: 'post',
                url: 'check_email.php',
                data:  {
                    myparam:emailVal //set it with a parameter name
                },
                success: function( data ) {
                    alert(data);
                    if(data==1){
                        document.getElementById('email_check').innerHTML="<p style='color:red;'>The email you have entered is already exist!</p>";
                    }
                    if(data==0){
                        document.getElementById('email_check').innerHTML="<p style='color:green;'>The email you have entered is available!</p>";}
                }
            });
        }else{ //If empty then nothing will be shown
            $('#email_check').html('');
        }

    });
});

If there is no value in the text field it will send 如果文本字段中没有值,它将发送

myparam: "" myparam:“”

That will then check if there is an email address with "" and that will return 0. 然后,将检查是否存在带有“”的电子邮件地址,并且该电子邮件地址将返回0。

Just check if the text field is empty before doing the php call to get around this. 只需检查文本字段是否为空,然后再进行php调用即可解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM