2012-03-03 103 views
1

我有这个代码块的一些奇怪的问题,这是一个小学校项目,目前进展顺利,但底部的表单没有返回任何发布数据。 var_dump($ _ POST);从不显示从此特定表单提交的任何发布数据。POST数据不发送

我只是无法理解为什么,一直在挣扎这么久。

下面是代码,我不明白在这个页面上的格式,每行添加4个缩进似乎有点乏味。

<?php session_start(); 
require_once'user.class.php'; 
require_once 'posts.class.php'; 
require_once'comments.class.php'; 
$posts = new Posts($user->getID(), $db); 
$comment = new Comments($user->getID(), $db); 

include_once'includes/header.php'; 
include_once'includes/nav.php'; 
?> 

<h2><br></h2> 
<?php 
include_once'includes/intro.php'; 

// CONTENT 
$post = $posts->showPost($_GET['id']); 
$title = $post['title']; 
$content = $post['content']; 
$created = $post['timestamp']; 

echo "<section> <article class='blogPost'> <header> " . " <h2>$title</h2> " . "<p> Posted on $created <a href ='#comments'> X comments</a></p></header>" . "<p>$content</p>" . "</article> </section>"; 

var_dump($_POST); 

if (isset($_POST['submit_comment']) && isset($_POST['id_comment'])) { 
    echo "Kom seg inn i ifen"; 
    $pid = $_GET['id']; 
    $comment->newComment ($db, $user->getID(), $pid, $_POST['id_comment']); 
    header ("location: showPost.php?id=$pid"); 
    exit(); 
} 
?> 

<form name='commentform' action='showPost.php?id=<?php echo $_GET['id'];?>' method='POST'> 
<h3>Post a comment</h3> 
<p> 
<label for='id_comment'>Comment</label> 
<textarea name='id_comment' id='id_comment' required></textarea> 
</p> 
<p><input style='width: 100%;' type='submit' name='submit_comment' value='Legg til kommentar' /></p> 
</form> 

<?php 
include_once'includes/asidefooter.php'; 
?> 

感谢您的帮助!

+1

存在这样会自动缩进代码段4位编辑了“代码示例”按钮。 – 2012-03-03 16:41:10

回答

1

看起来好像post是数据存在,用户被重定向。您只会在提交表单时看到发布数据,重定向会导致另一个页面加载,数据将不再存在。

你能看到的数据后,如果您注释掉重定向,以:

//header ("location: showPost.php?id=$pid"); 
//exit(); 
+0

事实上,当我删除这两条线时,它确实有效,不知道为什么我有他们。但我从未触发过的if语句中有回声。所以即时通讯仍然不知道为什么header()运行。谢谢! – Snowflow 2012-03-03 17:05:06

0

有些东西导致重定向,它将请求转变为GET。如果你想提交给index.php,那么不要忘记结尾的斜杠。

+0

我注释掉了“header(”location:showPost.php?id = $ pid“);” 但是为什么会因为它在if? – Snowflow 2012-03-03 16:56:07