This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 9 years ago.
In a html form I have a variable $var = "some value";.
I want to call this variable after the form posted. The form is posted on the same page.
I want to call here
if (isset($_POST['save_exit']))
{
echo $var;
}
But the variable is not printing. Where I have to use the code GLOBAL ??
EDIT: After your comments, I understand that you want to pass variable through your form.
You can do this using hidden field:
<input type='hidden' name='var' value='<?php echo "$var";?>'/>
In PHP action File:
<?php
if(isset($_POST['var'])) $var=$_POST['var'];
?>
Or using sessions:
In your first page:
$_SESSION['var']=$var;
start_session(); should be placed at the beginning of your php page.
In PHP action File:
if(isset($_SESSION['var'])) $var=$_SESSION['var'];
First Answer:
You can also use $GLOBALS :
if (isset($_POST['save_exit']))
{
echo $GLOBALS['var'];
}
Check this documentation for more informations.
Try that
First place
global $var;
$var = 'value';
Second place
global $var;
if (isset($_POST['save_exit']))
{
echo $var;
}
Or if you want to be more explicit you can use the globals array:
$GLOBALS['var'] = 'test';
// after that
echo $GLOBALS['var'];
And here is third options which has nothing to do with PHP global that is due to the lack of clarity and information in the question. So if you have form in HTML and you want to pass "variable"/value to another PHP script you have to do the following:
HTML form
<form action="script.php" method="post">
<input type="text" value="<?php echo $var?>" name="var" />
<input type="submit" value="Send" />
</form>
PHP script ("script.php")
<?php
$var = $_POST['var'];
echo $var;
?>
Related
This question already has answers here:
How to get PHP $_GET array?
(8 answers)
Closed 2 years ago.
I got a challenge in which I have this code in a PHP file:
<?php
include('secretfile.php');
if(isset($_GET['a']) && isset($_GET['b']))
{
$a = $_GET['a'];
$b = $_GET['b'];
if (!empty($a) && !empty($b))
{
if($a===$b)
{
if(isset($_GET['a']) && isset($_GET['b']))
{
$a = $_GET['a'];
$b = $_GET['b'];
if($a!==$b)
{
echo $secretcode;
}
}
}
}
}
I have to print secretcode in webpage with OUT changing the PHP file.
How can I do it?
I tried by giving parameters through URL like this:
http://127.0.0.1:8080/?a=1&b=1&a=22&b=33
But it didn't work. The file is taking the last values directly and no matter what, I couldn't go past the 13th line. I went through a lot of answers but I go no solution.
Is it possible to do it? If yes, how?
To pass multiple values for a or b, you need to use the [] notation like below:
http://127.0.0.1:8080/?a[]=1&b[]=1&a[]=22&b[]=33
That being said, the PHP code in your file seems to be poorly written.
To pass an array on html form you can use [] like
<form method="GET">
<input type="text" name="a[]" />
<input type="text" name="b[]" />
<form/>
This question already has answers here:
Fatal error: [] operator not supported for strings
(9 answers)
Closed 6 years ago.
I have the following code where the user selects the value from a drop down. Everytime they submit, the ID should be pushed in the array.
<?php
$_SESSION["test"] = array();
$link = mysql_connect("localhost","root","");
mysql_select_db("dbsikkim",$link);
?>
<form name="form1" method="post" action="">
<?php
$sql=mysql_query("SELECT ID,DOC_DESC FROM gstn_document_type_master",$link);
echo "<select name='doc_type_code2'>";
if(mysql_num_rows($sql)){
while($row=mysql_fetch_array($sql)){
echo "<option value=".$row['ID'].">".$row['DOC_DESC']."</option>";
}
}
echo "</select>";
?>
<input type="submit" value="Submit" name="submit">
</form>
<?php
if(isset($_POST['submit']))
{
$_SESSION["test"][] = $_POST['doc_type_code2']; //ERROR IN THIS LINE
for ($i=0; $i < sizeof($_SESSION["test"]); $i++) {
echo $_SESSION["test"][$i]."<br>";
}
}
?>
Only the current value is pushed into the array and replaces the previous one. In simple words, it is working as a variable and not an array.
You most likely need to get your session variable initialized.
$_SESSION["test"] is not an array by default. You could try to do:
// If the session variable is not yet an array, initialize it
if (!is_array($_SESSION["test"])) {
$_SESSION["test"] = [];
}
$_SESSION["test"][] = $_POST['doc_type_code2'];
The first line assigns an array to your field.
The second one assigns a value to the array.
Please note, there are several things to improve in your code. In example, you could use PDO these days. Also you should not use "global". Also mixing PHP and HTML code like that might lead to software which is hard to maintain. I don't want to sound arrogant, but I highly recommend you the book "PHP The right way".
I'm just trying to add two integers using PHP and XAMP.
I've placed my client.html file and service.php (which add numbers) in C:\xampp\htdocs
and I get
"Notice: Undefined variable: _get in C:\xampp\htdocs\service.php on
line 7
Notice:
Undefined variable: _get in C:\xampp\htdocs\service.php on line 8"
error.
Before posting this error to Stack Overflow. Let me tell you that I double checked my file names, variable names case-sensitive etc everything. but still having the same error. Any help will be really appreciated.
This is my client.html
form action="service.php" method="get">
input type="text" name="txt1"> <br />
input type="text" name="txt2"> <br />
input type="submit" value="add"><br />
and here is service.php
<?PHP
echo "This is my first program in php";
$a= $_get['txt1'];
$b= $_get['txt2'];
echo $a + $b;
?>
That's because $_GET and $_get are two different variables. You must use capital letters. So PHP thinks you're referring to another variable.
This will work :
<?php
echo "This is my first program in php";
$a= $_GET['txt1'];
$b= $_GET['txt2'];
echo $a + $b;
If you're new to PHP, these two pages should help :
Variable basics (php.net) and $_GET
GET Variable name should be all in CAPS,
So your code might look something like this,
<?PHP
echo "This is my first program in php";
$a= $_GET['txt1'];
$b= $_GET['txt2'];
echo $a + $b;
?>
Reference: http://php.net/manual/en/reserved.variables.get.php
$_GET is predefined reserved variable.
Also it's advisable to use POST method (as #Anant mentioned) to send sensitive data to server, You can access those data which is sent using POST method by $_POST variable.
GET is a SUPER GLOBAL VARIABLE and to access it you have to use $_GET.
So do like below:-
<?PHP
echo "This is my first program in php";
$a= $_GET['txt1'];
$b= $_GET['txt2'];
echo $a + $b;
?>
Note:-
using POST is more secure than GET (in the sense that data shown into the url in get request, but not in post)
So just use post instead of get in <form method>
and $_POST instead of $_GET.
like:-
form action="service.php" method="POST">
input type="text" name="txt1"> <br />
input type="text" name="txt2"> <br />
input type="submit" value="add"><br />
AND
<?PHP
echo "This is my first program in php";
$a= $_POST['txt1'];
$b= $_POST['txt2'];
echo $a + $b;
?>
I am developing a form in PHP. There are variables which are fix which will be displayed in the link of the web page. But some variable which are passed to other page are in hidden and are not fixed.
Eg.
http://editform.php?var1=23&var2=34 will have hidden variables hidvar=23
http://editform.php?var1=23 this will not have any hidden variable and also var2 is also not there
I have checked for variable in link with isset function.
if(isset($_GET['var2']))
now how to get all the variables values in another page with all combination of hidden variables and variable in Link.
I am further adding code which let you get the Idea what I need. Below web page is saved as webform.php
<?PHP
if(isset($_GET['YID']))
{ $YRID=$_GET["YID"]; }
else
{ $YRID=0; echo "Variable Missing. Program terminated."; }
?>
// GET THE VALUE OF $PASS;
//GET THE VALUE OF SESSIONID;
//GET THE VALUE OF YID.
<form action="WEBFORM.php?PASS=<?PHP echo $PASS;?>" name="FORM1" METHOD="POST">
<?php
//statement which do some operation using $YRID;
?>
<input type="hidden" name="SESSIONID" VALUE="<?PHP echo $SESID; ?>" />
</FORM>
while (list($key, $value) = each($_REQUEST))
{
echo ($key.' '.$value);
}
Where $key is the variable name, $value is variable value
I have tried the below code
isset($_POST['SUBMIT'])
if form is submitted then above code will check the variable which are hidden.
for checking the hidden variables and for the variable in link i have checked using below code
if(isset($_GET['YID']))
if form is not yet submitted then above code will check the variable.
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.