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 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题