2015-02-06 99 views
0

在我的index.php页面上获得了一个简单的表单,当我的post.php页面上处理了提交时。这个网页有一个重定向循环 - php

的index.php

<form id="form" action="post.php" method="post"> 
    Name:<br> 
    <input type="text" name="name"/> <br> 
    Gender:<br> 
    <input type="text" name="gender"/> <br> 
    Age:<br> 
    <input type="number" name="age" min="1" max="99"/> <br> 

    <input id="submit" type="submit" value="Input"> 
    </form> 

<div class="data"> 
    <?php 
     include ('post.php'); 
     foreach ($result as $row) { 
     echo $row['name'] . " "; 
     echo $row['gender'] . " " ; 
     echo $row['age'] . "<br>" . " "; 
     } 
    ?> 
</div> 

post.php中 下面是我的post.php中页面的代码,上方是我的头,但我不断收到同样的错误,当我提交形成。

<?php 

header("Location: index.php"); 
try { 
    $dbh = new PDO('sqlite:mydb.sqlite3'); 
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

    $dbh->exec("CREATE TABLE IF NOT EXISTS test (
      name VARCHAR(30), 
      gender VARCHAR(30), 
      age INTEGER)" 
      ); 

if (!empty($_POST)) { 
    $stmt = $dbh->prepare("INSERT INTO test (name, gender, age) VALUES (:name, :gender, :age)"); 
    $stmt->execute(array(':name' => $_POST['name'], ':gender' => $_POST['gender'],':age' => $_POST['age'])); 

    $title = $_POST['name']; 
    $message = $_POST['gender']; 
    $age = $_POST['age']; 
} 

    $result = $dbh->query('SELECT * FROM test'); 

    $dbh = null; 
} 
    catch(PDOException $e) { 

    echo $e->getMessage(); 
}  
?> 
+4

首先,您需要告诉我们您是如何导致无限重定向的。是否index.php重定向到post.php和post.php重定向到index.php ....因为如果你真的只是移动到post.php当用户点击提交,这不应该发生。 – developerwjk 2015-02-06 21:22:36

+0

哪个网页浏览器? – MonkeyZeus 2015-02-06 21:24:26

+0

只有当$ _POST被设置时才会重定向? – 2015-02-06 21:25:44

回答

3

里面index.phpinclude -ing的post.php文件,立即转发你回到index.php

您需要在post.php有条件转发,否则您需要重新修改您的逻辑。

+0

将我的标题放在我的If语句中时,它看起来工作正常,那是不好的做法? – Dianabolz 2015-02-06 21:35:02

+1

@daniel,在if语句中放置一个头是很好的。在设置标题之前,您只需确保不要输出任何内容到输出。 – developerwjk 2015-02-06 21:36:28

+0

非常感谢。 – Dianabolz 2015-02-06 21:37:46