簡體   English   中英

無法通過JavaScript循環打開PHP文件

[英]Can't open a PHP file from a JavaScript loop

在我的HTML文件上,我有一個按鈕,該按鈕在JavaScript <script>標記中具有onclick方法。 它激活了一個稱為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文件window.location.href = "file.php?number=" + num.toString(); 太快了。

您只希望在成功調用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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM