Page 1 of 3
Variable problem
Posted: Wed Jan 21, 2004 4:16 pm
by birkel
This is my first post and I am pretty new to PHP, so go easy on me.
PHP Version: 4.0.5
Display Errors: On
Error Level: Not E_ALL
Register Globals: On
I have two files with. I am wanting to take the select variable of the form from testing.php and send it to the script testing2.php. In testing2.php I am hitting a form button which reloads the page, but when the page reloads I lose my variable. Help!!
First file:
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head><body>
<form action="testing2.php" method="post"><select name="sel_id">
<option>MY Value</option>
</select>
<input type="hidden" name="op" value="view">
<p><input type="submit" name="submit" value="Edit your selection"></p>
</form>
</body></html>
The second file:
Code: Select all
<?php
if ($HTTP_POST_VARSїop] != "add") {
echo "The value you have passed is: $HTTP_POST_VARSїsel_id]";
$display_block .= "
<form method="post" action="$_SERVERїPHP_SELF]">
<input type="hidden" name="op" value="add">
<p><input type="submit" name="submit" value="Save"></p>
</FORM>";
} else if ($HTTP_POST_VARSїop] == "add"){
echo "The value you have passed is: $HTTP_POST_VARSїsel_id]";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
print $display_block;
?>
</body>
</html>
The output I get before I hit save is:
The value you have passed is: MY Value
The output after I hit save:
The value you have passed is:
What gives??
Posted: Wed Jan 21, 2004 4:29 pm
by Straterra
NOO! You are using depreciated stuff! Instead of
,
use
. Also, I don't know if this will make any difference, but instead of
Code: Select all
} else if ($HTTP_POST_VARS[op] == "add"){
, use
Code: Select all
} elseif ( $POST['op'] == "add" ) {
Notice how I don't have a space between the else and if. Tries these out, and tell me if it works.
EDIT : Upgrade your PHP, as it is VASTLY outdated. I currently run the 4.3.5RC1 version, but the latest stable is 4.3.4 . You can download it at
http://us2.php.net/downloads.php
EDIT #2 : Welcome to the Forums, you will find that most of us are friendly and will help you with your problems. Just remember that the PHP documents are your friends (
http://www.php.net/docs.php ). Also, remember to follow the General Posting Guidelines (
viewtopic.php?t=8815 ).
Posted: Wed Jan 21, 2004 4:36 pm
by Roja
Straterra wrote:NOO! You are using depreciated stuff! Instead of
,
use
.
Surely you mean..

Posted: Wed Jan 21, 2004 4:38 pm
by Straterra
ACK! Yes! $_POST['op']. Sorry about that, brain fart.
Posted: Wed Jan 21, 2004 4:45 pm
by birkel
ok I changed the second script to this
Code: Select all
<?php
if ($_POST['op'] != "add") {
echo "The value you have passed is: $_POST['sel_id'];
$display_block .= "
<form method="post" action="$_SERVER[PHP_SELF]">
<input type="hidden" name="op" value="add">
<p><input type="submit" name="submit" value="Save"></p>
</FORM>";
} elseif ($_POST['op'] == "add"){
echo "The value you have passed is: $_POST['sel_id']";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
print $display_block;
?>
</body>
</html>
Now I get this error message
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\apache\htdocs\nhc\testing2.php on line 3
and if I comment this out, then $_POST['sel_id']" is still an empty var
Posted: Wed Jan 21, 2004 4:47 pm
by dull1554
you have this
echo "The value you have passed is: $_POST['sel_id'];
you need it to be this
echo "The value you have passed is: ".$_POST['sel_id']; //you left out a quote and a period
Posted: Wed Jan 21, 2004 4:53 pm
by birkel
ok, now I do not have an error, but sel_id is empty when the second script first loads, and when I hit save it is not getting to the elseif portion of my script.
What exactly is the period doing in
Code: Select all
<?php echo "The value you have passed is: ".$_POST['sel_id'];
?>
Posted: Wed Jan 21, 2004 4:54 pm
by Straterra
Periods are used for concantination, much like the & in VB. All the . does is add the two strings together.
Posted: Wed Jan 21, 2004 5:10 pm
by birkel
Now the value of op and sel_id is "".
op used to have a value when I was using $HTTP_POST_VARS, but since I changed to $_POST.
Code: Select all
<?php
if ($_POST['op'] != "add") {
echo "The value you have passed is: ".$_POST['sel_id'];
echo "The value of op: ".$_POST['op'];
$display_block .= "
<form method="post" action="$_SERVER[PHP_SELF]">
<input type="hidden" name="op" value="add">
<p><input type="submit" name="submit" value="Save"></p>
</FORM>";
} elseif ($_POST['op'] == "add"){
echo "The value you have passed is: ".$_POST['sel_id'];
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
print $display_block;
?>
</body>
</html>
Again my first script that passes sel_id is
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head><body>
<form action="testing2.php" method="post"><select name="sel_id">
<option>MY Value</option>
</select>
<input type="hidden" name="op" value="view">
<p><input type="submit" name="submit" value="Edit your selection"></p>
</form>
</body></html>
Posted: Wed Jan 21, 2004 5:14 pm
by Straterra
What does the line
do? Also, where is there a period when you are setting the $display_block variable?
Code: Select all
$display_block .= "
<form method="post" action="$_SERVER[PHP_SELF]">
<input type="hidden" name="op" value="add">
<p><input type="submit" name="submit" value="Save"></p>
</FORM>";
Posted: Wed Jan 21, 2004 5:19 pm
by birkel
the period was left over from a cut and paste, thanks for noticing that.
The line
Code: Select all
<?phpif ($_POST['op'] != "add") {
?>
Should be nothing when the page is first loaded, but after I hit the save button, it should change to add, so that I know the user has hit the save button, and I can process whatever the "sel_id" might be
Posted: Wed Jan 21, 2004 5:22 pm
by microthick
Hi birkel,
You were misinformed by some of the above posters.
You should continue using $HTTP_POST_VARS rather than $_POST.
$HTTP_POST_VARS was depreciated in PHP 4.0.6 or 4.0.7 or so, but since you're using PHP 4.0.5, you should continue to use the post array you were using originally.
But, correct information was provided as well.
All occurances of $HTTP_POST_VARS[op] should be changed to $HTTP_POST_VARS['op'] or $HTTP_POST_VARS["op"].
The error in your code is as follows.
In your first file, sel_id is a form field and when you press "Edit your selection" the value of sel_id is passed to testing2.php.
On the initial load of testing2.php, $HTTP_POST_VARS["sel_id"] would equal the selected id.
But when you press the "Save" button to submit the form, it is passing op (which equals "add") but you are not passing sel_id. Your form on testing2.php does not contain sel_id as a form field which is why it is always appearing as a blank after you press Save.
Posted: Wed Jan 21, 2004 5:22 pm
by Straterra
What I would do is this
Code: Select all
<?php
if ( isset($_POST['op') == false ) {
//Do everything you want done if the form has not been submitted.
} else {
//Do everything you want done if the form has been submitted.
}
?>
Posted: Wed Jan 21, 2004 5:28 pm
by Straterra
Hey Micro..I also said that his version of PHP should be upgrades as well, which would make those depreciated....
Posted: Wed Jan 21, 2004 5:32 pm
by microthick
Straterra wrote:Hey Micro..I also said that his version of PHP should be upgrades as well, which would make those depreciated....
I see. And that's great advice.
But depending on his situation, an upgrade might not be an option for the next little while. So I was just explaining why $_POST["op"] was returning empty strings.