在PHP中,处理表单数据的依赖和联动可以通过以下步骤完成:
<form action="process.php" method="POST">
<label for="city">City:</label>
<select name="city" id="city">
<option value="1">New York</option>
<option value="2">Los Angeles</option>
<option value="3">Chicago</option>
</select>
<label for="state">State:</label>
<select name="state" id="state">
<!-- Options will be dynamically loaded based on the selected city -->
</select>
<input type="submit" value="Submit">
</form>
$city = $_POST['city'];
switch ($city) {
case 1:
$states = array('New York', 'New Jersey', 'Connecticut');
break;
case 2:
$states = array('California', 'Nevada', 'Arizona');
break;
case 3:
$states = array('Illinois', 'Wisconsin', 'Indiana');
break;
}
// 输出州选项
foreach ($states as $state) {
echo '<option value="' . $state . '">' . $state . '</option>';
}
foreach ($states as $state) {
echo '<option value="' . $state . '">' . $state . '</option>';
}
这样,当用户选择一个城市时,州下拉菜单将根据所选城市动态加载相应的州选项。
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站