Jump to content

PHP Form Validation - mutually exclusive form fields


Recommended Posts

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!

 

<?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

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?

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.

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]

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

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?

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

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.