Pass variables in url using POST - php

Assuming I have a two pages (page1 and page2), there are variable values I want to send to page2 without using $_GET function but instead $_POST through a url.
Example on page1
<?php
$name="Dman";
$id="12";
$level="122";
?>
Goto Page2
On page 2, I want to receive the variable values using POST.
name:dman
id: 12
level:122
UPDATE
Please what I am trying to achieve, is to prevent showing items being transmitted from one page to the another, when url is been used. I read some blogs and they are like, using POST workaround can help achieve this.On the contrary if there is a way to avoid using "?id=$id&name=$name" on a url but still receive your variables on the next page, I would be grateful to know.
Thanks for helping

You need to use the GET method if you want to pass parameters in the URL. POST is a different method and it's not possible to represent or reference POST data in the URL.
Some reading on the definition and difference between post and get: http://www.w3schools.com/tags/ref_httpmethods.asp

The GET method pass the parameters INTO the url, for example :
http://your/path?yourvariable=value
The POST method hide the parameters from the url.
The simplest way to pass parameter with POST method is to make a HTML form. In your Php sript, you can access to this value with the $_POST superglobal array.

Related

forwarding url parameters to the next url

Using PHP (for the first time in my life) and working in a CMS environment without access to the back-end PHP pages or code, I created a form on one page that places four parameters into the url of the next page to which the form send its data.
Here is the form page:
http://CMSDetroit.org/480
Here is the url the form generates:
http://www.chambermusicdetroit.org/422?School=formdata&Grade=formdata&Teacherformdata=&Handle=formdata
I need to collect the parameter information from the url and pass it on to the url of the next page after this one, either by putting a command into every link on the page (422), or through some other more efficient method.
I've tried all sorts of things and keep coming up dry...help!?!
For PHP see $_SERVER and more specifically QUERY_STRING
Then with that I'm not sure how you are invoking the next page? maybe with header?
header('Location: http://www.example.com/?' . $_SERVER['QUERY_STRING');
exit;
Point being it is just one large string. If you don't know already you use $_GET and key to extract the individual values or if you want you can use explode on the QUERY STRING

Getting GET/POST result from third PHP page

I have the following pages:
webservice.php - it has a webservice used to handle all the possible GET and POST variables sent to it, returning a string as result.
form_page.php - the page with a form whose action redirects to table_result.php.
table_result.php - on this page, there is a table which should be filled up with the results from webservice.php.
My question is: if the GET/POST variables' values are only available on table_result.php (after the form data has been submitted), how can I send them to webservice.php and get the result to fill up the table without leaving table_result.php? IT is important to note that on webservice.php there are a bunch of if (isset($_GET["var_name"])) to formulate a database query and return the result on a string, as said before.
Thanks in advance.
First, instead of the $_GET, use $_REQUEST, as this would make it available via both POST and GET. Then you can just use the search here and find your answer

Pagination together with a html form

On my website I use a pagination (similar to that one on the bottom of this page) for MySQL output. For the change of the current page I use GET method (variable page) and it works well.
However, on my page I have also a form, using method POST, which acts as a filter for the MySQL output. This rises a problem because, when I change the form settings an submit them (POST), the page in the address line (GET) remains the same. This is problem in some cases when the filtered output has less pages than that one currently set.
Is it somehow possible to set the page variable to 0 always when the form is submitted?
Particularly, I did it using $_SERVER['REQUEST_METHOD'] == 'POST'. However, this changes just the variable in the code. Not at the address line.
On the other hand I want to keep the POST variable when I change the page of the output.
Thanks in advance.
There is a logical collision in your setup:
Unlike GET method, POST method doesn't keep variables in the address bar. But for some reason you are using POST method.
So, the solution is quite simple - use GET method for the filtering.
To create pagination links use http_build_query() out of $_GET array
In fact it's better to send filter criteria in the address (GET method). It will solve your problem with pagination and you (and your users) will have a direct link to search results.
In your PHP code, you want to redirect to the URL with the page GET parameter removed if you are SUBMITTING (by pressing the "Filter" button, or whatever it's called). So it will start at page 0.
You would have to rewrite the URL yourself (or using a plugin).
Typo corrected, I meant page 0, not 1 :P

POST variables disappearing when a link is clicked

I just setup some pagination for a search, and the search uses POST variables to define what to search for etc. In my URL I can set the pagination offset like this search/OFFSET, and my links in the pagination link there correctly. However, when I click a link all POST variables vanish even if I explicitly set them so I can use them in the next script. I'm using codeigniter and I have GET turned off and really don't want to store these 5-6 values in a session since then it will get all clumsy.
Does clicking a link fully reload the page and delete POST variables?
Thanks
Yes, clicking a link creates a GET request so wouldn't keep any of the POST data. Although it's technically possible to do so with javascript, that's a bad idea.
This is an entirely appropriate use of GET, please read this fuller explanation.
Yes, clicking a link removes all the POST variables.
Do you have access to change your php page that receives the request? You might want to adjust your variables there to accept either GET or POST:
$defaultvalue='';//change this to '' or NULL or whatever you want
$searchQuery = (isset($_POST['s']) ? $_POST['s'] : (isset($_GET['s'])?$_GET['s']: $defaultvalue));
Then your php page will be better equipped to handle either GET or POST
POST data will only be present during the original request (i.e. it does not persist between requests). If you want data to persist, use sessions. However, it is common practice to use GET for search queries and pages.
You could use an incredibly ugly workaround and set a form full of hidden fields to submit when you click a link. I really wouldn't recommended it though.
You should be able to create a form that submits some set of post variables to the action parameter with the get variables. So the form should submit a post request to http://www.somedomain.com/FormSubmit.php?pag=1&sort=asc This would submit the post values of that form along with the get values of the string. If you can change your link to a form button, you should be good to go.

PHP: Pass non-form variables between pages?

I have a page. The user submits the page and sends it to a PHP results page. It works fine. Now, I want the results page to link to another page, and for the elements on that page to depend on what was on the results page. I know how to pass form variables to another page, but I don't know anything about passing non-form variables.
From my searching on the web, my best guess is that I should be passing by URL. Is this correct? If so, a possible problem: the page I want the results page to pass to will have a form, and the user will go to yet another results page by clicking submit (the form data will be sent by POST). Can I send the non-form data (the old results page variable) along with the form data, if the user is going to the other page using POST?
I strongly suggest using sessions. It's not that hard to learn, php makes it VERY easy using http://php.net/session_start and the $_SESSION variable.
Advantage is that you will not have to submit a form on every click, and no information will be displayed in plain text in the URL.
There are several options. However, the easiest may be to simply pass the data on using hidden input fields.
Other options would be using the session, storing to a database between forms, or some combination therein.
If you are going to use POST to go to the next page the most simple option is to include the data you want to send along using an input type="hidden" element in your form.
You might consider using a session to pass the data along.
You can embed the non-form data into the second form as hidden fields. To make sure that it is also passed in the link, you can just add it to the URL as a query string, like:
http://..../blah.php?var1=val1&var2=val2
as long as the data you're passing can fit into a URL. Be sure to urlencode() anything you're appending to the URL.
<?php
start_session();
$_SESSION['Foo'] = 'Bar' // Add stuff here.
?>

Categories