0
votes

I am very new to PHP but am struggling to learn. I have created a submit form for my school website, and in the form I have a group of radio buttons. See below:

*Account Fund: <input type="radio" name="accfnd" value="nonprofit" <?php 
echo $nonprofitChecked; ?>>

Academic/Non-Profit <input type="radio" 
name="accfnd" value="commercial" <?php echo $commercialChecked; ?>>

Commercial <input type="radio" name="accfnd" value="uc" <?php echo $ucChecked; ?>>

UC  <input type="text" size="40" name="ucfund" 
value="<?php if(isset($_POST['ucfund'])) echo $_POST['ucfund'];?>" />
<?php if($ucfundError != '') { ?>
    <span class="error">
    <?=$ucfundError;?>
    </span>
<?php } ?>

When the radio button with the value="uc" is checked, the text box next to it is required to be filled out otherwise they will get an error message. For the most part this is working fine, what I am having problem with is that I am getting the error message requiring the text message be filled even though I have one of the other radio buttons checked. The only time the text box is required is when the uc radio button is checked. I hope I am making sense.

Below are the php codes. I would appreciate any help. Thank you.

$accfnd = $_POST['accfnd']; 

if (isset($_GET['uc'])){  
    if ($_GET['accfnd'] == 'uc'){ $ucChecked = ' checked="checked" '; }  
} else if (trim($_POST['ucfund']) === '') { 
    $ucfundError = '<span class="error">Account fund is required for UCI users.</span>'; 
    $hasError = true; 
} else { 
    $ucfund = trim($_POST['ucfund']); 
}  

$body = " Account Fund: $accfnd \n\n UCI Account Fund: $ucfund";  
2
how are you submiting the data? I couldn't see the form. Are you send it by GET or POST? - Jose Areas

2 Answers

0
votes

I don't see a form element with the name uc; your code can also be simplified to

$accfnd = isset($_POST['accfnd']) ? $_POST['accfnd'] : '';
$ucfund = isset($_POST['ucfund']) ? $_POST['ucfund'] : '';

if ($accfnd == 'uc') {
    $ucChecked = ' checked="checked" '; 

    if (trim($ucfund) == '') {
        $ucfundError = '<span class="error">Account fund is required for UCI users.</span>'; 
        $hasError = true; 
    }
}

$body = " Account Fund: $accfnd \n\n UCI Account Fund: $ucfund";  
0
votes

As Jose alluded to, you are using both $_GET and $_POST form data variables interchangeably.

The best solution is to use $_REQUEST[] everywhere you might use $_GET[] or $_POST[]. $_REQUEST is a merged superglobal containing all submitted form data whether POST or GET. It is essentially $_GET and $_POST combined (along with $_COOKIE). Further information/clarification here in the manual.

Secondly, and the problem you asked about, is comparing data. You had multiple nested if() and elseif() statements that were never executed, because $_GET['uc'] does not exist.

$accfnd = $_REQUEST['accfnd']; 

if ( $accfnd == 'uc'){  // Commercial selected, text box required
    $ucChecked = ' checked="checked" ';
    if (trim($_REQUEST['ucfund']) === '') { 
        $ucfundError = '<span class="error">Account fund is required for UCI users.</span>'; 
        $hasError = true; 
    }
} else { 
    $ucfund = trim($_REQUEST['ucfund']); 
}  

$body = " Account Fund: $accfnd \n\n UCI Account Fund: $ucfund";