Kevin Aartsen Kevin Aartsen - 1 month ago 20
PHP Question

If(isset()) with select options

I was wondering why my code is not working.
Here is my html:

<select class="form-control" name="trademarktm">
<option name='empty'></option>
<option name='copyright'>©</option>
<option name='trademark'>™</option>
<option name='rmark'>®</option>
</select>


i gave the copyright token the name 'copyright' so i could call it in my PHP like i did here:

if(isset($_POST['copyright'])) {
echo "<div id='generated_footer_date' style='background-color:$backgroundColor; color:$fontColor; opacity: $opacity; ' >$trademark $date $company </div>";
} else {
echo "error";
}


In this case i only get the string Error even if copyright is set.

Answer

The select gets the name, not the option. The option should have the value.

<select class="form-control" name="trademarktm">            
    <option value='empty'></option>
    <option value='copyright'>©</option>
    <option value='trademark'>™</option>
    <option value='rmark'>®</option>
</select>

Then...

if(isset($_POST['trademarktm']) && $_POST['trademarktm'] == 'copyright') {
     echo "<div id='generated_footer_date' style='background-color:$backgroundColor; color:$fontColor; opacity: $opacity; ' >$trademark $date $company </div>";          
} else {
     echo "error";
}
Comments