[英]Track number of times radio button is selected and store in text file for PHP
[英]PHP If radio button selected, fill text field
我一直在努力解決這個小問題,並且准備把我的頭發拉出來哈哈。 我更像是一名設計師而不是一名程序員,但我已經為我的學校創建了一個 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 <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 } ?> - <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 <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 } ?> - <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>
注意你的邏輯:
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.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.