GET and POST on the same page?

2019-04-29 03:02发布

问题:

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

回答1:

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'].



回答2:

Yes you can. The method of a form (in a html page) can be POST and the action URL can contain "GET" arguments being something like process.php?vid=1001 so to say. So in process.php you'll have vid as $_GET and the rest of data from the form as $_POST.



回答3:

Sure you can, just set the action of the form to postReply.php?verseid=id_of_the_verse this way when an user submits a reply, in the POST array will be the reply related data and in the GET the id of the verse.



回答4:

Yes, it is possible to mix both GET and POST values with one request. The problem you have is probably that you pass the GET value to reply.php, which then passes POST values to postReply.php. So, unless you tell reply.php to send that GET value as well, it will get lost.

You can do this by either specifying the GET value in the action parameter of the form tag, or you could even switch to a POST value with that, by adding a <input type="hidden" name="verseid" value="<?php echo $verseid; ?>" /> to the form.



回答5:

Although it may seem counter-intuitive an HTTP request can come in with both Form and QueryString data. Like robertbasic says you can access them both via there respective arrays.



回答6:

Using a form with a hidden input (<input type="hidden" name="verseid" value="..." />) is probably the cleanest way of doing things.

PHP also defines the $_REQUEST global array in addition to $_GET and $_POST. In general you should use either $_GET or $_POST but in this case where verseid is being passed for both methods, it might be more convenient to use $_REQUEST['verseid']. This way you need not care about the HTTP method being used on your script.