nitation Posted June 18, 2008 Share Posted June 18, 2008 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> Quote Link to comment Share on other sites More sharing options...
ag3nt42 Posted June 18, 2008 Share Posted June 18, 2008 what exactly are you trying to validate here? Quote Link to comment Share on other sites More sharing options...
nitation Posted June 18, 2008 Author Share Posted June 18, 2008 Am trying to check if a user selected a value. if not, it shouldn't submit Quote Link to comment Share on other sites More sharing options...
.josh Posted June 18, 2008 Share Posted June 18, 2008 Okay so you told us what you're trying to do, but you didn't tell us what the problem is, and you didn't show us any code. Quote Link to comment Share on other sites More sharing options...
nitation Posted June 18, 2008 Author Share Posted June 18, 2008 i was trying with this. <?php if(empty($surname)){ $msg='Check Lastname Error'; }?> But its not working. Can you show me a way to validate it ??? Quote Link to comment Share on other sites More sharing options...
ag3nt42 Posted June 18, 2008 Share Posted June 18, 2008 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> Quote Link to comment Share on other sites More sharing options...
nitation Posted June 18, 2008 Author Share Posted June 18, 2008 do you mean i can not test to see if a user selected from the options in php?? Quote Link to comment Share on other sites More sharing options...
ag3nt42 Posted June 18, 2008 Share Posted June 18, 2008 no you can test for it but you MUST SUBMIT the form in order to run php code on it to validate BEFORE SUBMIT you cannot use php You'll need to use JS Quote Link to comment Share on other sites More sharing options...
nitation Posted June 18, 2008 Author Share Posted June 18, 2008 I am not that good with javascript. Please kindly show me how to validate a drop down menu using javascript. Regards Quote Link to comment Share on other sites More sharing options...
ag3nt42 Posted June 18, 2008 Share Posted June 18, 2008 try this code: (JS) <script> if(document.getElementById('initials').value=="") { alert('Please Select Initials!'); document.getElementById('initials').focus(); } </script> Quote Link to comment Share on other sites More sharing options...
.josh Posted June 18, 2008 Share Posted June 18, 2008 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. Quote Link to comment Share on other sites More sharing options...
ag3nt42 Posted June 18, 2008 Share Posted June 18, 2008 very good advice Quote Link to comment Share on other sites More sharing options...
nitation Posted June 18, 2008 Author Share Posted June 18, 2008 @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 Quote Link to comment Share on other sites More sharing options...
lilwing Posted June 18, 2008 Share Posted June 18, 2008 Why do it with javascript, when you can do it with CSS? http://www.alistapart.com/articles/hybrid Quote Link to comment Share on other sites More sharing options...
.josh Posted June 18, 2008 Share Posted June 18, 2008 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 } ?> Quote Link to comment Share on other sites More sharing options...
ag3nt42 Posted June 18, 2008 Share Posted June 18, 2008 Why do it with javascript, when you can do it with CSS? http://www.alistapart.com/articles/hybrid i'm pretty sure your confusing a JavaScript code snippet with css. if you read it, it says Their snippet of JavaScript looks like this: unless i'm missing something Quote Link to comment Share on other sites More sharing options...
ag3nt42 Posted June 18, 2008 Share Posted June 18, 2008 @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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.