aeris7 Posted January 28, 2009 Share Posted January 28, 2009 I have a PHP form that is sent to a MySQL database on submit and am having trouble with some validation. I have a text field ('captain_store') and a dropdown box/listmenu ('national_site'), and users must either fill out the text field OR select an option from the dropdown box - not both, and not neither. I've worked out that the script needs to be along the lines of the following, but have no idea how to actually write it. Any help or at least a point in the right direction? I cannot find the answer to this ANYWHERE! It should go something like this (and yes I know this is not written in any kind of code form): If captain_store and national_site both have no value on submit, do not send: Display warning "Please choose a site or National Office" If captain_store and national_site both have values on submit, do not send: Display warning: "Please choose either a site or a National Office. You cannot select both." If captain_store has a value and national_site is empty, send. If national_site has a value and captain_store is empty, send. I really want to be able to do this with server-side validation, but at this stage would even be happy with some java for client-side if it's all I can get. THANKS TO ANYONE WHO CAN HELP! Quote Link to comment https://forums.phpfreaks.com/topic/142724-php-form-validation-mutually-exclusive-form-fields/ Share on other sites More sharing options...
chronister Posted January 28, 2009 Share Posted January 28, 2009 <?php if(empty($_POST['field1']) && empty($_POST['field2'])) { echo 'Error: Must fill in at least 1 or the other'; }else{ if(!empty($_POST['field1']) && !empty($_POST['field2'])){ echo 'Error: Must not fill out both'; }else{ // send now. } } ?> I think this would take care of what you need. It looks to see if they are both blank, or both filled out. If both hit the else statement, then at least 1 has to be filled out. I think my logic is correct on that one. Nate Quote Link to comment https://forums.phpfreaks.com/topic/142724-php-form-validation-mutually-exclusive-form-fields/#findComment-748150 Share on other sites More sharing options...
.josh Posted January 28, 2009 Share Posted January 28, 2009 Your code doesn't really account for which one to use. Which brings me to this question: Is this data being inserted into the same column, or do you have 2 columns, but one of them must be empty, the other one filled? Quote Link to comment https://forums.phpfreaks.com/topic/142724-php-form-validation-mutually-exclusive-form-fields/#findComment-748152 Share on other sites More sharing options...
aeris7 Posted January 28, 2009 Author Share Posted January 28, 2009 Your code doesn't really account for which one to use. Which brings me to this question: Is this data being inserted into the same column, or do you have 2 columns, but one of them must be empty, the other one filled? Yes there are two columns - both set to NULL in MySQL so both can be empty - hence why I need the validation, because I don't want both empty. *** Nate: Thanks, I will try that out and see if it works. Quote Link to comment https://forums.phpfreaks.com/topic/142724-php-form-validation-mutually-exclusive-form-fields/#findComment-748158 Share on other sites More sharing options...
aeris7 Posted January 28, 2009 Author Share Posted January 28, 2009 Ok, I have tried that and this is the error I'm getting: Parse error: syntax error, unexpected '!' in C:\htdocs\admin\admin_addcaptain.php on line 596 But both '!' in the code look correct as they make empty NOT empty? If it helps, I put the code just outside the table cell that holds the drop down box, as that is where I want the error message to appear. I also removed the curly braces around the 'send now' comment, plus the second else... or am I supposed to put the whole code up the top inside the insert record, and wrap the second else statement around... what part? I've attached the Insert record code below. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/142724-php-form-validation-mutually-exclusive-form-fields/#findComment-749007 Share on other sites More sharing options...
chronister Posted January 28, 2009 Share Posted January 28, 2009 You can put this code wherever you wish the message to appear. I have looked over it and tested it on my server, what I posted is correct with no syntax errors. I would need to see yours to know what the problem is. Sample.php is not the code referenced in the errror. <?php if(empty($_POST['field1']) && empty($_POST['field2'])) { echo 'Error: Must fill in at least 1 or the other'; }else{ if(!empty($_POST['field1']) && !empty($_POST['field2'])){ echo 'Error: Must not fill out both'; } } ?> Nate Quote Link to comment https://forums.phpfreaks.com/topic/142724-php-form-validation-mutually-exclusive-form-fields/#findComment-749041 Share on other sites More sharing options...
aeris7 Posted January 29, 2009 Author Share Posted January 29, 2009 My mistake - I deleted one too many closing curly braces. It works perfectly now, but because I don't have an isset statement, the first warning shows up on the page straight away? Is there an easy way to have it pop up only after the form has been submitted? Quote Link to comment https://forums.phpfreaks.com/topic/142724-php-form-validation-mutually-exclusive-form-fields/#findComment-749087 Share on other sites More sharing options...
chronister Posted January 29, 2009 Share Posted January 29, 2009 Yeah, anytime you are referencing form values, you want to ensure that the form is actually submitted first. I generally make the errors an array, so I can display them individually, and I make their key the name of the field they apply to. This allows me to apply class styles to a particular form field, and also fill in the fields again so a user does not have to fill them out again. <?php if(isset($_POST['submitButonName'])){ if(empty($_POST['field1']) && empty($_POST['field2'])) { $error = 'Error: Must fill in at least 1 or the other'; }else{ if(!empty($_POST['field1']) && !empty($_POST['field2'])){ error= 'Error: Must not fill out both'; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142724-php-form-validation-mutually-exclusive-form-fields/#findComment-749154 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.