joetaylor Posted May 10, 2013 Share Posted May 10, 2013 ok, after hours trying to figure this out myself, I thought it was about time I joined a php community anyway. I built a html web form. It uses a php script to check for required fields and send the e-mail if it passes. Just checking for a single field is filled, or not, is pretty straightforward and works like this.. if(empty($dob)) { $error_message .= 'Error: dob required'; } if(empty($dayphone)) { $error_message .= 'Error: day phone required'; } But I have a couple situations in the form where I need to only require another field if another field (or radio) is filled. There's a field for Mobile Phone. Under that I have two radio buttons that answer Allow Texts? yes/no <label for="mobphone"> Mobile Phone: </label> <input type="text" name="mobphone" id="fields"><br> </div> <div id="form-radio"> <label for="texts"> Allow Texts? </label> <input type="radio" name="texts" value="yes">Yes <input type="radio" name="texts" value="no">No </div> This was my best try... if(isset($mobphone) && empty($texts)) { $error_message .= 'Error: Allow Texts yes or no required'; } ... and now, you probably know my level of php experience. ..I'm learning. lol Am I on the right track? Is it possible to do this? a Better way? The other fields with the same scenario are similar,.. they aren't radio buttons. They are drop down or text fields. Thanks for any help in advance! ~Joe Link to comment https://forums.phpfreaks.com/topic/277903-php-form-only-require-if-another-field-is-filled/ Share on other sites More sharing options...
Psycho Posted May 10, 2013 Share Posted May 10, 2013 This was my best try... if(isset($mobphone) && empty($texts)) { $error_message .= 'Error: Allow Texts yes or no required'; } Well, I assume you are SETTING $mobphone using something like $mobphone = $_POST['mobphone']; So, it would always be set. You should check that it has a value. You can use empty(), but that has some downsides. For example a value of '0' will be considered empty. But, using the same logic as you have now, you could do this if(!empty($mobphone) && empty($texts)) { $error_message .= 'Error: Allow Texts yes or no required'; } Link to comment https://forums.phpfreaks.com/topic/277903-php-form-only-require-if-another-field-is-filled/#findComment-1429617 Share on other sites More sharing options...
joetaylor Posted May 10, 2013 Author Share Posted May 10, 2013 Well, I assume you are SETTING $mobphone using something like $mobphone = $_POST['mobphone']; Correct. And.. I think you just gave me the one scenario I didn't try! It seems to work after a few tests. Thank you so much! Link to comment https://forums.phpfreaks.com/topic/277903-php-form-only-require-if-another-field-is-filled/#findComment-1429619 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.