Sergio Campos
Sergio Campos

Reputation: 41

Get parameter through GET and submit via POST (PHP)

I have got an issue to ask related with GET/POST.

I am trying to make a simple blog with posts and comments of them.

From each post I have got on main page, I would like to add a comment form in a new page that enables to save the post´s index to have a control of the commentaries.

I get this index value through GET in the new page but when I submit the form via POST I lose the reference to the index.

I read that is not possible to use both methods at the same time and I would like to know how can I keep a parameter from the main page and store it with the rest of the values in the new form.

Thanks a lot,

BR

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

<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> 
  		<input type='submit' name='btn_comment_submit'><br><br>
  	</form>

Upvotes: 1

Views: 89

Answers (2)

Sergio Campos
Sergio Campos

Reputation: 41

I found a solution for this problem I would like to share in case someone have the same trouble.

Finally I get working my "Posts" and "Comments" databases fixing the variable reference problem using $_SESSION superglobal variable.

It works like this:

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

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

With this superglobal variable you can keep the index variable as a cookie for using it as long as you keep the session opened.

More related info here: http://php.net/manual/es/reserved.variables.session.php

Thanks again! :D

Upvotes: 1

vsergi
vsergi

Reputation: 785

I am not sure if I understood you question. I suppose you want to get the parameter by URL and send it through a form. I think you should do the next.

<?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>

In the simple_blog_new_comment.php you will need this if you want to get the value of num_index.

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

I think you are looking for something similar. I hope it would be useful.

Upvotes: 0

Related Questions