Variable problem

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!

Moderator: General Moderators

birkel
Forum Newbie
Posts: 11
Joined: Wed Jan 21, 2004 4:16 pm
Location: Milwaukee, WI

Variable problem

Post 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&#1111;op] != "add") &#123;
echo "The value you have passed is: $HTTP_POST_VARS&#1111;sel_id]";
	
	$display_block .= "
	<form method="post" action="$_SERVER&#1111;PHP_SELF]">
	<input type="hidden" name="op" value="add">
	<p><input type="submit" name="submit" value="Save"></p>
   </FORM>";
&#125; else if ($HTTP_POST_VARS&#1111;op] == "add")&#123;
echo "The value you have passed is: $HTTP_POST_VARS&#1111;sel_id]";
&#125;
?>
<!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??
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

NOO! You are using depreciated stuff! Instead of

Code: Select all

$HTTP_POST_VARS[op]
,
use

Code: Select all

$POST['op']
. 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 ).
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Straterra wrote:NOO! You are using depreciated stuff! Instead of

Code: Select all

$HTTP_POST_VARS[op]
,
use

Code: Select all

$POST['op']
.
Surely you mean..

Code: Select all

$_POST['op']
:)
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

ACK! Yes! $_POST['op']. Sorry about that, brain fart.
birkel
Forum Newbie
Posts: 11
Joined: Wed Jan 21, 2004 4:16 pm
Location: Milwaukee, WI

Post 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
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
birkel
Forum Newbie
Posts: 11
Joined: Wed Jan 21, 2004 4:16 pm
Location: Milwaukee, WI

Post 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']; 

?>
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

Periods are used for concantination, much like the & in VB. All the . does is add the two strings together.
birkel
Forum Newbie
Posts: 11
Joined: Wed Jan 21, 2004 4:16 pm
Location: Milwaukee, WI

Post 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

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;&lt;head&gt;&lt;title&gt;Untitled Document&lt;/title&gt;&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;/head&gt;&lt;body&gt;
&lt;form action="testing2.php" method="post"&gt;&lt;select name="sel_id"&gt;
  &lt;option&gt;MY Value&lt;/option&gt;
&lt;/select&gt;
&lt;input type="hidden" name="op" value="view"&gt;
        &lt;p&gt;&lt;input type="submit" name="submit"  value="Edit your selection"&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;&lt;/html&gt;
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

What does the line

Code: Select all

if ($_POST['op'] != "add") {
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>";
birkel
Forum Newbie
Posts: 11
Joined: Wed Jan 21, 2004 4:16 pm
Location: Milwaukee, WI

Post 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
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post 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.
Last edited by microthick on Wed Jan 21, 2004 5:23 pm, edited 1 time in total.
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post 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.
}
?>
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

Hey Micro..I also said that his version of PHP should be upgrades as well, which would make those depreciated....
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post 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.
Post Reply