The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Undefined variable that "is" defined?
Discuss Undefined variable that "is" defined? in the PHP Development forum on Dev Shed. Undefined variable that "is" defined? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 25th, 2012, 05:30 PM
|
|
Contributing User
|
|
Join Date: Jun 2012
Posts: 99
 
Time spent in forums: 21 h 59 m 39 sec
Reputation Power: 1
|
|
Undefined variable that "is" defined?
Hi all,
I've got a page (blah.html) that checks if a value is set for a select box. If it's not, then the select box is displayed with options. If it is set, then some other stuff is displayed.
First time into this page, $var1 is not set (as expected), so the select box is shown.
However, when either "This" or "That" is selected (i.e. $var1 then has a value) and the form is submitted, the expected result should be that the "Some other html" should be displayed. It's not. When I echo $var1, it always shows "" instead of "1" or "2" when either is selected. And, the php error says "Undefined variable" after a selection is made and the form is submitted.
Code:
<? if (!isset($var1)) { ?>
<form action="blah.html" method="post">
<select name="var1">
<option value="">-Select One-
<option value="1">This
<option value="2">That
</select>
</form>
<? } else { ?
Some other html.
<? } ?>
If "This" or "That" is selected on the first loading of the page, and then submitted, what would cause $var1 to * not* be set and thus not falling into the } else { where is should be after the submit?
|

December 25th, 2012, 07:21 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
Hi,
I don't see $var1 defined anywhere in the code. I hope you're not using the ancient register_globals?
What does your browser tell you about the POST request? Open the developer tools and check the form parameters.
|

December 25th, 2012, 08:16 PM
|
|
Contributing User
|
|
Join Date: Aug 2011
Location: Sydney Australia
|
|
Quote: | Originally Posted by we5inelgr Hi all,
First time into this page, $var1 is not set (as expected), so the select box is shown.
|
What do you think sets $var1 ?
You could get it like this
Quote:
However, when either "This" or "That" is selected (i.e. $var1 then has a value) |
Not until you assign it a value.
Unless you are using really old code that puts all your $_GET and $_POST variables into php variables, and that's not a good idea.
This code works, but NOT recommended
PHP Code:
<html>
<head>
</head>
<body><?php
if (!isset($_POST['var1'])) {
?>
<form action="blah.php" method="post">
<select name="var1">
<option value="">-Select One-
<option value="1">This
<option value="2">That
</select>
<input type="submit" value="submit">
</form>
<?php } else { ?>
Some other html.
<?php } ?>
</body>
</html>
Also, if you want your ACTION file to do stuff, then it needs to be of a type that the server can parse, like php, not html.
You need to use proper php script tags too.
are shortcut tags and NOT supported everywhere.
You should use
to mark up php code.
Bottom line. You are using a VERY old tutorial. You need to find something more up to date.
|

December 26th, 2012, 02:02 AM
|
|
Contributing User
|
|
Join Date: Jun 2012
Posts: 99
 
Time spent in forums: 21 h 59 m 39 sec
Reputation Power: 1
|
|
|
Thanks for the replies guys.
$var1 is coming from the select box.
When the page first gets loaded, there is nothing selected and therefore no value.
When either option "This" or "That" is selected, and the form is submitted, I can see other variables on the resulting page...but for some reason the value from the select dropdown doesn't get set.
|

December 26th, 2012, 02:22 AM
|
|
Contributing User
|
|
Join Date: Jun 2012
Posts: 99
 
Time spent in forums: 21 h 59 m 39 sec
Reputation Power: 1
|
|
If one of the items is selected from the dropdown, and the form submitted,
this fails to identify that $var1 has a value:
if (!isset($var1))
but this works:
if (!isset($_POST['var1']))
Perhaps a newer version of PHP is running on the server since this code was last used successfully?
In other words, is (!isset($var1)) deprecated?
|

December 26th, 2012, 04:40 AM
|
|
Contributing User
|
|
Join Date: Aug 2011
Location: Sydney Australia
|
|
Quote: | Originally Posted by we5inelgr is (!isset($var1)) deprecated? |
Yes.
http://php.net/manual/en/security.globals.php
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|