Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET and POST on the same page?

EDIT: Answer found! Thank you very much people, a lot of answers worked, I chose the hidden field answer as it was easiest :D

I am creating a commenting script and I came across a problem. I am having to use $_POST and $_GET on the same page, which I don't think makes sense.

I am very new to php and am training myself.

I have a page named viewVerses.php - this has a lists of verses. When someone follows the reply link,

echo '<br /><a href="reply.php?verseid='.$verseid.'">Reply</a>';

I'm passing the verseid (commenting on bible verses) into the reply.php, so that a query may be made with that verseid. (This is so that the user can still see the verse he/she is commenting on).

Now reply.php has the form in it for posting a reply. The form goes to postReply.php

This is in postReply.php

$title = $_POST['title'];
$body = $_POST['body'];
$verseid = $_GET[verseid];

Can I get the verseid from the url and the POST the values from the form in the same page?

If not, is there a way I can do this better? Remember, I am new at php and probably won't implement a solution that is super hard. I have to get it for my to put it in my site.

I hope this is clear

like image 735
Phil Avatar asked Jun 13 '10 19:06

Phil


People also ask

Can you use POST and get at the same time?

With Ajax you can send data to a PHP script via GET and POST in the same request. The process is similar to sending data through the POST, it's actually the same, being only applied a "trick".

Is get the same as POST?

POST. HTTP POST requests supply additional data from the client (browser) to the server in the message body. In contrast, GET requests include all required data in the URL.

Can we use GET and POST interchangeably in PHP?

While GET should only be responsible for retrieving data from the server, POST can be used to send data to the server and thus change the server state. While this desired behavior of the HTTP methods is well-defined in RFCs, some applications choose to treat GET and POST requests interchangeably.

How do I choose between POST and get?

GET method passes request parameter in URL String while POST method passes request parameter in request body. GET request can only pass limited amount of data while POST method can pass large amount of data to server. GET request can be bookmarked and cached unlike POST requests.


1 Answers

I would add a hidden input to the comment form:

<input type="hidden" name="verseid" value="
<?php echo $_GET['verseid']; ?>
" />

That way, in postReply.php, you can access it using $_POST['verseid'].

like image 101
zildjohn01 Avatar answered Oct 16 '22 02:10

zildjohn01