[英]Can't open a PHP file from a JavaScript loop
On my HTML file, I have a button that has an onclick
method in a JavaScript <script>
tag. 在我的HTML文件上,我有一个按钮,该按钮在JavaScript
<script>
标记中具有onclick
方法。 It activates a function called deleteRow
, which deletes a row from a table in the HTML page. 它激活了一个称为
deleteRow
的函数,该函数从HTML页面的表中删除一行。
Inside the function, there is a loop. 函数内部有一个循环。 In that loop there is an
if
condition which deletes the row. 在该循环中,有一个
if
条件删除该行。 I want to add a code line after the deletion code that opens a PHP file and sends it the current i
of the loop as GET variable. 我想在删除代码之后添加一个代码行,该代码行将打开一个PHP文件,并将该循环的当前
i
作为GET变量发送给它。
The problem is that is that it does delete the row, but it doesn't open the file. 问题在于它确实删除了该行,但没有打开文件。
I've tried to do it using: 我试着用以下方法做到这一点:
// 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();
For some reason nothing works, not even with a break;
由于某种原因,什么都没有,即使
break;
after the code. 代码之后。 I've also tried to do that with
Ajax
. 我也尝试过用
Ajax
做到这一点。
This is the code of the whole function: 这是整个功能的代码:
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
}
}
}
}
Do you have any idea why it isn't working? 您知道为什么它不起作用吗? I have to fix this.
我必须解决这个问题。
It seems like you are reloading the page before the request can be sent successfully to your PHP file by using the window.location.href = "file.php?number=" + num.toString();
似乎您正在重新加载页面,然后才能使用
window.location.href = "file.php?number=" + num.toString();
来将请求成功发送到PHP文件window.location.href = "file.php?number=" + num.toString();
too soon. 太快了。
You want to put window.location.href
only once and after you have successfully made the call to your PHP file. 您只希望在成功调用PHP文件之后放置一次
window.location.href
。
I have attached the change in your code in a snippet here. 我在此处的代码段中附有您代码中的更改。 You can uncomment the lines that handles the page reload to test but it works for me locally.
您可以取消注释处理页面重新加载以进行测试的行,但它在本地对我有用。
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.