PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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!!
<?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:
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 ).
<?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
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.
<?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>
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
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.
Last edited by microthick on Wed Jan 21, 2004 5:23 pm, edited 1 time in total.
<?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.
}
?>
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.