Page 1 of 1

Problem with indexes on drop down

Posted: Mon Apr 02, 2007 9:47 am
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

Posted: Mon Apr 02, 2007 9:52 am
by feyd

Code: Select all

(isset($_POST['test']) && $_POST['test'] == $value) ? ' selected="selected"' : ''

Posted: Mon Apr 02, 2007 10:01 am
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

Posted: Mon Apr 02, 2007 10:08 am
by feyd
it's not an if expression. It replaces your existing ternary.

Posted: Mon Apr 02, 2007 10:24 am
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

Posted: Mon Apr 02, 2007 10:26 am
by feyd
Is $display not initialized somewhere?

Posted: Mon Apr 02, 2007 10:32 am
by genista
No only here, this was given to me so I ma just trying to solve what I have, any ideas?

Thanks

Posted: Mon Apr 02, 2007 10:45 am
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.

Posted: Mon Apr 02, 2007 10:50 am
by genista
ok, this looks great, I will go through some testing, but for the moment we seem to be done.


Thanks,

G

Posted: Mon Apr 02, 2007 1:15 pm
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.