【问题标题】:Prevent echo line from AJAX response防止来自 AJAX 响应的回声线
【发布时间】:2019-11-21 22:20:31
【问题描述】:

我想防止来自 AJAX 响应的回显。我有 2 个按钮,我需要通过 AJAX 响应使用 JS 启用和禁用它们。如果 AJAX URL 页面的条件,启用/禁用 HTML 元素的 JS 代码已经写入 PHP 中。通过 AJAX,我可以在 <span id="dupmsg"></span> 上显示结果。 结果将是“已经存在”和“不存在”。我只想显示消息并根据条件启用/禁用按钮。在这里它不起作用:

php 中的索引页面:

<h2>Enabling and Disabling text field using JavaScript</h2>

<form id="registration-form">
Enter your name: <input type="text" id="name">
</form>

<button onclick="disable()">Disable the text field</button>
<button onclick="enable()">Enable the text field</button>


    <p>Ajax Response is: <span id="dupmsg"></span></p>  

<script>
    function check_dup()
    {                       
    var barcode=$("#memb_barcode").val();
        $.ajax({
            type: 'POST',
            url: "ajax_attendance.php",
            data: {

                barcode: barcode

            },

            success: function(msg)
            {

                //alert(msg); // your message will come here. 
                    $('#dupmsg')
                    .css('color', 'red')
                    .html(msg)                                      
            },

                 error: function(jqxhr, status, exception) {
                     alert('Exception:', exception);
                 }
        })              
    }
</script>

Ajax URL 页面:

<?php
$reg_no = mysqli_real_escape_string($con, $_POST['reg_no']);
$barcode = mysqli_real_escape_string($con, $_POST['barcode']);

$sql = "SELECT id from tblstudent  where reg_no = '$reg_no' && barcode like '$barcode' ";
$query = mysqli_query($con, $sql);
$ecount = mysqli_num_rows($query);
if($ecount!=0)
{
    printf("Already Exists");
    echo'   
          <script>
          function disable() {
              document.getElementById("name").disabled = true;
          }
          </script> ';                                
}
else
{
    printf("Not Exists");

    echo'
        <script>
        function enable() {
            document.getElementById("name").disabled = false;
        }
        </script>  ';   
}               
?>

问题是写在 PHP 回显中的 JS 通过 AJAX 响应反射回 span id="dupmsg"。我不想将它带入 AJAX 响应。请帮忙。

【问题讨论】:

  • 不想放到span里面为什么还要echo呢?
  • PHP 脚本应该只在响应中回显 JS 客户端需要的内容。

标签: javascript php html ajax


【解决方案1】:
<h2>Enabling and Disabling text field using JavaScript</h2>

<form id="registration-form">
Enter your name: <input type="text" id="name">
</form>

<button id="disable" onclick="disable()">Disable the text field</button>
<button id="enable" onclick="enable()">Enable the text field</button>


    <p>Ajax Response is: <span id="dupmsg"></span></p>  

<script>
    function check_dup()
    {                       
    var barcode=$("#memb_barcode").val();
        $.ajax({
            type: 'POST',
            url: "ajax_attendance.php",
            data: {

                barcode: barcode

            },

            success: function(msg)
            {

                if(msg=='true'){
                    document.getElementById("name").disabled = true;
                    $("#disable").attr("disabled", true); // write the id of the button u want to hide
                    $('#dupmsg')
                    .css('color', 'red')
                    .html("Already Exists") 
                }
                else if(msg=='false')
                    {
                        document.getElementById("name").disabled = false;
                        $("#enable").attr("disabled", false); // write the id of the button u want to hide
                        $('#dupmsg')
                    .css('color', 'red')
                    .html("Not Exists")
                    }


            },

                 error: function(jqxhr, status, exception) {
                     alert('Exception:', exception);
                 }
        })              
    }

     function enable() {
            document.getElementById("name").disabled = false;
        }
    function disable() {
              document.getElementById("name").disabled = true;
          }
</script>




ajax_attendance.php
<?php
$reg_no = mysqli_real_escape_string($con, $_POST['reg_no']);
$barcode = mysqli_real_escape_string($con, $_POST['barcode']);

$sql = "SELECT id from tblstudent  where reg_no = '$reg_no' && barcode like '$barcode' ";
$query = mysqli_query($con, $sql);
$ecount = mysqli_num_rows($query);
if($ecount!=0)
{
    return true;

}
else
{
    return false;

}               
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多