Php 如何从表单中获取数据并将用户重定向到另一个页面?

Php 如何从表单中获取数据并将用户重定向到另一个页面?,php,html,forms,submit,Php,Html,Forms,Submit,我有一张表格,你可以在下面看到。当按下提交按钮时,我需要一个名为“Orders.php”的脚本将所有数据发送到数据库。另外,当按下按钮时,我希望用户被重定向到另一个页面。问题是,现在当php文件位于action属性中时,用户会看到php代码 有没有办法解决这个问题 HTML Orders.php 您可以在$\u POST变量中找到表单数据,如$\u POST['name'] 要将其转发到另一个页面,您可以使用以下功能 header( "Location: url/to/mySecondPag

我有一张表格,你可以在下面看到。当按下提交按钮时,我需要一个名为“Orders.php”的脚本将所有数据发送到数据库。另外,当按下按钮时,我希望用户被重定向到另一个页面。问题是,现在当php文件位于
action
属性中时,用户会看到php代码

有没有办法解决这个问题

HTML


Orders.php


您可以在
$\u POST
变量中找到表单数据,如
$\u POST['name']

要将其转发到另一个页面,您可以使用以下功能

header( "Location: url/to/mySecondPage.php" );
我的建议

改变你的形式 现在,它将把您的用户带到您希望他去的地方

<form id="TheForm" action="users/final/destination.php" method="post">
  <!-- add this hidden input -->
  <input type="hidden" name="actionScript" value="orders" />
  ...
</form>
  • 不需要头重定向
  • 您可能需要记录调试信息以查找错误
  • 您可以将
    orders.php
    脚本封装在函数中,以避免变量冲突
好的,首先这是您的HTML文件:
您必须在“input”标记中具有“name”属性,才能在php文件中使用其值。
现在,这是您的PHP文件(请确保在PHP文件的末尾放上“”:
orders.php

用户如何查看php代码?您的服务器是否配置良好,能够正确运行php文件?这并不是webdev的经验。。不确定服务器是什么。他们看到的代码就像php是一个html文件,没有“服务器井”这样的东西。“well”是修饰“configured”的副词。。。我对php代码的关注程度不如我对将所有数据发送到.php并同时重定向的关注程度。第一:在php代码中?第二:在html文件或表单标签中?难道没有一种方法可以在将数据发送到php的同时将其重定向到另一个页面吗?或者它必须在php中完成吗?它必须在php中完成,也许您没有打开php标记检查代码中的

header( "Location: url/to/mySecondPage.php" );
<form id="TheForm" action="users/final/destination.php" method="post">
  <!-- add this hidden input -->
  <input type="hidden" name="actionScript" value="orders" />
  ...
</form>
<?php
// Check if this file has been requested through your form
if (isset($_POST['actionScript'])) {
  // you may want to encapsulate this in a function
  ob_start();
  require 'orders.php';
  $debug_info = ob_get_clean();
}
// done, now do everything you wanted in usersFinalDestination.php
Okay, first here is your HTML file:

<!-- HTML FILE -->

YOU MUST HAVE "name" attribute in "input" tag to use its value in php file.

<form id="TheForm" action="orders.php" method="post">
   <div id="Row">
                <input type="text" id="Name" name="Name">
                <input type="text" id="Surname" name="Surname" >
    </div>  

</form>

Now here is your PHP file (just be sure to put "<?php" at starting and "?>" at end of php file ):
orders.php

<?php 


$connect = mysql_connect(“server_name”, “admin_name”, “password”); 
if (!connect) 
{ 
    die('Connection Failed: ' . mysql_error()); 
}
mysql_select_db(“database_name”, $connect);

$_POST["Name"];//to access input field value with name="Name"
$_POST["Surname"];// to access input field value with name="Surname"

//Once you are done with saving data to database you can redirect to another page using:

header("Location:  redirect.php");


?>