This is more a question on php theory, and wondering how this scenario would be done..
So I have a gallery which has pagination, all done through reloading the page through $_GET.
I want the ability for a user to change the amount of images are displayed on a page (basically my LIMIT). I have this working, however when they go to the next page, the php reloads, and the pagecount gets reset back to default.
Is there a way to store this variable through $_POST to another page when they choose the page count, and then every time the page re-loads, it will grab that variable, so it is not re-set?
Excuse my noobiness. Hopefully this makes sense
I believe you are looking for session variables
<?php
session_start();
$_SESSION['views'] = 1; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data
?>
http://www.tizag.com/phpT/phpsessions.php
What you want are PHP sessions, which
...consists of a way to preserve certain data across subsequent accesses
See the PHP docs for more information.
Whenever you make a request to the server, pass along all the variables that you need. So if you're changing the limit by submitting a form, pass along the page number as a hidden form field:
<select name="limit">...</select>
<input type="hidden" name="pageNum" value="<?= htmlspecialchars($pageNum) ?>" />
Or if you're changing the limit with a link, pass along the page number as another URL argument:
Limit 10
Then you can read it on the server using $_POST["pageNum"] or $_GET["pageNum"].
I don't recommend storing things like this in the session. If you do, you'll prevent people from having multiple windows open to different pages. It's best to pass everything in the request (i.e. the form or link).
Related
I have a page where I need to get the referring URL so I can redirect them back after they compete a form. My problem is, the form has several drop menus and each time a selection is made (prior to submitting the form), the referring URL keeps changing to this page.
I have....
$_SERVER['HTTP_REFERER'];
And it works only on landing of course.
Now the user selects an option from drop menu and the page refreshes with another drop menu to continue choosing options. Once done, the form I submit to needs to read the original referring URL, not the form page URL.
I need to be able to put the original in a hidden field and submit along with the form options. Thanks for any help. I apologize if this isn't clear, it was difficult to explain.
If you insist on doing it with forms and hidden inputs, that's easy.
<input type="hidden" name="referrer" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" />
You could also do it with sessions, though, supposing you use session_start().
session_start();// At the very top of your page. Literally THE TOP.
// Set our session variable only if it is not currently set.
if (!isset($_SESSION['referrer'])) {
$_SESSION['referrer'] = $_SERVER['HTTP_REFERER'];
}
For more on sessions, see this. Sessions were meant for just this sort of thing, taking variables across pages and preserving state.
if($_SESSION['GOBACK']=='')
{
$_SESSION['GOBACK'] = $_SERVER['HTTP_REFERER'];
}
This seems to work. Thanks everyone.
When they go to the page save the referrer in a session variable.
After it refreshes and does the page logic, redirect to the session variable you saved.
Store the referer in the first request into a session variable or a cookie. $_SESSION or $_COOKIE super globals can help achieve this.
I recommend sessions as the user won't be able to hijack the value locally.
How to hide this html line, that will not appear for the user in inspect element or view source.
<input type="hidden" name="kda" value="<?php echo $code;?>">
how can I do this?
You can't do that - everything you send to the browser can eventually be read and stored somehow.
What you can do instead, however, is using a session to store this information. Then, only a session identifier will be sent to the browser (and back to the server) while your sensitive information can stay on the server.
It's impossible to hide HTML from view source. Any HTML gets sent to the client and can be viewed in view source. Try storing it in $_SESSION, a PHP superglobal. It gives the user a cookie that tells PHP where to look to find that user's information in $_SESSION.
$_SESSION is an array. That means you can store $_SESSION['pies_bought'] = 7 and $_SESSION['cakes_bought'] = 3.
http://www.php.net/manual/en/session.examples.basic.php
If you "hide" it, it won't work.
You could surround in PHP comment so it's stripped when the server renders the page, but I think you are asking to hide the value of this hidden form field, and that you can not do as you are suggesting.
You could post the "viewable" form fields to another php script that then adds this "confidential" key, and then submits the form wherever it's going.
You could, upon submission of form, call an ajax request to get the value and submit all at once.
Many ways to skin a cat.
Set this flag as a PHP variable instead of actually including the hidden input field in the form.
You may use encryption if you still want to use it as a query parameter in your form otherwise session is your best bet.
<input type="hidden" name="kda" value="<?php echo some_php_crypt_function($code);?>">
When you receive kda on the server,just decrypt it and get the value.
This link http://www.php.net/manual/en/mcrypt.examples.php has examples of how to use encryption/decryption in php.
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.
?>
I have a php page that has a form that asks for an e-mail. When you press the send button, it gets to another php page, which gets the form data and does its stuff. I need to then be able to go back to the old page (the one that contained the form) and give it some data so that it will be able to change itself and say "You've sent your e-mail successfully, and will not display the form.
How do I do it?
Sessions probably
http://us2.php.net/manual/en/book.session.php
You can either use sessions or cookies, to not depend on the URL cookies have always to be enabled.
Check the PHP Manual (Sessions and Cookies).
Options:
1) Set a cookie (or use a session variable, which is kind of the same thing)
2) Use a separate thank-you page. After you've processed the form, redirect to http://www.mysite.com/thankyou
3) Process the form on the same page as itself. If your form is at http://www.mysite.com/myform, then at the top of that page have a little
if ($_POST)
// process form
// display thank you
else
// display form
Good luck!
If the user is just seeing data that they've entered anyway, you can just use hidden form fields:
<input type="hidden" id="lang" name="lang" value="en" />
That way you can continue to POST new forms and pass the data down the lane. That's the easiest thing to do without having to write a single extra line of PHP code.
You could also store each section in a database and save each section as-added. That would give you the added benefit of having partial data in the case of a browser crash or whatever, depending on how many parts your form is. You could then pass just an ID to the DB table row and retrieve the data for display.
Is it possible to use a variable from one page in a piece of code on another? e.g. submit a form on one page and on the second page use a PHP script to add the data from the form to a MySQL table
Thanks for all the help
Your best bet would be to use PHP's session functions. That way if the user navigates away from your page and then comes back, the session variables will still be there (provided the session hasn't expired). You can get more information here -- PHP Sessions. Basically all you have to do is call
session_start();
at the top of each php page (before anything is outputted to the browser) where you want to have access to the session variables. You can then set/retrieve a variable using
// set
$_SESSION['varname'] = "something";
// retrieve
$somevar = $_SESSION['varname'];
This is what the GET and POST 'super' globals are for.
http://www.tizag.com/phpT/postget.php
The script that generates the form does not have to be the script the processes the form. all it takes is for the FORM tag to point to the correct script. The only caveat is that whatever page you submit the form data to also has to then generate some suitable output (unless you do a AJAX POST).
If you're instead asking if two page executions can handle the same POSTed data, well that is quite a different question. At a simplistic level, you can have the first include() the second so it has access to all the same data. A bit more advanced is to re-create the original POST and synthesize a POST. This will probably require some JavaScript assistance. Another approach is to use PHP's session feature and put the value aside so a later script has access to it. This relies on the user then following the correct link, however!
Use session to access your variable to the second/other page.
Example:
index.php
<?php
session_start();
$_SESSION['yourData'] = 'yourData';
?>
=========================================================================
second_page.php
<?php
echo $_SESSION['yourData'];
?>
You may try this one. Visit this link for more details. Or PHP's official website.