PHP重定向没有被执行?

时间:2016-04-08 10:03:42

标签: php sql forms

我在存储表单值时遇到问题。我的目标是提交我的第一个表单,将值保存在变量中,重定向到我的第二个表单,并在我的数据库中提交/存储所有表单值(SQL)。

HTML

<form action="process.php" class="form" method=POST name="firstform" onsubmit="return chkForm()">
      <label id="form_title">Bevor wir anfangen können, benötigen wir noch ein paar Informationen von Ihnen:</label>
      <br/>
      <br/>
      <label for="name">Benutzername</label> 
      <input type="text" id="name" name="name" value="">

      <label for="email">Email</label>  
      <input type="email" id="email" name="email" value="">

      <button type="submit" name="submit" value="abschicken">Eingaben absenden</button>
    </form>

process.php

try {
$connect = new PDO("mysql:host=$host;dbname=$name;charset=utf8mb4", $user, $pass);
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connect->beginTransaction();   
if (!empty($_POST['firstform'])) {
    $alias = $_POST['name'];
    $mail = $_POST['email'];
    echo '<script type="text/javascript">window.document.location.href = "http://www.example.de/form2.php";</script>';
    exit;
}
if(isset($_POST['fullform'])){ 
  $connect->exec("INSERT INTO user (name, email, answer_0, answer_1, answer_2, answer_3, answer_4, answer_5, answer_6, answer_7, answer_8, answer_9, answer_10, answer_11, answer_12, answer_13, answer_14, answer_15, answer_16, answer_17) VALUES ('$alias', '$mail', '$_POST[answer_0]', '$_POST[answer_1]', '$_POST[answer_2]', '$_POST[answer_3]', '$_POST[answer_4]', '$_POST[answer_5]', '$_POST[answer_6]', '$_POST[answer_7]', '$_POST[answer_8]', '$_POST[answer_9]', '$_POST[answer_10]', '$_POST[answer_11]', '$_POST[answer_12]', '$_POST[answer_13]', '$_POST[answer_14]', '$_POST[answer_15]', '$_POST[answer_16]', '$_POST[answer_17]');");

} 
$connect->commit();
echo "Danke Für Ihre Teilnahme";
echo $_POST['email'];
exit;

为什么在将值保存到$ alias和$ mail后,我的脚本不会重定向。

2 个答案:

答案 0 :(得分:1)

您可以使用header("location:http://www.example.de/form2.php");代替javascript。

如果您想使用javascript,请使用window.location代替window.document.location.href

echo "<script type="text/javascript">window.location = 'http://www.example.de/form2.php';</script>";

答案 1 :(得分:0)

在网站上快速搜索,我找到了并且可以帮到你。

//与HTTP重定向类似的行为:

  

window.location.replace( “http://stackoverflow.com”);

//与点击链接类似的行为:

  

window.location.href =“http://stackoverflow.com”;

How to redirect to another webpage in JavaScript/jQuery?