简体   繁体   English

我只希望页面刷新一次

[英]i want my page refresh only once

I am making an admin page where I can delete users but when I delete a user I have to refresh the page. 我正在创建一个可以删除用户的管理页面,但是当我删除用户时,我必须刷新页面。

I used header refresh but that refreshed my page multiple times, I want my page refreshed only once. 我使用标题刷新,但是刷新了我的页面多次,我只希望页面刷新一次。

can you guys help me out? 你们可以帮我吗?

 <?php $dbcon= mysqli_connect("localhost","root",""); mysqli_select_db($dbcon,"register") or die(mysqli_connect_error($dbcon)); $query ="SELECT * FROM users"; $run = mysqli_query($dbcon,$query); while ($row= mysqli_fetch_array($run)){ $user_id = $row[0]; $user_name = $row[1]; $user_pass = $row[2]; $user_mail = $row[3]; ?> <tr align="center"> <td><?php echo $user_id ?></td> <td><?php echo $user_name ?></td> <td><?php echo $user_pass ?></td> <td><?php echo $user_mail ?></td> <td><a href="delete.php?del=<?php echo $user_id;?>"><button onclick="myFunction()">Verdwijder</button></a></td> </tr> <?php } ?> 

您可以使用1)位置reload()2)window.location.href

First of all, you should make two PHP functions. 首先,您应该创建两个PHP函数。 One for displaying the user list and one for deleting users. 一种用于显示用户列表,另一种用于删除用户。 Both with different URLs. 两者都有不同的URL。

Then, consder the following flow: 然后,考虑以下流程:

  1. User goes to the user list page 用户转到用户列表页面
  2. User clicks on delete button, which calls DELETE action (delete URL). 用户单击删除按钮,该按钮将调用DELETE操作(删除URL)。
  3. Delete action deletes record and sends the redirect header back to the user list action . Delete操作删除记录,并将重定向头发送回用户列表操作
  4. User is redirected back to user list action, where the whole loop may repeat itself. 用户被重定向回用户列表操作,整个循环可能会重复进行。

If anything goes wrong in the user deletion function, you have two choices. 如果用户删除功能出了问题,则有两种选择。 Either the delete action can show the user list with the additional message. 删除操作都可以显示带有附加消息的用户列表。 Or - much better - it can redirect back to the user list action, but with additional parameter (saved in session, encoded in URL, a cookie etc). 或者- 更好 -它可以重定向回用户列表操作,但带有附加参数(保存在会话中,编码为URL,Cookie等)。 This parameter is checked always in user list action and, if present, the appropriate status message is shown. 始终在用户列表操作中检查此参数,如果存在,则会显示相应的状态消息。 To prevent the status message being displayed twice, you can delete the parameter (cookie, session, etc) just after you read it in user list action (so-called flash messages). 为防止状态消息显示两次,您可以在用户列表操作中读取参数(即Flash消息)后立即删除该参数(cookie,会话等)。

As a general rule, it is good to separate actions displaying UI to user with actions actually doing something (deleting the records, inserting new ones etc). 通常,将显示UI的操作与实际执行某项操作(删除记录,插入新记录等)的用户分开是很好的。 This way you have much more control of the process flow. 这样,您可以更好地控制流程。 All decent frameworks allow this approach (eg. in Symfony you can create controller function for GET request on some URL, and another for POST or DELETE request on same URL). 所有体面的框架都允许这种方法(例如,在Symfony中,您可以为某个URL上的GET请求创建控制器功能,为同一URL上的POST或DELETE请求创建另一个控制器功能)。

Also, as stated in my comment below your question, you should definitely consider using templating framework to further separate view from logic. 另外,正如我在问题下方的评论中所述,您绝对应该考虑使用模板框架来进一步将视图与逻辑分开。 Twig and Smarty are choices worth consideration (i vote for Twig). Twig和Smarty是值得考虑的选择(我投票赞成Twig)。

You can use this code i hope it will help. 您可以使用此代码,希望对您有所帮助。

if($queryfired){
echo '<script>alert("Record Deleted:")</script>';
echo '<script>window.location.href="somepage.php";</script>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让我的页面只刷新一次? - how can i make my page refresh only once? 我希望我的页面仅滚动一次即可连续多次滚动。 下面是我的代码? - i want my page to scroll only once for for continuous multiple scrolls. below is my code? 为什么来自我的 api 的数据只显示一次,当我刷新页面时它会出错 - Why does data from my api only display once and when I refresh the page it gives an error 我想在第一次加载时只刷新一次反应页面(功能组件),而不是在任何事件上而是在第一次渲染之后,怎么做? - I want to refresh the react page(functional component) only once when it first loads, not on any event but after first render, how to do that? 我的脚本只运行一次,我希望无限次不重新加载html页面 - My script runs only once, I want unlimited times without reloading the html page vue 只刷新一次页面 - vue refresh the page only once xModal window 只打开一次,比我需要刷新页面 - xModal window opens only once, than I need to refresh page Web按钮只能使用一次,然后我必须刷新页面 - Web button only works once, then I have to refresh page 我想从没有页面刷新的情况下提交我的联系人 - i want to submit my contact from without page refresh 我想一键刷新我的页面 5 次 - i want to refresh my page 5 times on one click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM