2016-11-12 114 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