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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。