如何使用表单操作检索网址数据?

时间:2014-03-26 19:55:11

标签: php html

我想检索页面的id,但页面是动态的,具体取决于id

例如filepath/Post.php?id=5

数据是通过表单操作发送的,我希望获取其他页面ID,但我不知道该怎么做。

echo "<form action='../PHP/Comment.php' method='post'>" . "<input class='comment' type='text' name='comment' placeholder='Add a comment'>" . "</form>"  

//then what the action does
$id = isset($_GET['id']); //trying to get the page id but it doesn't
$comment = $_POST['comment'];

//insertion into the table in the database.
mysql_query("INSERT INTO comments (id, comment) VALUES ('$id', '$comment')") 

2 个答案:

答案 0 :(得分:0)

有更好的方法可以做到这一点。你尝试过使用专用的$ _SESSION变量吗?在PHP中,我这样做: -

<?php $_SESSION['PAGE_ID'] = 10;?>

然后在表单上检索它

<?php echo $_SESSION['PAGE_ID']?>

答案 1 :(得分:0)

试试这个

echo "<form action='../PHP/Comment.php' method='post'>
<input class='comment' type='text' name='comment' placeholder='Add a comment'>
<input type="hidden" name="id" value="<?php echo  $_GET['id']; ?>" />
<input type="submit" value="Submit" />
</form>" 

然后进行表格处理

$id = isset($_POST['id']);
$comment = $_POST['comment'];
mysql_query("INSERT INTO comments (id, comment) VALUES ($id, '$comment')");