Jump to content

php form, only require if another field is filled..


joetaylor

Recommended Posts

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.   :blink:  ..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
Share on other sites

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
Share on other sites

 

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.  :facepalm:

 

Thank you so much!  

Edited by joetaylor
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.