我正在尝试附加一个数字“帖子ID”值到评论,以便它们可以从数据库中检索并显示在适当的位置。如何在html表单中建立此数值,并将其发送到脚本,然后将其插入到数据库中?我假设我需要使用GET或POST,但我不知道如何使用它们来发送除用户输入的文本之外的任何内容。
这是我用来发送"name“和"comment”输入的表单:
<div class="comments">
<form action="foxpost.php" method="post">
<label for="name">Name</label><br>
<input id="name" name="name" type="text" /><br>
<label for="message">Comment</label><br>
<textarea class="message" id="message" name="message"></textarea><br><br>
<input type="Submit" value="Post Comment" />
</form>
</div>发布于 2010-08-07 06:07:46
既然你用PHP标记了这个问题,我猜这就是你在后端使用的语言。我做的另一个假设是,你实际上是用postID格式化你的请求查询字符串,比如"http://example.com/posts.php?postID=1212",注意查询字符串中的postID,你只需传递它,就像这样:
<div class="comments">
<form action="foxpost.php?postID=<%= $_GET['postID'] %>" method="post">
<label for="name">Name</label><br>
<input id="name" name="name" type="text" /><br>
<label for="message">Comment</label><br>
<textarea class="message" id="message" name="message"></textarea><br><br>
<input type="Submit" value="Post Comment" />
</form>
</div>使用
<%= $_GET['postID'] %>将简单地将查询字符串中的postID回显到超文本标记语言中,或者您可以将其赋给一个变量。
发布于 2010-08-07 05:39:16
将id放在表单的隐藏字段中:
<input type="hidden" name="post_id" value="id_goes_here" />发布于 2010-08-07 05:48:03
我建议不要将ID作为表单的一部分,而只使用数据库的AUTO_INCREMENT功能。
https://stackoverflow.com/questions/3427977
复制相似问题