简体   繁体   English

PHP-避免在提交表单后重新加载页面

[英]PHP - Avoid reloading page after form submit

I'm new to php and web-programming... I'm working on a school project modifying databases through a corporate site. 我是php和网络编程的新手。我正在一个学校项目中,通过公司站点修改数据库。

I'm using some forms to allow the user to look for information that will be used to fill up other forms on the same page. 我正在使用一些表格,以允许用户查找将用于填充同一页面上其他表格的信息。 In this case, a car rental, I look for available cars, show them on a table and then the user picks a car and its info would fill up some other inputs on the same page. 在这种情况下,租车时,我会寻找可用的汽车,将它们显示在桌子上,然后用户选择汽车,其信息将在同一页面上填写其他一些输入。

I'm able to do the search and show the result, but then when the user picks the car and clicks submit the whole page is uploaded again. 我可以进行搜索并显示结果,但是当用户选择汽车并单击提交时,整个页面将再次上传。 Please, any suggestions? 有什么建议吗?

J. J.

Redirect to a different page/route, other than the one page was submitted to. 重定向到另一个页面/路由,而不是提交到的页面。

An example for different page redirection. 不同页面重定向的示例。

header("location: differentpage.php");
exit;

Once you are in differentpage.php you cannot reload the POST request. 一旦进入differentpage.php ,就无法重新加载POST请求。 TO get the data you can use SESSION or GET parameter as per requirements. 要获取数据,可以根据需要使用SESSIONGET参数。

You need to do this in JavaScript, not in PHP. 您需要使用JavaScript而不是PHP来执行此操作。

PHP is a server-sided language, what you do is parsed on the server side. PHP是一种服务器端语言,您所做的工作是在服务器端进行解析的。

To stop a form submission you need to use JavaScript. 要停止提交表单,您需要使用JavaScript。 Set an onSubmit event on your form.. 在表单上设置一个onSubmit事件。

Example code: 示例代码:

<script>
  function validation()
  {
    // do stuff

    return false; // stops submission of form
  }
</script>
<form onsubmit="return validation();">
  <input type="submit" value="Submit" />
</form>

If you don't want to use javascript and ajax you can use an iframe and set the target of the form as the iframe's name 如果您不想使用javascript和ajax,则可以使用iframe,并将表单目标设置为iframe的名称

note that you have to write 2 different pages to achieve this: 请注意,您必须编写2个不同的页面才能实现此目的:

  1. One that creates the form and the iframe 一个创建表单和iframe的文件
  2. One that display data and uses posted values 一种显示数据并使用过帐值的方法

Code in index.html index.html中的代码

<form action="test.php" target="tar">
    <input type="submit"/>
</form>
<iframe name="tar">
</iframe>

Code in test.php test.php中的代码

var_dump($_POST); // show your data

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

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