Problem with indexes on drop down

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

Post Reply
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Problem with indexes on drop down

Post by genista »

Hi,

I have a script that generates html based on certain conditions being met, one of the types is a drop down list. I have been given this script below to generate the drop down, but the problem I am having relates to undefined indexes as indicated below:

Code: Select all

$display .= "Please select rate type:"; **** Undefined index on '$display'
    $display .= '<select size="1" name="test">'; 
foreach(array('Approximate Charge', 'Per Hour') as $value) 
{ 
$selected = ($_POST['test'] == $value) ? ' selected="selected"' : ''; **** undefined index on 'test'
$display .= "<option value='$value'$selected>$value</option>\n"; 
} 
$display .= '</select>'; 
echo $display;
When I enter a value in the drop down and hit submiot the page returns with these errors gone, I therefore tried to use isset on post but this then did not display the html above because nothing is intially set.


Any ideas?


Thanks,


G
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

(isset($_POST['test']) && $_POST['test'] == $value) ? ' selected="selected"' : ''
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Post by genista »

Ok thanks for that, here is what I did with what you gave:

Code: Select all

if	(isset($_POST['test']) && $_POST['test'] == $value) ? ' selected="selected"' : ''{
$display .= "Please select rate type:"; 
    $display .= '<select size="1" name="test">'; 
foreach(array('Approximate Charge', 'Per Hour') as $value) 
{ 
$selected = ($_POST['test'] == $value) ? ' selected="selected"' : ''; 
$display .= "<option value='$value'$selected>$value</option>\n"; 
} 
$display .= '</select>'; 
echo $display; 
}
The problem I have is an 'unexpected '?' on' the first line of the code above.


G
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's not an if expression. It replaces your existing ternary.
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Post by genista »

ok thanks, here is what I have now:

Code: Select all

$display .= "Please select rate type:"; 
    $display .= '<select size="1" name="test">'; 
foreach(array('Approximate Charge', 'Per Hour') as $value) 
{ 
$selected = (isset($_POST['test']) && $_POST['test'] == $value) ? ' selected="selected"' : ''; 
$display .= "<option value='$value'$selected>$value</option>\n"; 
} 
$display .= '</select>'; 
echo $display;
The one last problem I have is I am getting an undefined variable error on $display.


Thanks!

G
Last edited by genista on Mon Apr 02, 2007 10:33 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Is $display not initialized somewhere?
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Post by genista »

No only here, this was given to me so I ma just trying to solve what I have, any ideas?

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the ".=" operator requires the variable to exist and be initialized prior to it's evaluation. So you either need to change the first one to a normal assignment, or initialize the variable prior.
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Post by genista »

ok, this looks great, I will go through some testing, but for the moment we seem to be done.


Thanks,

G
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Do you understand any of what was fixed? I only ask because there is a strong likelihood that these same errors will come up again in later code of yours if you are not sure about what was handled here.
Post Reply