GET and POST on the same page? - php

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 />Reply';
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

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

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.

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.

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.

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.

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.

Related

Why doesnt my <form> work with REQUEST but works with POST

Hello I have the following form that collects data entered and later I output it. It works just fine when I use POST but when I use REQUEST like the teacher said to do, the echo $word comes back empty. Any ideas guys? please?
<Form name ="form1" Method ="REQUEST" Action ="">
<Input Type = "text" Value ="<?php echo $word ?>" Name ="word">
<Input Type = "Submit" Name = "Submit1" Value = "Submit">
<?php
if (isset($_POST['Submit1'])) {
$word = $_POST['word'];
$book = $_POST['book'];
}
?>
There is no method called REQUEST on a Form. It should be either GET or POST
Maybe your teacher is confused with the $_REQUEST in PHP.
I think you are looking for GET, not REQUEST.
GET will include the contents of the form submission in the URL itself, so it's suitable for things that should be able to be bookmarked, like search form submissions.
Here's more: http://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post
Not sure why your teacher asked you this, but "REQUEST" is not a standard HTTP method so I don't think there's any shortcut in PHP to retrieve the data. I found that even using PATCH sometime causes problems.
What you could try is to read the raw data directly using:
file_get_contents("php://input")
There is NO method named REQUEST. You can use only two methods : POST and GET.
If you are using POST as method, you can get the values using only POST OR REQUEST.
If you are using GET as method, you can get the values using only GET OR REQUEST.
For more information please refer to this page: http://www.w3schools.com/tags/ref_httpmethods.asp

Using hidden "form" to pass variables

Just learning about passing variables from page to page in php, and trying to find the best way to do so for me, as I have to pass ~10 variables between 5 pages. On the first page, does it make sense to have a form:
<form method="post">
<input type="hidden" name="test" value="<?php $test ?>" />
</form>
Then on the next page could I receive this variable using POST? I would not like to have an ACTUAL form, just use it as a storage area for my variables. Also, what do I use for action= if the second page is called second.php.
Any help is appreciated, thanks
Short Answer: Forms only work when submitted. You probably want to use sessions.
Longer Answer:
It does not make sense to have a form without user input. That's what it exists for.
the action= attribute on a form reflects where the form would be submitted. If the processing page is second.php then the action= attribute should point there.
Sessions are not the only possibility. PHP can also set cookies, and if the server doesn't care about the data (only being used as a medium), you can use HTML5's localStorage.
Really, if you need to be passing 10 variables through all five pages, you're probably better off using sessions. You can store all of them as part of the $_SESSION variable and access them from any page as long as the session is kept alive.
You can't use $_POST variables to store data in a user's session.
You should use:
Sessions
Cookies
HTML5 storage
It depends on what you want to do, you have various options:
1. Using a form and post as you outlined. In this case, on page 1 your action="second.php"
2. Passing the data via URL using GET
3. Sessions, as stated by the previous two posts
4. Cookies
If I understand your question correctly you could try the following to POST your values without having the form appear on the page or actually be on the page at all. You need to have the jQuery library referenced in order to use this code.
function hiddenPost(param1) {
$('<form />')
.hide()
.attr({ method: "post" })
.attr({ action: "http://my-URL-here.com/SomePage.php" })
.append($('<input />')
.attr("type", "hidden")
.attr({ "name": "post_data" })
.val(param1)
)
.append('<input type="submit" />')
.appendTo($("body"))
.submit();
}
You can retrieve the POST values on the page you POST to in the same manner as if it was a regular POST with a form tag.
For PHP:
var postData = $_POST["post_data"];
Hope that helps.

why all variable names and values are not displayed When using method="post" in HTML forms

i m getting confuse taking to that question why all variable names and values not show in url when we use method "post" in HTML forms.
i hope my question is clear.
That is because POST requests include variables to message body and not URL. See this: http://www.cs.tut.fi/~jkorpela/forms/methods.html (Methods GET and POST in HTML forms - what's the difference? )
Only GET methods show the variable names and values in the URL, not POST methods
When you are using POST method than all the data like your variable name,variable value, cookie is send to server in Request body,
so you cant see that parameter while using POST.
GET and POST methods are two different way to exchange data between server and client.
GET - retrieve data from URL (Eg: http://domain.com/index.php?var1=val1&var2=val2)
echo $_GET['var1']; (will return `val1`) and so on
POST - collect values in a HTML form with method="post", data send with curl with the same method, and so on.
<input name="username" />
echo $_POST['username'];
Use this like example, the actual content is more complex.

Submitting GET data with no input field?

I've never really thought about this, but it helps with some security of something I'm currently working on. Is it possible to submit GET data without an actual input field, and instead just getting it from the URL?
If so, how would I go about doing this? It kind of makes sense that it should be possible, but at the same time it makes no sense at all.
Perhaps I've been awake too long and need some rest. But I'd like to finish this project a bit more first, so any help you can offer would be appreciated. Thanks
Yes. If you add some query-string to yourl url, you can obtain that in php using $_GET without form submitting.
Going to this URL adress http://yoururl/test.php?foo=bar cause echoing foo (if there will be no foo query string, you'll get warning).
# test.php
echo $_GET['foo'] # => bar
Is this what you mean?
Link
// page.php
echo $_GET['type']; // foobar
This is what I understand of your question:
You have a <form method="get" action="foo.php">-like tag on your page
You have a series of <input type="text" name="bar"/> in your page
You want to pass additional GET parameters that are not based on an input from the form
If so, it is possible, but I hardly see how it could help with security. Input from a client cannot be trusted, so even if you hardcode the GET value, you have to check it serverside against SQL injection, HTML injection/XSS, and whatnot.
You have two ways:
Use a hidden input: <input type="hidden" name="myHiddenGetValue" value="foobar"/>
Add the GET parameter to the form action: <form method="get" action="foo.php?myHardcodedGetValue=foobar">
If what you meant is that you want to have a GET request without a form, you just need to pass all the GET parameters to the href of a link:
Click here!
Yes it's possible. Just append the GET data to the link.
For example:
<a href="main.htm?testGet=1&pageNo=54>Test</a>
You can also use Javascript to build the url.
If you happen to be using jQuery and want to build the GET data dynamically you can do this:
var getParams = { testGet:1, pageNo:54 };
$(".myLink").attr("href", url + "?" + $.param(getParams));

What happens if you go to a GET style url with a POST request?

Let's say I have a page called display.php and the user is viewing display.php?page=3. I want to allow the user to do an action like voting via a POST request and then bring them back to the page they were on. So, If I do a POST request to display.php?page=3 would the page information also be available to the script?
The simple answer is 'yes'. You can use a GET-style URL as the submission URL for a POST form. PHP will have both the POST and GET information available to it in the usual ways when the form is submitted.
This is not to say that you should do this, but it will work.
In PHP, you can get request variables from the special global arrays:
$_GET['page'] (for GET requests)
$_POST['page'] (for POST requests)
$_REQUEST['page'] (for either)
It sounds like you are looking for "Redirect after Post", I would suggest separating display.php and vote.php into separate files. Vote looks something like this:
<?php
//vote.php
$page_number = (int)$_REQUEST['page'];
vote_for_page($page_number); //your voting logic
header('Location: display.php?page=' . $page_number); //return to display.php
Note that blindly accepting unsanitized form data can be hazardous to your app.
Edit: Some folks consider it bad form to use $_REQUEST to handle both cases. The hazard is that you may want to signal an error if you receive a GET when you expect a POST. Typically GET is reserved for viewing and POST is reserved for making changes (create/update/delete operations). Whether this is really a problem depends on your application.
Yes, the GET array is always filled with the URL parameters regardless of the request method. You can try it with a simple page like this:
<form action="test.php?a=b" method="post">
<input name="a"/>
<input type="submit"/>
</form>
<pre>
POST:
<?php print_r($_POST); ?>
GET:
<?php print_r($_GET); ?>
</pre>

Categories