I have a simple question, with maybe not so simple of an answer. I want to be able to set a variable in one script and pass that value and variable to another script. Without passing it through the url, and having the ability to pass an array.
So I have index.php in there I have a variable
<?php
$myvariable = '';
<form action=editRecord.php>
do some form actions here and submit moving to editRecord.php
</form>
?>
Now in editRecord.php
<?php
header('Location: go back to index.php);
run functions and edit the Mysql DB
//Ok now here is where I want to assign a value to $myvariable and pass it back to index.php
?>
Is this possible? I am sorry for the amateurish question, but I am very green when it comes to php.
Thank You.
you can set it in the $_SESSION variable:
<?php
session_start();
$_SESSION["myVar"] = "blabla";
....
Of course you can store an array() in this variable too.
Just pass that information in the querystring. Then you can access it via $_GET. If it doesn't exist, just set the value to an empty string or whatever default value you want it to have.
// editRecord.php
<?php
// do db dtuff
header('Location: index.php?myvariable=somevalue');
?>
// index.php
<?php
$myvariable = (isset($_GET['myvariable'])) ? $_GET['myvariable'] : '';
<form action="editRecord.php" >
</form>
?>
You can also use session variables:
// editRecord.php
<?php
session_start();
// do db stuff
$_SESSION['myvariable'] = 'somevalue';
header('Location: index.php');
?>
// index.php
<?php
session_start();
$myvariable = (isset($_SESSION['myvariable'])) ? $_SESSION['myvariable'] : '';
<form action="editRecord.php" >
</form>
?>
You can use sessions, POST data, GET data, cookies and database data.
Related
I created an app to receive and tally votes. I am trying to make the voter's selection carry over to a confirmation page so that I can display the name of the candidate for whom they voted on the confirmation page.
I am trying to use $_SESSION on the variable of the selected candidate from the voter's submission, and then call the variable on the confirmation page, however I continue to get undefined variable error.
voterSubmit.php:
<?php
$selectedCandidate = $_POST['candidateid'];
session_start();
$selection = $_SESSION[$selectedCandidate];
//Redirect to results page
header("Location: views/confirmation.php");
confirmation.php
<?php
session_start();
$_SESSION[$selectedCandidate] = $selection;
include "views/confirmation.php";
confirmation view:
<?php include "../partials/header.php"; ?>
<h1>Thanks For Your Vote!!</h1><br>
//This is where the error occurs (on my selection variable):
<h2>You voted for <?=$selection?>
View Results
<?php include "../partials/footer.php"; ?>
I want the name of the selected candidate to appear on the confirmation page by way of the $selection variable. However, all I receive on the front end is an "undefined variable" error. I also would like to note that instead of using the $selectedCandidate variable in my session, I have also tried grabbing the name directly by just using the name of the radio button selection as such:
$_SESSION['candidateid'] = $selection
I also would like to mention that i have tried the reverse:
on confirmation.php:
session_start();
$selection = $_SESSION[$selectedCandidate];
on voteSubmit.php:
session_start();
$_SESSION[$selectedCandidate] = $selection;
Your using the $_SESSION variable incorrectly.
Try:
voterSubmit.php:
<?php
$selectedCandidate = $_POST['candidateid'];
session_start();
$_SESSION['selectedCandidate'] = $selectedCandidate;
//Redirect to results page
header("Location: views/confirmation.php");
confirmation.php
<?php
session_start();
$selection = $_SESSION['selectedCandidate'];
include "views/confirmation.php";
voterSubmit.php looks ok... but I don't understand why you have include "views/confirmation.php"; in the confirmation.php file.
<?php
session_start();
$_SESSION[$selectedCandidate] = $selection;
include "views/confirmation.php";
Try coding your HTML/PHP this way:
<?php
/* confirmation.php */
session_start();
$_SESSION[$selectedCandidate] = $selection;
require_once("../partials/header.php");
//This is where the error occurs (on my selection variable):
echo <<<_STRT
<h1>Thanks For Your Vote!!</h1><br>
<h2>You voted for $selection</h2>
<p>View Results</p>
_STRT;
require_once("../partials/footer.php");
?>
I essentially eliminated a lot of start/stop code. The start of your PHP recalls the session and variable. Then I go into the HTML portion to provide the results to your site visitor. Give it a shot. Comment on it if it doesn't resolve your issue so we can rethink it.
I think session_start() needs to be the first line after the php tag.
$_SESSION is an array. So you need to assign it values like:
$_SESSION['keyname'] = $value;
voterSubmit.php:
<?php
session_start();
$_SESSION['selectedCandidate'] = $_POST['candidateid'];
//Redirect to results page
header("Location: views/confirmation.php");
confirmation.php
<?php
session_start();
$selection = $_SESSION['selectedCandidate'];
include "views/confirmation.php";
There are 2 issues in your code :
session_start() defined in the wrong place.
Wrong way of assigning value to your session variable.
Answer :
session_start() should be the first thing defined after your php
tag.
Correct way to declare session variables is : $_SESSION["selectedCandidate"] = $selectedCandidate; where, $selectedCandidate is the value to be assigned to your session variable, named selectedCandidate.
You have done 2 mistakes in your code:
1. Session should be defined in starting of page it means you have to define it like this:
<?php
session_start();
//php code goes here.
?>
2.wrong initialization of session variable .you should do it like this:
<?php
$_SESSION['selectedCandidate']=$selectedCandidate;
?>
Hey guys I'm trying to pass a php variable to another page. I tried it with sessions but no result.
newspaper.php
$newspaper= $newspaper['newspath'];
print_r($newspaper);
this outputs:
path/to/the/newspaper.
Now I want to use the variable in the second page.
newspaperviewer.php
echo $newspaper;
$SESSION = $newspaper;
I tried the first one but no result. The second one seems to be faulty.
Hope you guys can help me out.
Session is what you are looking for. A session variable can store a value and use this value on all pages of your project.
First thing to do is to start session on each file on your project. You can do this like this example
<?php
session_start(); //declare you are starting a session
$_SESSION['newspaper'] = "New York Times"; //Assign a value to the newspaper session
?>
On the other file you can use the value of the session by trying something like this
<?php
session_start(); //always start session don't forget!!
echo $_SESSION['newspaper'];
// This will echo New York Times
?>
Store the variable after starting session on page A, like so:
// FIRST PAGE (foo.php)
session_start();
$_SESSION['name'] = 'Jack';
Now, on the second page (or any page that you want to have access to $_SESSION, simply do the same but pull the variable.
// SECOND PAGE (bar.php)
session_start();
$name = $_SESSION['name'];
$_SESSION['name'] = null; // Or use session_unset() to delete all SESSION vars.
And that's how you pass variables using $_SESSION.
Please use this code to set session
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
You can write like this
newspaper.php
session_start();
$newspaper= $newspaper['newspath'];
$_SESSION['newspaper'] = $newspaper;
Now you can use this session variable in
newspaperviewer.php
session_start();
$newspaper = $_SESSION['newspaper'];
echo $newspaper;
session_unset(); // remove all session variables
First
newspaper.php
$newspaper= $newspaper['newspath'];
//print_r($newspaper);
session_start(); //it starts your session here
$_SESSION['newspaper']=$newspaper; //it sets a session variable named as newspaper
Second
$newspaper= isset($_SESSION['newspaper'])?$_SESSION['newspaper']:''; //checks and sets value
echo $newspaper; //outputs value
For more see session_start
http://php.net/manual/en/session.examples.basic.php
First you will need to start the session by using session_start() at the top of your page. Second, a session variable is written like this: $_SESSION['foo'].
I suggest you read these pages to get a better understanding of what's going on.
http://php.net/manual/en/reserved.variables.session.php
http://www.w3schools.com/php/php_sessions.asp
How can i get the value of previous url in php code?
Here is the url page1.php
http://localhost/spk/kelulusan_process.php?lulus=10003
page2.php:
I want to get the parameter from the previous url in page1.php. I use
<?php
$_GET['lulus'];
?>
But the value is null.
For $_GET to work you have to redirect user using form action
Or you can do that by setting session variables and access them any page
You can try $_SERVER["HTTP_REFERER"]
But don't forget to escape $_SERVER["HTTP_REFERER"] since it's common for attacks.
Better is to store the current page in a $_SESSION var.
if (!isset($_SESSION)) {
session_start();
}
$_SESSION['lastpage'] = $_SERVER['REQUEST_URI'];
Then when loading the next page:
if (!isset($_SESSION)) {
session_start();
}
// now you can access the last page
$lastpage = $_SESSION['lastpage'];
page2.php
<?php
echo htmlspecialchars($_GET["lulus"]);
?>
how are you trying to pass from page 1?
if through a form set the method to get and action to page2.php
in page1.php make sure you set the name
<form method="get" action="page2.php">
<input type="text" name="lulus">
<input id="submit" name="submit" type="submit" value="Send">
You cannot display $_GET['lulus']; in page2.php unless you write get parameter in page2.php.
Your code is redirect from page1.php to page2.php, You must set $_GET['lulus']; into one session and echo in page2.php
Page1.php
<?
$_SESSION['GET'] = $_GET['lulus'];
?>
Page2.php
<?
echo $_SESSION['GET'];
?>
Instead, echo $_GET['lulus'], you should echo in page2.php $_SESSION. Because $_SESSION variable was overrode by $_GET itself.
Use $_SERVER['HTTP_REFERER'] ... hopefully this will help you. And parse url something like below:
$url = $_SERVER['HTTP_REFERER'] ; //http://localhost/spk/kelulusan_process.php?lulus=10003
$array = explode("?lulus=",$url);
echo $array[1];
When I post my form data:
<form action="layouts.php" method="post">
<input type="text" name="bgcolor">
<input type="submit" value="Submit" align="middle">
</form>
to a php page, "layouts.php", it returns the string, as expected.:
$bgcolor = $_POST['bgcolor'];
echo $bgcolor; //returns "red"
echo gettype($bgcolor); // returns "string"
But when I include "layouts.php" in another page it returns NULL.
<?php
include("php/layouts.php");
echo $bgcolor; //
echo gettype($bgcolor); //returns "NULL"
?>
How do I pass the variable to another page?
You'll have to use a session to have variables float around in between files like that.
It's quite simple to setup. In the beginning of each PHP file, you add this code to begin a session:
session_start();
You can store variables inside of a session like this:
$_SESSION['foo'] = 'bar';
And you can reference them across pages (make sure you run session_start() on all of the pages which will use sessions).
layouts.php
<?php
session_start();
$bgcolor = $_POST['bgcolor'];
$_SESSION['bgcolor'] = $bgcolor;
?>
new.php
<?php
session_start();
echo $_SESSION['bgcolor'];
?>
Give the form two action="" and see what happens. I just tried that on a script of mine and it worked fine.
A more proper way to solve this might exist, but that is an option.
How to pass variables from a php file to another while it is not html inputs ,just i have a link refer to the other file and i want to pass variables or values to it
Example:
File1.php
<?php
$name='OdO';
echo "<a href='File2.php'>Go To File2</a>";
?>
File2.php
<?php
echo $name;
?>
Use sessions to store any small value that needs to persist over several requests.
File1.php:
session_start();
$_SESSION['var'] = 'foo';
File2.php:
session_start();
$var = $_SESSION['var']; // $var becomes 'foo'
Try to use sessions. Or you can send a GET parameters.
You can use URLs to pass the value too.
like
index.php?id=1&value=certain
and access it later like
$id = $_GET['id'];
$value = $_GET['value'];
However, POST might be much reliable. Sessions/Cookies and database might be used to make the values globally available.
Here's one (bad) solution, using output buffering:
File 1:
<?php
$name = 'OdO';
echo 'Go To File2';
?>
File 2:
<?php
ob_start();
include 'File1.php';
ob_end_clean();
echo $name;
?>