简体   繁体   中英

PHP If radio button selected, fill text field

I have been trying to figure this little problem out and am about ready to pull my hair out lol. I am more of a designer than I am a programmer, but I have created a submit form in php for my school. Here is the test form I am working on:

http://www.inrf.uci.edu/php-form-test/

What I am trying to do is when a person selects External user, they should be required to fill out their Company Name. When someone selects Internal user, they are required to fill out their Group/PI name. Well this isn't working. When I select Internal, it's supposed to require only Group/PI name and not Company name. I would appreciate any assistance. Thank you!

Here is my PHP code here:

// internal selected (group pi)

if ($userType == 'internal') {
    $internalChecked = ' checked="checked" ';
} else {
    if(trim($_POST['grpPI']) === '') {
        $grpPIError = '<span class="error">Please enter your group or PI name.</span>';
        $hasError = true;
    } else {
        $grpPI = trim($_POST['grpPI']);
    }
}

    }

// external selected (company name)

 if ($userType == 'external') {
    $externalChecked = ' checked="checked" ';
 } else {
    if(trim($_POST['companyName']) === '') {
        $companyNameError = '<span class="error">Please enter your company name.</span>';
        $hasError = true;
    } else {
        $companyName = trim($_POST['companyName']);
    }
 }

and HTML here:

<table class="usertype" id="usertype">   

                         <tr>
                           <td colspan="2">
                             <input type="radio" name="userType" value="external" <?php echo $externalChecked; ?>>External 
                             <select name="externalSelect" size="1">
                               <option selected="selected">Select</option>
                               <option>Academic</option>
                               <option>Non-Profit</option>
                               <option>Commercial</option>
                           </select><?php if($externalError != '') { ?><span class="error"><?=$externalError;?></span><?php } ?></td>
           </tr>
                         <tr>
                           <td colspan="2"><label for="companyName">Company Name:</label> 
                           <input type="text" size="40" name="companyName" id="companyName" value="<?php if(isset($_POST['companyName'])) echo $_POST['companyName'];?>" />
                <?php if($companyNameError != '') { ?>
                <span class="error"><?=$companyNameError;?></span><?php } ?></td>
                         </tr>
                         <tr>
                           <td colspan="3"><input type="radio" name="userType" value="internal" <?php echo $internalChecked; ?>>Internal &nbsp;&nbsp;&nbsp;&nbsp;<input title="Enter Account String. Required for Internal Users." type="text" size="30" name="ucfund" value="<?php if(isset($_POST['ucfund'])) echo $_POST['ucfund'];?>" placeholder="KFS or Account (eg. xxxxxxx)" /><?php if($ucfundError != '') { ?><span class="error"><?=$ucfundError;?></span><?php } ?>&nbsp;-&nbsp; <input title="Enter Fund String. Required for Internal Users." type="text" size="25" name="ucfund2" value="<?php if(isset($_POST['ucfund2'])) echo $_POST['ucfund2'];?>" placeholder="Fund (eg. xxxxx)" /><?php if($ucfund2Error != '') { ?><span class="error"><?=$ucfund2Error;?></span><?php } ?><br /><span class="small">(Please enter KFS number or Accound Fund)</span>
</td>
                         </tr>

                         <tr>
                           <td colspan="3">Optional&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" size="30" name="sub" value="<?php if(isset($_POST['sub'])) echo $_POST['sub'];?>" placeholder="Sub" /><?php if($subError != '') { ?><span class="error"><?=$subError;?></span><?php } ?>&nbsp;-&nbsp; <input type="text" size="25" name="proj" value="<?php if(isset($_POST['proj'])) echo $_POST['proj'];?>" placeholder="Project" /><?php if($projError != '') { ?><span class="error"><?=$projError;?></span><?php } ?>
</td>
                         </tr>
                         <tr>
                           <td colspan="3"><label for="grpPI">Group / PI:</label> 
                           <input type="text" size="40" name="grpPI" id="grpPI" value="<?php if(isset($_POST['grpPI'])) echo $_POST['grpPI'];?>" />
                <?php if($grpPIError != '') { ?>
                <span class="error"><?=$grpPIError;?></span><?php } ?></td>
                </tr>

      </table>

Note your logic:

if ($user == 'internal') {
   ...
} else {
   ... check for empty field ...
}

So if a user select 'external', then the "internal-only" field will be empty, and you force an error. why should the internal-only field be set since the user selected "external".

Similarly for your external check, the internal-only field will be blank and you force another error.

You need an else clause to tie those two if() statements together, so that only ONE of the two code paths executes:

if ($user == 'internal') {
   ... do internal-only checks ...
} else if ($user == 'external') {
   ... do external-only checks ...
} else {
   ... $user is undefined type, "zomg how'd you get here" error handling.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM