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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.