【问题标题】:PHP If radio button selected, fill text fieldPHP 如果选择单选按钮,则填充文本字段
【发布时间】:2015-03-25 09:53:40
【问题描述】:

我一直在努力解决这个小问题,并准备把我的头发拉出来,哈哈。我更像是一名设计师而不是一名程序员,但我已经为我的学校创建了一个 php 提交表单。这是我正在处理的测试表格:

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

我想要做的是当一个人选择外部用户时,他们应该被要求填写他们的公司名称。当有人选择内部用户时,他们需要填写他们的组/PI 名称。好吧,这是行不通的。当我选择内部时,它应该只需要组/PI 名称而不是公司名称。我将不胜感激任何帮助。谢谢!

这是我的 PHP 代码:

// 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']);
    }
 }

这里是 HTML:

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

【问题讨论】:

  • $userType 在哪里确定?您是否进行过任何调试以确定代码从何处开始表现出与您预期不同的行为?
  • if ($userType == 'internal') ELSE IF ($userType == 'external') ...
  • 使var_dump($userType); 用户类型在服务器上显然不存在。您需要发送并使用$_POST 获取

标签: php button if-statement radio


【解决方案1】:

注意你的逻辑:

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

因此,如果用户选择“外部”,则“仅限内部”字段将为空,并且您会强制出错。既然用户选择了“外部”,为什么还要设置内部字段。

与您的external 检查类似,内部专用字段将为空白,您会强制另一个错误。

您需要一个else 子句将这两个if() 语句绑定在一起,以便只执行两个代码路径中的一个:

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.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 2016-08-18
    相关资源
    最近更新 更多