How do I write the IF Statement for a dropdown listbox in PHP

I have the following form elements:

//Last Name
 <label for="lastName">Last Name<span class="required">*</span></label>
                    <input name="lastName" id="lastName" type="text" value="" />

//Sex
 <label>Sex<span class="required">*</span></label>
                    <div class="radioList">
                        <div class="radioPair">
                            <label>
                                <input name="sex" type="radio" value="Male"  />&nbsp;Male
                            </label>
                        </div>
                        <div class="radioPair">
                            <label>
                                <input name="sex" type="radio" value="Female"  />&nbsp;Female
                            </label>
                         </div>

//Session Number

                      <label>Please select the session number: </label>
                      <select name="threeWeekSessionNum" id="threeWeekSessionNum"> <option value = "">Session Number</option>
                       <option value = "1">1</option>
                       <option value = "2">2</option>  
                       <option value = "3">3</option>
                       <option value = "4">4</option>
                       <option value = "5">5</option>  
                       <option value = "6">6</option>
                       <option value = "7">7</option>
                        </select>  

The validation for the last name and radio button is as follows:

if( !$("input[name='lastName']").val() ){
                alert('Please enter a Last Name');
                return false;    

if( !$("input:radio[name='sex']:checked").val() ){
                alert('Please enter a Sex');
                return false;    
            }

I would like some help in writing the code for the listbox validation as I am not familiar with the syntax. If the user selects "Session" (meaning they didn't make an actual selection), then I would like it to throw up an alert like the others.
geeta_m9Asked:
Who is Participating?
 
F PCommented:
if($("#threeWeekSessionNum option:selected").text() == "Session Number")
{
    alert("You shall not pass... without selecting a valid session number");
    event.preventDefault();
    return false;
}
0
 
F PCommented:
Of course, I would wrap all your validation on an anonymous function triggered when the form is submitted, or button clicked.
0
 
F PCommented:
BTW, that code I submitted was javascript. If you want to do validation in PHP, I would use this:

// I used REQUEST because I don't know if you're POSTing or GETting data
// --- use $_POST or $_GET depending on which you use because it's faster than parsing those + $_COOKIE

if(strpos(strtolower($_REQUEST['threeWeekSessionNum']), 'session') !== FALSE)
{
    echo 'Didn't select a session number!";
}
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Vimal DMSenior Software EngineerCommented:
Hi,

$op2 .= "\n\t<option <?PHP if($d['area_id']=='')echo 'disabled=disabled'; ?> value=\"{$d['area_id']}\">{$d['area']}</option>";


<select id="area" name="area">
<?php echo $op2;?>
</select>
0
 
Scott MadeiraCommented:
Another thing you can do on the PHP side is set the option value for Session to "#"  (you have it set to "") and test for an integer response  in your validation.

if( ! is_int( $_REQUEST['threeWeekSessionNum']))
{
    echo 'Didn't select a session number!";
}

Open in new window


Also, I'm not a javascript guru but you should be able to do a similar check for an integer selection there as well...
0
 
geeta_m9Author Commented:
Your solution works great, Frank! Thank you to all of you for your help.
0
 
F PCommented:
Glad I could help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.