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

时间:2013-11-28 17:18:23

标签: php html forms submit

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

有关如何解决此问题的任何想法?

HTML

<form id="TheForm" action="orders.php" method="post">
            <div id="Row">
                <input type="text" id="Name" placeholder="*Förnamn" required >
                <input type="text" id="Surname" placeholder="*Efternamn" required >
            </div>      
            <div id="Row">
                <input type="email" id="FirstEmail" placeholder="*e-postadress" autocomplete="on" required >
                <input type="email" id="SecondEmail" placeholder="*Verifiera e-postadress" autocomplete="off" required >
            </div>
            <div id="Row">
                <input type="text" id="Town" placeholder="*Ort" required >
            </div>  
            <div id="Row"> 
                <input type="text" id="Address" placeholder="*Adress" required >
            </div>
            <div id="Row">
                <input type="text" id="PostCode" placeholder="*Postnummer" required >
            </div>
            <div id="Row">
                <input type="text" id="MobileNumber"  placeholder="*Mobilnummer" required >
                <input type="text" id="TelephoneNumber" placeholder="Telefonnummer" >
            </div>
            <textarea id="Comment" placeholder="Förslag på hur vi skulle kunna förbättra oss!"></textarea>
            <input type="submit" id="Submit" onclick="SlutSidan.html" value="Skicka">
        </form>

Orders.php

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

非常感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

您可以在$_POST变量$_POST['name']

中找到您的表单数据

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

header( "Location: url/to/mySecondPage.php" );

答案 1 :(得分:0)

我的建议

更改表单

现在它会将您的用户带到您希望他去的地方

<form id="TheForm" action="users/final/destination.php" method="post">
  <!-- add this hidden input -->
  <input type="hidden" name="actionScript" value="orders" />
  ...
</form>

更改users/final/destination.php

这应该在文件的顶部

<?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
  • 无需重定向标头
  • 您可能希望记录调试信息以查找错误
  • 您可以将orders.php脚本封装在函数中以避免可变冲突

答案 2 :(得分:0)

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");


?>