tags:

views:

96

answers:

2

I'm sorry if this is a very simplistic question but I'm still learning the ropes of PHP.

I'm writing a mail script in PHP that takes the contents of a form and sends it out to one of two emails. I had it working perfectly but the person for whom I'm creating it came back with some edits and now I'm struggling.

Essentially, there are two sets of radio button where if "Yes" is checked another "additional information" field also needs to be filled in. If "No" is check, the other field can remain blank.

This is what I have so far:

if ($var1 == "String" AND $var2 =="")
{
    echo("Fill in the blah field");
}
elseif ($var3 == "Yes" AND $var4 == "") 
{
    echo ("Fill in the blah blah field");
}
elseif ($var1 !="" AND $var2 !="" AND $var7 !="")
{
    mail(....)
    echo(....)
}

I know there has to be a better way to first check if one set validates, then if the other does, and then if all the required fields are filled in.... Currently when I submit the form all I get is a blank screen.

Thanks for your help!

+1  A: 

I'm not sure if this is actually an error in your code or just in copying and pasting for this post, but try closing your quotes.

echo("Fill in the blah field");
echo ("Fill in the blah blah field");
froadie
Oops, I think it was just a transposition error. Thanks.
Vecta
+1  A: 

Your description and the code don't seem related to me, but I'm confused by the variables named 'var' and the 'blah' fields. But, based on your description, maybe this will help you.

$set_2_required = !empty($_GET['radio_set_1'] && $_GET['radio_set_1'] == 'yes';

if ($set_2_required && empty($_GET['radio_set_2'])){
    echo 'ERROR: You must fill out radio set 2.';
} else {
    // Send your mail.
}

EDIT: I think my previous comment has all the logic pieces of what you need, but maybe this will be closer to what you'd actually write.

// With these ternary operators, you logic further down can rely on a NULL
// value for anything that's not set or an empty string.
$dropdown_1 = !empty($_GET['dropdown_1']) ? $_GET['dropdown_1'] : NULL;
$dropdown_2 = !empty($_GET['dropdown_2']) ? $_GET['dropdown_2'] : NULL;
$field_1 = !empty($_GET['field_1']) ? $_GET['field_1'] : NULL;
$field_2 = !empty($_GET['field_2']) ? $_GET['field_2'] : NULL;

// This 'valid' variable lets you avoid nesting and also return multiple errors
// in the request.
$valid = TRUE;
if (!$field_1 && $dropdown_1 == '<string makes field required>'){
    echo 'ERROR: Field 1 is required for this dropdown selection.';
    $valid = FALSE;
}
if (!$field_2 && $dropdown_2 == '<string makes field required>'){
    echo 'ERROR: Field 2 is required for this dropdown selection.';
    $valid = FALSE;
}

// A final check if the logic gets complicated or the form on the front end
// wants to check one thing to determine pass/fail.
if (!$valid){
    echo 'ERROR: One or the other fields is required.';
} else {
    // Everything's fine, send the mail.
}
mqsoh
Sorry, now that I look back it is confusing. Var1 is a value from a dropdown menu. Of all of the values, if 1 of them is selected var2 has contain something. If any of the other options in the dropdown is selected it doesn't matter. So "String" is referring to the value of the dropdown option. The 'blahs' are referring to the particular field that needs to be filled in. There are two of them.
Vecta