javascript - 无法从 JavaScript 循环打开 PHP 文件

标签 javascript php html ajax

在我的 HTML 文件中,我有一个带有 onclick 的按钮JavaScript 中的方法 <script>标签。它激活一个名为 deleteRow 的函数,它从 HTML 页面的表中删除一行。

函数内部有一个循环。在该循环中有一个 if删除该行的条件。我想在删除代码后添加一行代码,打开 PHP 文件并向其发送当前的 i循环的 GET 变量。

问题是它确实删除了该行,但没有打开文件。

我尝试使用以下方法来做到这一点:

// 1.
window.location.href = "file.php?number=" + num.toString();

// 2.
var xmlhttp;
if (window.XMLHttpRequest) { 
    xmlhttp = new XMLHttpRequest();
} else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "file.php?number=" + num.toString(), true);
xmlhttp.send();

// 3.
location.href = "file.php?number=" + num.toString();
location.reload();

出于某种原因,没有任何效果,即使使用 break; 也不行。代码之后。我也尝试过 Ajax 来做到这一点。
这是整个函数的代码:

function deleteRow(tableID) {
    var conf = confirm("Are you sure you want to delete the checked products?");
    if (conf == true) {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;
        for (var i = 0; i < rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].querySelector('[type=checkbox]');
            if (null != chkbox && true == chkbox.checked) {
                if (rowCount <= 1) {
                    alert("Cannot Remove all Products");
                    break;
                }
                table.deleteRow(i);
                rowCount--;
                i--;

                var num = i + 1;
                // The above code to load the PHP file goes here
            }
        }
    }
}

你知道为什么它不起作用吗?我必须解决这个问题。

最佳答案

您似乎正在重新加载页面,然后才能使用 window.location.href = "file.php?number="+ num.toString();< 将请求成功发送到您的 PHP 文件 太快了。

您只想在成功调用 PHP 文件之后放置一次 window.location.href

我已在此处的代码片段中附加了代码中的更改。您可以取消注释处理页面重新加载的行以进行测试,但它在本地适用于我。

function deleteRow(tableID) {
    var conf = confirm("Are you sure you want to delete the checked products?");
    if (conf == true) {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;
        for (var i = 0; i < rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].querySelector('[type=checkbox]');
            if (null != chkbox && true == chkbox.checked) {
                if (rowCount <= 1) {
                    alert("Cannot Remove all Products");
                    break;
                }
                table.deleteRow(i);
                rowCount--;
                i--;

                var num = i + 1;

                var xmlhttp;
                if (window.XMLHttpRequest) { 
                    xmlhttp = new XMLHttpRequest();
                } else {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.open("GET", "test.php?number=" + num.toString(), true);
                xmlhttp.send();
                
                // Uncomment the code below to refresh the current page 
                // with the GET parameter
                //window.location.href = window.location.href + '?number=' + num.toString();
                
                // Uncomment the code below to open the test.php page instead
                // with the GET parameter
                //window.location.href = 'test.php?number=' + num.toString();

            }
        }
    }
}
<table id="test" border="1px">
<tr>
<td><input type="checkbox" onclick="deleteRow('test')">delete</td>
<td>product name</td>
</tr>
<tr>
<td><input type="checkbox" onclick="deleteRow('test')">delete</td>
<td>product name</td>
</tr>
</table>

关于javascript - 无法从 JavaScript 循环打开 PHP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46591674/

相关文章:

javascript - 如何显示 select2 的选定值?

javascript - 测试子字符串是否是一个数字以及它是否大于 Javascript 中的另一个数字

PHP关闭HTTP连接然后继续处理?

php - mysql_select_db() 导致我的 $_POST 变量为空

html - 一侧有 CSS 平行四边形,里面有图像

javascript - 由于 Chrome 中的线性渐变,选择不可见

javascript - jquery,ajax,将发布结果加载到div

PHP 使用 mail() 发送 html - 某些服务器以文本形式接收

javascript - 如何根据链接点击显示相同的DIV容器和不同的内容

javascript - 获取 POST 参数不起作用