【问题标题】:Are there better solutions than redirect header location?有比重定向标头位置更好的解决方案吗?
【发布时间】:2018-07-10 23:18:00
【问题描述】:

目前我正在阻止使用重定向标头位置重新提交。但我认为页面加载速度较慢,因为去一个地方只是为了重定向到另一个地方是浪费时间。它为流程增加了一个额外的步骤。

PHP 还有哪些其他选择?

有没有办法在提交时将表单数据发送到另一个文件/页面而不会“重定向”到该页面?是否有可能在不需要重定向的情况下做到这一点,因为你从一开始就没有发送到那里。只需将“表单数据”传递到其他页面。

也可以使用其他解决方案,只要是 PHP。

保重

【问题讨论】:

  • 你现在做得很好。
  • 您可以通过设置html表单标签的“action”属性将表单提交到您想要的任何页面。像这样:
    我猜你现在正在做的是提交一个没有设置此属性的表单标签,并根据某些条件在同一个脚本上进行重定向。
  • 如果您不想重新加载页面,您可以使用 Ajax。
  • 如果您不想重定向到另一个页面,请将表单标签中的 action="" 留空,然后在 php 文件的顶部您可以执行类似 if(isset($_GET['form_submited'])) { //do something here } 的操作(form_submited 指到提交按钮name 属性。)。但是,这将重新加载页面,如果您不想重新加载它,您可以将我上面解释的代码放在文件顶部的函数中,就像@Jonny 所说,通过 Ajax 调用该函数。
  • @JacekDziurdzikowski 如果我使用该操作,我会将表单发送到该特定页面,但随后我也会被重定向到该页面。这就是为什么我需要重定向回来,这就是我目前的做法。但我想将表单发送到特定页面而不被重定向,所以我不需要重定向回来。我不知道 PHP 是否可行?

标签: php forms duplicates submit refresh


【解决方案1】:

使用 include 将 PHP 包含在您的其他页面中,它将在没有重定向的情况下处理表单。

include 'YourFile.php';

这是一个结构示例。

 <?php
 include 'functions/db.php';// your db connection to process the form
 $error['last'] = '';
 $error['first'] = '';
 $error['user'] = '';
 if(isset($_POST['addUser'])){ // Check if form is submitted
 $LastImp = $_POST['LastImp'];
 $FirstImp = $_POST['FirstImp'];
 $UserImp = $_POST['UserImp'];
 if($LastImp == '' || $FirstImp == '' || $UserImp == ''){// Check if inputs are empty.
 if($streetImp == ''){$error['street'] = 'Must enter a Street';}
 if($LastImp == ''){$error['last'] = 'Must enter a Last Name';}
 if($FirstImp == ''){$error['first'] = 'Must enter a First Name';}
 if($UserImp == ''){$error['user'] = 'Must enter a Username';}  
 $error['alert'] = "All Fields are Required.";
 include 'views/employee_front.php';
 exit();
 } else {
 //Process the Form.
 }}
 include 'YourFile.php';// the php reads this first because all the other code is conditional upon submit.

这是你的文件页面

 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
 <title>Untitled 1</title>
 <body>

 <form method="post">
 <div class="rowContainer">
 <label class="User">Username</label>
 <input class="Userimp" name="UserImp" type="text"></div>
 <div class="error"><?php echo $error['user']; ?></div>
 <div class="rowContainer">
 <label class="First">First&nbsp;Name</label>
 <input class="Firstimp" name="FirstImp" type="text"></div>
 <div class="error"><?php echo $error['first']; ?></div>
 <div class="rowContainer">
 <label class="Last">Last&nbsp;Name</label> 
 <input class="Lastimp" name="LastImp" type="text"></div>
 <div class="error"><?php echo $error['last']; ?></div>
 <button class="addSubmitA" type="submit" name="addUser">Add&nbsp;User</button>
 </form>

【讨论】:

  • 这就像将所有代码都写在同一个文件中。这意味着当我在提交后刷新页面时,它会给我一个“警告弹出窗口”,它将再次提交。那不是我想要的。这就是为什么现在我将表单操作放到另一个 php 文件中,在那里我处理然后重定向回来。乳清我可以在提交后刷新页面而不会收到“关于重新提交的弹出警告”它解决了这个问题,但我想看看它是否可以在没有重定向的情况下以某种方式完成,因为这只是一个额外的步骤,会减慢速度。
  • 可以用 AJAX 完成。如果你愿意,我可以调整我对 Ajax 的回答?
  • 只有 AJAX?没有其他你可能想到的 php 解决方案?
  • 不刷新页面。对不起
  • 刷新与浏览器的外部页面无关,Ajax是避免刷新的唯一解决方案,我能想到
【解决方案2】:

在表单标签&lt;form method="POST" action="submit-data.php"&gt;和submit-data.php的action属性中给出php文件,数据提交成功后给出header('Location:form.php')。它对我来说很好,无需在浏览器重新加载时重新提交数据。这是防止在页面重新加载时重新提交数据的一种方法。第二种方式是使用 Ajax Jquery 方法提交数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 2021-08-24
    • 2013-10-22
    • 2010-09-29
    • 2013-07-21
    • 2016-03-27
    • 1970-01-01
    相关资源
    最近更新 更多