doujin4031 2015-12-25 14:30
浏览 61
已采纳

在AJAX中成功后重新启用“提交”按钮

I have been trying to enable Submit Button after the AJAX Request success. The validation is in PHP file which has following code

    if(!empty($_POST["username"])) {

        $fetch = "SELECT * FROM student WHERE std_username='" . $_POST["username"] . "'";
        $result = mysqli_query($con, $fetch);
        $user_count = mysqli_num_rows($result);

        if($user_count>0) {
            echo "<span class='status-not-available'> Username Not Available.</span>";
        }else{
            echo "<span class='status-available'> Username Available.</span>";
        }
    }

and this is AJAX

    function checkAvailability() {
        $("#loaderIcon").show();
        jQuery.ajax({
        url: "check_availability.php",
        data:'username='+$("#username").val(),
        type: "POST",
        success:function(data){
            $("#user-availability-status").html(data);
            $("#loaderIcon").hide();
        },
        error:function (){}
        });
    }

I have used this is success what it does is disable the submit button even the data matches the request or not, the button stays disabled

    $("#submit").attr("disabled", true);

and following in error

    $("#submit").attr("disabled", false);

I have gone throught these questions and tried the solutions but those solutions didn't work out for me.

jQuery: Re-enabling disabled input buttons after 'success:' is run and

Disable Button while AJAX Request

this is the HTML

    <form method="POST">
        <label>Check Username:</label>
        <input name="username" type="text" id="username" class="demoInputBox" onBlur="checkAvailability()"><span id="user-availability-status"></span><br>    
        <label>Check Username:</label>
        <input name="email" type="text" id="email" class="demoInputBox" onBlur="checkAvailabilitye()"><span id="email-availability-status"></span> <br>
        <input type="submit" id="submit"> 
    </form>

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • duangejian6657 2015-12-25 14:36
    关注

    on the success event, you need to set the disabled property value to false. This will re-enable the button

    $( "#submit" ).prop( "disabled", true ); //disable when ajax call starts
    
    jQuery.ajax({
        url: "check_availability.php",
        data:'username='+$("#username").val(),
        type: "POST",
        success:function(data){
            $( "#submit" ).prop( "disabled", false ); //enable it back
            $("#user-availability-status").html(data);
            $("#loaderIcon").hide();
        },
        error:function (){}
        });
    

    EDIT : As per the comments,

    Looks like you are trying to enable/disable button based on the check you are doing aginist the db table. Currently your php script is returning a human readable string. May be you can change your php script to return a flag value which your javascript code can use.

    if($user_count>0) {
            echo "no";
    }else{
            echo "available";
    }
    

    And in your success handler inspect this value and hide/show buttons/messages

    success:function(data){
            $("#loaderIcon").hide();
            var msg="Not available";
    
            if(data=="available")
            {
              $( "#submit" ).prop( "disabled", false ); //enable it back
              msg="Username available";
            }
            $("#user-availability-status").html(msg);
    
    },
    

    I am not a PHP person. There could be a way to return JSON structure from your PHP page which can return both the flag and the message you want to show in the message div. You may try that too.

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测