当使用ajax将参数传递到onClick事件(ajax在同一页面上)的函数时,我是否得到html和javascript代


Am i getting html and javascript code when using ajax to pass parameters to the function on onClick event (Ajaxing on the same page)

<html>
    tables, textbox, buttons
</html>
<?php
//some php, sql stuff

    echo "<td><input type='button' name='disable' value='Disable' onClick='disable($id);'/></td>";
if(isset($_POST['action']) && $_POST['action']=="delete")
    { 
        if(isset($_POST['ID']) && !empty($_POST['ID']))
        {
        $id = $_POST['ID'];
        echo "Id:".$id;
       //Call to another function  
      die();
    }
?>
<script>
    function disable(id) { 
        jQuery.ajax({   type: 'Post', 
                    url: '', 
                    data: {action: 'delete', ID: id} 
        })
        .done(function(data) { 
           alert("Data Saved: " + data); 
            location.reload();
        }); 
    }
</script>

警告框显示html代码,这是在html块和成功的消息从php块。我不需要显示HTML代码,只需要显示成功的消息。怎么做?非常感谢

问题是您需要在任何html被发送到浏览器之前响应ajax请求,并调用exit;来阻止剩余的页面内容也被发送。这样就可以了:

<?php
if(isset($_POST['action']) && $_POST['action']=='delete'){
    // process request...... 
    $id = $_POST['ID'];
    echo "Id:".$id;
    //Call to another function 
    exit; // stop the script here so it doesnt also return the html content of the page
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
tables, textbox, buttons
<?php
//some php, sql stuff 
// note that $id is not defined where you try to use it here in your code, not sure what that's about....
echo "<td><input type='button' name='disable' value='Disable' onClick='disable($id);'/></td>";
?>
<script>
    function disable(id) {
        jQuery.ajax({   type: 'Post',
            url: '',
            data: {action: 'delete', ID: id}
        })
            .done(function(data) {
                alert("Data Saved: " + data);
                location.reload();
            });
    }
</script>
</body>
</html>