Jump to content


Photo

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


  • Please log in to reply
2 replies to this topic

#1 joetaylor

joetaylor

    Member

  • Members
  • PipPip
  • 11 posts

Posted 10 May 2013 - 06:11 PM

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

 

 



#2 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,809 posts
  • LocationCanada

Posted 10 May 2013 - 06:28 PM

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';
}

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#3 joetaylor

joetaylor

    Member

  • Members
  • PipPip
  • 11 posts

Posted 10 May 2013 - 06:45 PM

 

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, 10 May 2013 - 06:45 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com