Jump to content

Sticky Select Menu Error Message


groma1

Recommended Posts

Hi all, have a sticky select menu for favourite colour.

However need to post a message if user tries to submit it empty i.e. "please select colour".

Not sure how but would it work when $csel=false (where would i check this?)

 

then display the error message by

 

if ($csel===false){
$errs[]='Please select your favourite colour <br />';
}

 

 

If anyone could give a heads up would appreciate it, code is as below

cheers.

 

p.s giving me an undefined index error for line $csel=$_POST['colour']; so i assume it is because i have not created a rule when the select menu is empty.

 

$csel = array();
$csel = $_POST['colour'];
print '<label>Select your favourite colour(s):';
print '</label>';
print '<select name="colour[]" multiple="multiple">';

$colours=array('R'=>'Red','O'=>'Orange','Y'=>'Yellow','G'=>'Green','B'=>'Blue','P'=>'Purple','Pi'=>'Pink');

foreach($colours as $colour => $cname){
$sel = '';
foreach ($csel as $clr) {
if($clr === $colour){
$sel = 'selected="selected"';
}
}
print '<option value="'.$colour.'" '.$sel.'>'.$cname.'</option>';
}
print '</select>';

Link to comment
https://forums.phpfreaks.com/topic/269354-sticky-select-menu-error-message/
Share on other sites

You need to check if $_POST['colour'] exists before trying to use it. You could use a ternary operation to assign $csel.

$csel = isset($_POST['colour']) ? $_POST['colour'] : NULL;.

 

Also, based on your code, there is no way $csel is going to be an array, so don't try to use a foreach on it. You're already looping through the colors.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.