2016-11-12 70 views
0

我有一个问题要求与GET/POST相关。通过GET获取参数并通过POST提交(PHP)

我想做一个简单的博客文章和他们的意见。

从每个职位我都主页上的话,我想在一个新的页面,使保存post's指数有评论的控制添加注释的形式。

我通过GET在新页面中获取此索引值,但是当我通过POST提交表单时,我失去了索引的引用。

我读这是不可能同时使用这两种方法,我想知道我可以保持一个参数从主网页,并将其与价值的新形式,其余存储。

非常感谢,

BR

http://localhost/simple_blog_new_comment.php?postIndex=xx 

<form action='simple_blog_new_comment.php' method='POST'> 
 
\t \t Commentary:<br> 
 
\t \t <textarea onfocus='clearContent(this)' cols='30' rows='5' name="txt_comment">Enter the text here...</textarea><br> 
 
\t \t Author: <input type='text' name='txt_comment_author'><br> 
 
    \t \t <input type='submit' name='btn_comment_submit'><br><br> 
 
    \t </form>

+0

这里索引引用究竟是什么? –

+3

你想添加一个隐藏的输入字段到评论表单,其中包含帖子ID('”>' )。 – arkascha

回答

0

我发现了这个问题的解决方案,如果有人遇到同样的问题,我想分享一下。

最后,我使用$ _SESSION超全局变量修复了我的“Posts”和“Comments”数据库修复了变量引用问题。

它的工作原理是这样的:

session_start(); // This allows the use of $_SESSION superglobal var 

$_SESSION['index'] = $_GET['postIndex']; // Save the variable into $_SESSION 

有了这个超级全局变量,你可以保证指数变量作为一个cookie,只要你保持会话打开使用它。

更多相关资讯:http://php.net/manual/es/reserved.variables.session.php

再次感谢! :D

0

我不知道如果我明白你的问题。我想你想通过URL获取参数并通过表单发送。我认为你应该做下一个。

<?php 
$index=$_REQUEST["Index"]; 
?> 
<form action='simple_blog_new_comment.php' method='POST'> 
     Commentary:<br> 
     <textarea onfocus='clearContent(this)' cols='30' rows='5' name="txt_comment">Enter the text here...</textarea><br> 
     Author: <input type='text' name='txt_comment_author'><br> 
     <?php echo "<input type=hidden name=num_index value=" . $index . ">"; ?> 
     <input type='submit' name='btn_comment_submit'><br><br> 
</form> 

在simple_blog_new_comment.php中,如果你想获得num_index的值,你将需要这个。

<?php 
$kk=$_REQUEST["num_index"]; 
echo $kk; 
?> 

我认为你正在寻找类似的东西。我希望它会有用。

+0

嗨Perancker, 感谢您的答复。你明白了,这是我为这个问题提出的解决方案之一,但我仍然遇到GET参考的问题。我将解释它: 我试图获得并保持变量槽GET(URL为http://localhost/simple_blog_new_comment.php postIndex = XX)成HTML变种像你解释隐藏的例子或PHP变种,如“$索引“,但这里出现了问题,当我通过POST方法提交表单时,两个引用(隐藏的var和$ index)都会因为指向URL而丢失。 我在提交

kempes007

+0

时发生URL更改我试图在表单的action参数中包含变量,但仍然丢失。 我希望这可以清理一点点我的混乱,找到一起解决方案。 无论如何非常感谢:D – kempes007