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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改