duangu8264 2014-11-02 12:25
浏览 55
已采纳

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

I'm trying to check whether the email user just entered is in the database or not and display message.its working fine. 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

<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,

      <?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. How could this happened. If there is no value in the text field, how it entered in to sql query segment in php file. Is there any default value or something which pass behind the scene? please explain?

  • 写回答

2条回答 默认 最新

  • dongqie7806 2014-11-02 12:30
    关注

    If there is no value in the text field it will send

    myparam: ""

    That will then check if there is an email address with "" and that will return 0.

    Just check if the text field is empty before doing the php call to get around this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?