我一直在尝试为我的游戏社区设置提交表单。我们在游戏中跟踪船只,并且我已经获得了形状。它现在正确地向数据库提交信息;但是,我试图找到一种方法来减少第二个下拉框依赖于第一个。例如,我提供了我正在使用的表单的代码,其中有5个字段;
Ship ID,
Ship Class,
Ship Type,
Ship Owner,
Ship Name.
船舶类别和船舶类型是下拉框。
我希望将它们级联,以便例如
I choose Battleship the second drop down would populate
with only Battleship 1, Battleship 2, and Battleship 3 and not the rest of fields
in the second drop down.
我已经搜索了谷歌寻找解决方案但它似乎在逃避我,我是否正确假设第二次下拉必须基于关系表?此外,是否需要单独的查询才能这样做?
<?php
/* submit the application */
/* get today's date */
$today = getdate();
/* build the insert query that's going to be used */
$insert = "INSERT INTO ship_registry ( shipID, shipClass, shipType, shipOwner, shipName ) ";
$insert.= "VALUES ( %s, %s, %s, %s, %s ) ";
/* run the query through sprintf and the safety function to scrub for security issues */
$query = sprintf(
$insert,
escape_string( $_POST['shipID'] ),
escape_string( $_POST['shipClass'] ),
escape_string( $_POST['shipType'] ),
escape_string( $_POST['shipOwner'] ),
escape_string( $_POST['shipName'] ),
escape_string( $today[0] )
);
/* run the query */
$result = mysql_query( $query );
/* close the if statement on submitting the application */
?>
<div class="body">
<?
$check = new QueryCheck;
$check->checkQuery( $result, $query );
if( !empty( $check->query ) ) {
$check->message( "entry", "submit" );
$check->display();
}
?>
<span class="fontTitle">Submit Ship</span>
<br /><br />
<table>
<form method="post" action="<?=$webLocation;?>index.php?page=addship">
<tr>
<td class="tableCellLabel">Ship ID</td>
<td> </td>
<td><input id="product" type="text" class="image" maxlength="7" name="shipID" /></td>
</tr>
<tr>
<td class="tableCellLabel">Ship Class</td>
<td> </td>
<td>
<select name="shipClass">
<option value="Battleship">Battleship</option>
<option value="Battlecruiser">Battlecruiser</option>
<option value="Cruiser">Cruiser</option>
<option value="Destroyer">Destroyer</option>
<option value="Frigate">Frigate</option>
</select>
</td>
</tr>
<tr>
<td class="tableCellLabel">Ship Type</td>
<td> </td>
<td>
<select name="shipType">
<option value="Battleship 1">Battleship 1</option>
<option value="Battleship 2">Battleship 2</option>
<option value="Battleship 3">Battleship 3</option>
<option value="Battlecruiser 1">Battlecruiser 1</option>
<option value="Battlecruiser 2">Battlecruiser 2</option>
<option value="Battlecruiser 3">Battlecruiser 3</option>
<option value="Cruiser">Cruiser</option>
<option value="Cruiser">Cruiser</option>
<option value="Cruiser">Cruiser</option>
<option value="Destroyer 1">Destroyer 1</option>
<option value="Destroyer 2">Destroyer 2</option>
<option value="Destroyer 3">Destroyer 3</option>
<option value="Frigate 1">Frigate 1</option>
<option value="Frigate 2">Frigate 2</option>
<option value="Frigate 3">Frigate 3</option>
</select>
</td>
</tr>
<tr>
<td class="tableCellLabel">Ship Owner</td>
<td> </td>
<td><input type="text" class="image" maxlength="64" name="shipOwner" /></td>
</tr>
<tr>
<td class="tableCellLabel">Ship Name</td>
<td> </td>
<td><input type="text" class="image" maxlength="64" name="shipName" /></td>
</tr>
<tr>
<td colspan="2"></td>
<td>
<strong class="yellow">Please make sure all information is entered correctly before submitting.</strong></b><br />
</td>
</tr>
<tr>
<td colspan="3" height="10"></td>
</tr>
<tr>
<td colspan="2"></td>
<td><input type="image" src="<?=$webLocation;?>skins/<?=$skinChoice;?>/buttons/submit.png" name="action" class="button" value="Submit" /></td>
</tr>
</form>
</table>
</div> <!--Close the div body class tag-->
答案 0 :(得分:0)
如果你不介意额外的往返调用,那么选择ajax(使用jQuery或类似方法使其更容易)将会很好地工作。
但是,我可能会在PHP构建期间在JS中生成一个简单的多维数组,并在Ship Class更改事件中添加一些JS,该事件根据该数组中的信息重新填充ship类型。比ajax调用复杂一点,但避免了额外的服务器调用。