I can pass values form one page to another but I need to pass value like this,
Page 1:
Page2.php
Page3.php
I need to pass the radio button values in the Page1.php to Page2.php, and also i need to use same session . if the form is redirected to page3, I am unable to get the value of page 1. its online quiz project. I tried session, form post method and few other methods but I am yet to succeed.
I would be very happy if you can help me with the code or some suggestions.
Thanks!
The best practice would be to go with OOP. So, you can do the following:
Create a class with fields to hold all the information you want to pass.
class PageInfo
{
var $pageTitle;
var $currentPage;
var $btnValue;
.....
}
2.Now crate an object of PageInfo class, and assign the values you want to set.
$page = new PageInfo();
$page->pageTitle = "Home";
$page->btnValue = 1;
...
3.Assign this object (holding all the details of your page) to the super-global session variable.
// store session data
$_SESSION['page'] = $page;
4.Now you can access the value stored in the session at the different pages.
$otherPage = $_SESSION['page'];
echo $otherPage->pageTitle;
echo $otherPage->btnValue;
Note: The session_start() function must appear BEFORE you print anyting on the page.
Actually your question is vague, first of all when you are using sessions, you need to be sure you are calling session_start() at the very top of the page, secondly, you can save the form data in your session variable like
if(isset($_POST['YOUR_SUBMIT_BUTTON_NAME_HERE'])) {
$store_temp_val1 = $_POST['whatever']; //Sanitize the value first
$_SESSION['page_one']['first_val'] = $store_temp_val1;
}
Now you can simply retrieve the value stored in the session variable on the second page like
echo $_SESSION['page_one']['first_val']; //Will echo the previous page value
Note: Use session_start() on each page at the very top.
Related
The goal is to persist some data between pages after a refresh when a user selects a db search filter criteria from 6 select dropdown boxes on the page using session_start() and the SESSION variables are set to the 6 select-box values the user selected, as a search button is clicked calling a Laravel controller action, where the SESSION is session_start() being begun and the 6 select-box variables are being set for the SESSION.
The goal is to reset the select boxes from session variables populated when the search button is clicked retaining the select box selections-criteria selected by user so they do not have to reselect the 6 select-boxes again. Those select-box selection values are stored in session variables;
Upon refresh after a successful search is being returned the SESSION variables are read in the masterlayout.blade html section to programmatically to reselect the previous search-filter selection the user had entered as the retrun page is rendered with the masterlayout.blade including the nav.blade.
In the masterlayout.blade (which includes the nav.blade which has the select-boxes) in the Laravel template system is where I READ these SESSION variables upon refresh of a user-search, then the SESSION variables set from the controller called from the button click reset the select boxes to values in the SESSION vars.
But, that masterlayout.blade template is called from other pages as it and search-nav appear on other pages which may be used for a search or not, before or after a user has done a filtered search.
The problem is when other pages are requested, even after a search, the SESSION variables seem to be unavailable, reset to nothing or whatever--- I cannot figure out as I did not destroy the session and they should be available from any page after being set.
But I get the _SESSION variables unavailable error when other pages are requested.
If you know how to fix this let me know.
If you know how to persist the select-box values between pages in a better manner let me know. I don't want to right to a db to do this, but maybe there is a javascript way to retain variables across pages.
Thanks
This is in the controller accessing post data from button click which calls the controller
//colvals
session_start();
$bidded=$_GET["bidded"];
$_SESSION["bidded"] = $bidded;
$state=$_GET["state"];
$_SESSION["state"] = $state;
$city=$_GET["city"];
$_SESSION["city"] = $city;
$biddertype=$_GET["biddertype"];
$_SESSION["biddertype"] = $biddertype;
$job=$_GET["job"];
$_SESSION["job"] = $job;
$subjob=$_GET["subjob"];
$_SESSION["subjob"] = $subjob;
In the search filter navigation page the select box values from previous user search (stored in above code from controller) is set by this:
<script>
document.getElementById("bidded").addEventListener("load", mySelect());
document.getElementById("state").addEventListener("load", mySelect());
document.getElementById("city").addEventListener("load", mySelect());
document.getElementById("biddertype").addEventListener("load", mySelect());
document.getElementById("job").addEventListener("load", mySelect());
document.getElementById("subjob").addEventListener("load", mySelect());
function mySelect() {
document.getElementById("<?php echo $_SESSION['bidded'];?>").selected = "true";
document.getElementById("<?php echo $_SESSION['state'];?>").selected = "true";
document.getElementById("<?php echo $_SESSION['city'];?>").selected = "true";
document.getElementById("<?php echo $_SESSION['biddertype'];?>").selected = "true";
document.getElementById("<?php echo $_SESSION['job'];?>").selected = "true";
document.getElementById("<?php echo $_SESSION['subjob'];?>").selected = "true";
}
</script>
Javascript session and local variables are not of any use in this case because when the page refreshes it reads the default state of the select boxes and overwrites the ones stored prior to the search input submission.
The problem was other pages would use the same template and complain about no _SESSION vars defined, when I did not destroy the session, but I guess only one script can access it, even if those pages have none. It requires a form submit to set the session filter select vals again.
Because this is a unique function it may end up having its own template as worrying about 100 other pages session vars is more of a PITA than a second template. I'll give cookies a shot but did not want to expose query data on user end.
Thanks for the input...
I have three forms - payment.php , payment1.php and paydb.php . payment.php contains the front end form.payment1.php contains the back end of the form of payment.php. whereas we are shifting to paydb.php from payment1.php. Now I'm filling the form by entering member number in payment.php which is retrieved in a variable $member_no in payment1.php .Now I want to get the value of member_no in paydb.php . How to do that ?
After receiving $member_no in payment1.php redirect to paybd.php with a get array
using
header('Location: http://www.example.com/paydb.php?member_no=$member_no');
then receive $_GET['member_no'] number and assign to a variable
example:
$member_no = $_GET['member_no']
The first thing is make sure you are not passing sensitive information where the public can see it. Such as in a URL.
As soon as you get the member's number... store it in a session variable.
You can probably do this when they log in.
session_start();
$_SESSION['member_no'] = $member_no;
OR on the first payment page (assuming 'member_no' is the name of the form element being passed) like this...
$_SESSION['member_no'] = $_POST['member_no'];
Now that session will persist as long as the visitor has their browser open and you don't have to worry about passing it from page to page.
You can use that session on any subsequent page simply by calling it.
<?php echo $_SESSION['member_no'] ?>
Without showing this information to the public.
ALWAYS make sure you place this at the top of any page where you want to use session variables.
if (!isset($_SESSION)) {
session_start();
}
I am trying to make a login form with PHP, and I want to get a value from a form like so:
$name = $_POST["name"];
$email = $_POST["email"];
then, when the user clicks "Submit", I want the new page to have access to these variables. Using the
include 'file1.php';
technically works, but I dont want ALL of 'file1.php', just the variables.
Any Suggestions?
You have to use session. Here is a simple demonstration:
page1.php - Load page one to set the session variable
<?php
// begin the session
session_start();
// set the value of the session variable 'foo'
$_SESSION['foo']='bar';
// echo a little message to say it is done
echo 'Setting value of foo';
?>
page2.php - Load page two and it will have access to the session variable you set in page1.php
<?php
// begin our session
session_start();
// echo the session variable
echo 'The value of foo is '.$_SESSION['foo'];
?>
This is exactly what sessions are for.
Sessions are a simple way to store data for individual users against a unique session ID. This can be used to persist state information between page requests. Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data.
I'm trying to send the value of this php get_field variable from a wordpress Archive page.
get_field('supplier_email_address', 'product_brand_' . $term->term_id );
to another sendmessage.php page which is never accessed other than with the of JS to send a form. The variable I need filled is called:
$sendto="value";
How can I achieve this?
EDIT: Apparently this requires more information.
Use session variables.
First of all every page you want to use sessions on needs to have the function session_start(); declared at the top of every page. You then can use the session superglobal to store things.
Pg1.php:
<?php
session_start();
$_SESSION['variable_name'] = 'string';
?>
Pg2.php:
<?php
session_start();
echo $_SESSION['variable_name'];
?>
Pg2.php will show: some string
Either POST your value so your second page can retrieve it in the $_POST array, or include it in the URL path so your second page can $_GET it.
Without more info, we can't really help you any more than that.
I have created a page with name edit.php. I have moved on this page from action.php using a edit button. I have successfully retrieved the values in the respective text boxes and other form items. I have problem that if by mistake this edit.php page is refreshed all values are gone. What is other way to maintain the values? Though thing are going well if page is not refreshed. If session variable is created than how values are retrieved of both that is of session variable and from database?
What I have did with problem.. I have requested "albumid" on action.php page..
session_start();
$aid = mysql_real_escape_string($_REQUEST['albumid']);
Now if action.php page is requested through edit.php page using edit button. than I have created a session variable. and destroyed it after successful update query.
if (isset($_POST["edit"])) {
$_SESSION["aid"]=$aid;
$result= mysql_query("SELECT * FROM table WHERE a_id =".$_SESSION["aid"]) or die(mysql_error());
$row=mysql_fetch_array($result); }
It means now session is created.. if page is refreshed than also session values remains and accordingly values are selected from this variable.
if($_POST['update']!="") {
Update query
session destroyed }
Than also my problem is not solved that is if page is refreshed before hitting update button I am loosing all values.
session variables are just data you put into the $_SESSION superglobal. Accessing them is no different than accessing any other array, except that the session array is saved for you automatically. All you need to remember is to do a session_start() before doing anything with the session.
$_SESSION['formfield1'] = $_POST['formfield1'];
$_SESSION['formfield2'] = $_POST['formfield2'];
etc...
<input type="text" name="formfield1" value="<?php echo htmlspecialchars($_SESSION['formfield1']) ?>" />
By default, PHP uses file-based sessions. To put it into a database, you'd have to write your own session handlers and point PHP to them using session_set_save_handler().
When you submit, save the values in appropriately named $_SESSION members. Then on your page, if you can't find the members in $_GET/$_POST, you can choose to look them up in $_SESSION. Or vice versa. Every time the user submits a form though, you should always update $_SESSION so that the values are the most current. (in case they backtrack, or resubmit, or whatnot).
session_start();
if (!empty($_POST)) {
$_SESSION['post'] = $_POST;
}
elseif (empty($_POST) && !empty($_SESSION['post'])) {
$_POST = $_SESSION['post'];
}
just don't forget to unset($_SESSION['post']); when you're done with it.
If I have understood your question correctly, I think you need some variables from database as well as from the session.
Simply put the key of your database tuple (or keys in case of multiple values) along with other stuff. Page, upon loading will check for the session variables and when it finds the key, it can use it to retrieve the data from the database.
In your previous page, the code will look like this :
$_SESSION["player_key"] = 56;
$_SESSION["tournament_key"] = 100;
And current page, start processing like this :
<?php
session_start();
$player = $_SESSION["player_key"];
$tournament = $_SESSION["tournament_key"];
/*
* your database connection steps
*/
$query = "select * from player where pid=".$player;
$res = mysql_query($query);
/*
Now assign values for your forms/components here or anywhere in the page. You don't have to read anything from the querystring.
*/
?>