Jump to content

Dropdown Menu


nitation

Recommended Posts

Hello,

 

Am trying to validate a drop down menu using PHP. I am actually processing my form on the same page.

 

please help  ???

 

<select name="initials" id="initials"  tabindex="100">
                <option value="" selected >...</option>
			<option value="Mr" >Mr</option>
                <option value="Mrs" >Mrs</option>

                <option value="Ms" >Ms</option>
                <option value="Miss" >Miss</option>
                <option value="Dr" >Dr</option>
                <option value="Rev" >Rev</option>
                <option value="Sr" >Sr</option>
              </select>

Link to comment
Share on other sites

Am trying to check if a user selected a value. if not, it shouldn't submit

 

well its going to submit regardless of php validation here.. because php cannot validate this UNLESS you submit it..

 

What I would do is use some JS for this validation and say if "initials" ==""; then alert('SELECT SOMETHING!')

 

also ID and NAME are the exact same attribute.. you can't use both..

 

try this code:

(JS)

<script>

if(document.getElementById('initials').value=="")

{

alert('Please Select Initials!');

document.getElementById('initials').focus();

}

 

</script>

Link to comment
Share on other sites

I am not that good with javascript. Please kindly show me how to validate a drop down menu using javascript.

 

Regards

 

Form validation with javascript is not a good idea.  People can just turn off javascript and happily go on their way sending your server whatever they want. You should only use javascript as a "feature" to help reduce requests to your server.  Field left blank? use javascript to give a popup or something, before the form is sent.  But check if it's blank with php just the same.

Link to comment
Share on other sites

@crayon

 

is it possible to validate a drop down menu using php. the reason am going for javascript is because ag3nt42 advised to do so.

 

Please provide help crayon on how to use php instead of javascript. Note, am processing my form on the same page

Link to comment
Share on other sites

example:

 

<?php

// your form
$form = <<<SOMEFORM
  <form action = '{$_SERVER['PHP_SELF']}' method = 'post'>
     some text <input type = 'text' name = 'sometext'>
     <input type = 'submit' value = 'submit'>
  </form>
SOMEFORM;

// if no posted vars, echo form
if(!$_POST) {
  echo $form;
// if nothing entered in field (or blank spaces) make an error msg
} elseif (!$_POST['sometext'] || trim($_POST['sometext'] == '')) {
  $error = "must fill out field.<br />";
} 

// if there's an error msg, echo it and form again
if ($error) {
  echo $error;
  echo $form;
// otherwise, form is validated
} else {
  // do something with $_POST['sometext'] here
}
?>

Link to comment
Share on other sites

@crayon

 

is it possible to validate a drop down menu using php. the reason am going for javascript is because ag3nt42 advised to do so.

 

Please provide help crayon on how to use php instead of javascript. Note, am processing my form on the same page

 

I recommend the JS if your going for PRE submit validation if you don't care when it validates then just go stricly with php and forget the JS.. you only use the JS for a bonus or not at all. Its not something to rely on.

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.