简体   繁体   English

在php中插入数据库后刷新到数据发布页面

[英]Refreshing to the data posted page after database insertion in php

Suppose I have three pages in php: Page1, Page2 and Page3. 假设我在php中有三个页面:Page1,Page2和Page3。

  1. Page1 has a link (anchor tag) with a variable. Page1有一个带有变量的链接(锚标记)。 Suppose in the anchor tag href is like- href="Page2.php?id='$myid'" , where $myid has been posted in a variable id to the Page2.php if I click that link. 假设锚标记中的href是like- href="Page2.php?id='$myid'" ,如果我单击该链接,其中$myid已以变量id的形式发布到Page2.php。
  2. In the Page2.php I have a variable suppose $newid where I am storing the id variable (ie posted from Page1.php) using GET. 在Page2.php中,我有一个变量,假设$newid使用GET存储id变量(即从Page1.php发布)。 It is like- $newid=$_GET['id']; $newid=$_GET['id']; - $newid=$_GET['id']; . In this page I am posting some input data to Page3.php using a submit button. 在此页面中,我使用提交按钮将一些输入数据发布到Page3.php。
  3. In Page3.php, my posted data are inserted to my database ( mySql ) correctly. 在Page3.php中,我发布的数据正确地插入了我的数据库( mySql )。

Now my question is, after successful insertion of data to the database in Page3.php, how can I revert back to Page2.php since there is a variable id from Page1.php ( $newid=$_GET['id']; ) using a javascript message box. 现在我的问题是,在成功将数据插入到Page3.php中的数据库后,由于Page1.php中有变量id$newid=$_GET['id']; ),我该如何恢复为Page2.php $newid=$_GET['id'];使用JavaScript消息框。 Even I have stored that $newid to a SESSION variable also in Page2. 甚至我也将$newid存储到SESSION变量中,也存储在Page2中。 Please help. 请帮忙。

Assuming you want to manipulate the PHP variable in JavaScript or run logic against it, I would recommend making use of the HTML DOM itself as an intermediary point. 假设您要在JavaScript中操纵 PHP变量或对其进行逻辑处理,我建议您将HTML DOM本身用作中介点。

First, echo the PHP variable somewhere on the page: 首先,在页面上的某处显PHP变量:

<span id="php">
  <?php echo $newid; ?>
</span>

This output location can be hidden with CSS if need be. 如果需要,可以使用CSS 隐藏此输出位置。

#php {
  display: none;
}

Then, target the element with JavaScript: 然后,使用JavaScript定位元素:

var id = document.getElementById('php').innerHTML;

You can then manipulate the variable as you would like, running conditional logic, and redirecting accordingly: 然后,您可以根据需要操纵变量,运行条件​​逻辑并相应地重定向:

 var id = document.getElementById('php').innerHTML; if (id == 2) { // Logic triggers window.location = "page1.php?id=" + id; } 
 <span id="php">2</span> 

The above example will pass the id parameter as $_GET to page 1. 上面的示例会将id参数作为$_GET传递给第1页。

However, it's important to note that $_SESSION data persists between pages, so you could always just use $_SESSION['id'] directly, from any of your pages. 但是,请务必注意, $_SESSION数据页面之间仍然存在,因此您始终可以直接从任何页面直接使用$_SESSION['id']

Hope this helps! 希望这可以帮助! :) :)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM