php PHP刷新按钮onclick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15938543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:13:15  来源:igfitidea点击:

PHP Refresh button onclick

phphtmlsqlbuttononclick

提问by TheShadowbyte

I have this while loop that echoes entries from a SQL database. I have an "Add to Cart" button which is supposed to remove the entry from the list and add it somewhere else. This part works, but once I click on the "Add to Cart" button, the entry is not removed right away, I have to manually refresh the page or visit another page and come back in order to see that the item is removed. I tried using the following (and few similar) lines on the onclick propery of the button, but it didn't work.

我有这个 while 循环,它回显 SQL 数据库中的条目。我有一个“添加到购物车”按钮,它应该从列表中删除条目并将其添加到其他地方。这部分有效,但是一旦我单击“添加到购物车”按钮,该条目不会立即删除,我必须手动刷新页面或访问另一个页面并返回以查看该项目已被删除。我尝试在按钮的 onclick 属性上使用以下(和一些类似的)行,但没有用。

echo "<br /><center><input type='submit' name='submitAdd' value='Add to Cart' onclick='window.location.reload();'></center>";

Any suggestions are appreciated.

任何建议表示赞赏。

Thank you.

谢谢你。

EDIT: This is the while loop that I use to generate the table. It is within a tag which is within the .

编辑:这是我用来生成表的 while 循环。它位于 .

while ($row = mysql_fetch_array($song_query)) {
    foreach ($_SESSION['selected_items'] as $key => $value) {
        if ($value == $row['Item_ID']) {
            $rowID = $row['Item_ID'];
            echo "<tr style='background-color: #66FFFF;'>";
            echo "<td><input type='checkbox' name='removeFromCart[]' value='$rowID'></td>";
            echo "<td>" . $row['Item_ID'] . "</td>";
            echo "<td>" . $row['Name'] . "</td>";
            echo "<td>" . $row['Artist'] . "</td>";
            echo "<td>" . $row['Album'] . "</td>";
            echo "<td>" . $row['Time'] . "</td>";
            echo "<td>" . $row['Year'] . "</td>";
            echo "<td>" . $row['Bit_Rate'] . "</td>";
            echo "<td>" . $row['Sample_Rate'] . "</td>";                
            echo "</tr>";
        }
    }
}

回答by asifrc

PHP is a server-side language, so any "actions" will be executed on the server, before the results are displayed to the user. In order to modify the page after it has been loaded on the user's browser, you have to use a client-side language, such as JavaScript. If you want to show an update to your cart without having to reload the page, then using an AJAX request is your best option. Check out the tutorial at http://www.w3schools.com/ajax/and let me know if you have any questions about how to implement one :)

PHP 是一种服务器端语言,因此在将结果显示给用户之前,任何“操作”都将在服务器上执行。为了在页面加载到用户浏览器后对其进行修改,您必须使用客户端语言,例如 JavaScript。如果您想在无需重新加载页面的情况下显示购物车的更新,那么使用 AJAX 请求是您的最佳选择。查看http://www.w3schools.com/ajax/ 上的教程,如果您对如何实现一个有任何疑问,告诉我:)

Update: Force Reload
If you want to just stick with reloading the page as is, try onclick='window.location.reload(true);'instead of onclick='window.location.reload();'. The trueparameter forces a get request to the server. http://www.w3schools.com/jsref/met_loc_reload.asp

更新:强制重新加载
如果您只想按原样重新加载页面,请尝试onclick='window.location.reload(true);'代替onclick='window.location.reload();'。该true参数强制向服务器发出获取请求。http://www.w3schools.com/jsref/met_loc_reload.asp

回答by Dalthon

I use a link that references to the same page

我使用引用同一页面的链接

<a href="<?php $_SERVER['PHP_SELF']; ?>">Recargar</a>

回答by Lee Binder

not a button but a simple link, but maybe still useful for some:

不是按钮而是一个​​简单的链接,但可能对某些人仍然有用:

<?php 
$page = $_SERVER['PHP_SELF'];
print "<a href=\"$page\">Reload this page</a>";
?>

回答by Micaiah Wallace

If the only feature you are looking for is to reload the page on the button's click, the way I typically do that is onclick="history.go(0)"

如果您正在寻找的唯一功能是在单击按钮时重新加载页面,我通常这样做的方式是 onclick="history.go(0)"

But if the form button is what's submitting a form and already reloads the page then I don't see why you couldn't put the while loop after your other code like this,

但是,如果表单按钮是提交表单并且已经重新加载页面的内容,那么我不明白为什么您不能将 while 循环放在像这样的其他代码之后,

CHECK FOR FORM() { UPDATE SQL DB }

检查 FORM() { 更新 SQL 数据库 }

WHILE () { OUTPUT DB DATA }

WHILE () { 输出数据库数据}