PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 25th, 2012, 05:30 PM
we5inelgr we5inelgr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 99 we5inelgr User rank is Lance Corporal (50 - 100 Reputation Level)we5inelgr User rank is Lance Corporal (50 - 100 Reputation Level)we5inelgr User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 21 h 59 m 39 sec
Reputation Power: 1
Question 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?

Reply With Quote
  #2  
Old December 25th, 2012, 07:21 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,864 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 20 h 32 m 37 sec
Reputation Power: 813
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.

Reply With Quote
  #3  
Old December 25th, 2012, 08:16 PM
BarryG BarryG is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Location: Sydney Australia
Posts: 131 BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 7 h 51 m 28 sec
Reputation Power: 83
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
PHP Code:
 $var1 $_POST['var1']; 


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.
PHP Code:
<?  ?>

are shortcut tags and NOT supported everywhere.
You should use
PHP Code:
<?php  ?>

to mark up php code.

Bottom line. You are using a VERY old tutorial. You need to find something more up to date.

Reply With Quote
  #4  
Old December 26th, 2012, 02:02 AM
we5inelgr we5inelgr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 99 we5inelgr User rank is Lance Corporal (50 - 100 Reputation Level)we5inelgr User rank is Lance Corporal (50 - 100 Reputation Level)we5inelgr User rank is Lance Corporal (50 - 100 Reputation Level) 
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.

Reply With Quote
  #5  
Old December 26th, 2012, 02:22 AM
we5inelgr we5inelgr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 99 we5inelgr User rank is Lance Corporal (50 - 100 Reputation Level)we5inelgr User rank is Lance Corporal (50 - 100 Reputation Level)we5inelgr User rank is Lance Corporal (50 - 100 Reputation Level) 
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?

Reply With Quote
  #6  
Old December 26th, 2012, 04:40 AM
BarryG BarryG is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Location: Sydney Australia
Posts: 131 BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 7 h 51 m 28 sec
Reputation Power: 83
Quote:
Originally Posted by we5inelgr
is (!isset($var1)) deprecated?


Yes.

http://php.net/manual/en/security.globals.php

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Undefined variable that "is" defined?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap