doutuzhuohao6449 2017-10-30 09:15
浏览 79
已采纳

jQuery Ajax如果提交的变量是ok隐藏按钮

I am trying to hide the submit button if the email is the same with the one in the database from action.php. How could I implement this in my following code:

<form onsubmit="return submitdata();">
    <input type="text" id="mail">
    <input type="submit" value="Check">
</form>

<p id="msg"></p>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script type="text/javascript">

function submitdata()
{
    var name=document.getElementById('mail').value;

$.ajax({
    type: 'post',
    url: 'action.php',
    dataType: 'text',
    data: {
        'name':name
    },
    cache:false,
    success: function (data) {
        console.log(data);
        $('#msg').html(data);
    }
});

return false;
}

</script>

action.php:

<?php 
require_once 'config.php';
$email_ck=$_POST['name'];
if(extract($crud->get_email($email_ck))){
    echo "Response: ".$email;
    $hide = 1;
}else{
    echo 'hmmmm';
}
?>

When the email coincide I get the correct message, but how could I call back $hide to determine to hide my submit button?

  • 写回答

1条回答 默认 最新

  • douxin1956 2017-10-30 09:20
    关注

    Instead of returning a message, return an indication. (In the JS script write the message accordingly)

    A detailed json string would be a good idea but for simplification see the following.

    PHP:

    <?php 
    require_once 'config.php';
    $email_ck=$_POST['name'];
    if(extract($crud->get_email($email_ck))){
        echo "already_exists";
    }else{
        echo 'not_exists';
    }
    ?>
    

    JS:

    $.ajax({
        type: 'post',
        url: 'action.php',
        dataType: 'text',
        data: {
            'name':name
        },
        cache:false,
        success: function (data) {
            if(data == 'already_exists'){
               $('#buttonId').hide();
            } else if(data == 'not_exists'){
               $('#msg').html('Response: ' +name);
            }
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题