我不知道javascript是如何工作的,但我可以通過從不同的站點複製來編寫下面的代碼。javascript內部的PHP代碼
我的目標是擁有2個電臺(1 =印度,2 =除印度以外),如果選擇了除印度以外的其他國家,則會顯示一個顯示所有國家/地區名稱的下拉框。
從數據庫中選擇下拉菜單,它通過自定義php函數實現。
我可以根據我的無線電盒選擇使代碼顯示下拉菜單。
我不能做什麼如下:
- 我無法從下拉列表中選擇任何值。
- 我不能讓下拉隱藏如果我改變選擇的單選框
這裏是形式的代碼:
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<fieldset>
<br />
<script type="text/javascript">
function onclickradio(entry){
if (entry === true) {
alert("YOU HAVE CHOSEN INDIA");
}
else {
document.getElementById('OTHER THAN INDIA').innerHTML = '</br><?php dropdown('country');?>';
}
}
</script>
Country: </br>
<input onclick="onclickradio(document.getElementById('INDIA').checked);" id="INDIA" name="country" type="radio" checked="checked"/> INDIA
<input onclick="onclickradio(document.getElementById('INDIA').checked);" id="OTHER THAN INDIA" name="country" type="radio"/> OTHER THAN INDIA
<br /><br /><br />
State: <input type="text" name="State" maxlength="30"/><br />
Line1: <input type="text" name="Line1" maxlength="50" /><br />
Line2: <input type="text" name="Line2" maxlength="50" /><br />
City: <input type="text" name="City" maxlength="40" /><br />
PIN Code: <input type="text" name="PIN_Code" maxlength="8" /><br />
<input type="submit" name="submit_address" value="Submit Address" />
</fieldset>
</form>
下面是自定義代碼PHP下拉菜單功能:浮現在腦海
<?php
function dropdown($tablename) /*remember to add single quote around the input*/
{
$sql="SELECT * FROM ".$tablename;
$result=mysqli_query($GLOBALS["connection"], $sql)
or die('Error in running SELECT query');
$options=""; //initialising the variable, so that it can be concatenated later
while ($row=mysqli_fetch_array($result))
{
$x=0; /*$x is the index of the field in a row (it has to start with $x=0;
because the first index is 0 in php*/
$rowstr=" # "; /*initialising the variable,
so that it can be concatenated later*/
while ($x+1<=mysqli_num_fields($result)) /*mysqli_num_fields gives the actual number of fields, and not the index.
Since the index starts with 0, it is to be incremented by 1
before comparison with the mysqli_num_fields*/
{
$rowstr.= $row[$x]." # "; //concatenation operator
$x=$x+1;
}
$options.="<option value=".$rowstr.">".$rowstr."</option>"; //concatenation operator
}
Echo "<select>".$options."</select>";
}
?>
你必須記住PHP的事情是一個服務器端的語言,而JavaScript是一種客戶端語言 – craig1231
怎麼樣在id字段的空間?你能得到「其他不是印度」的元素嗎? –
印度/ notindia會更好,雖然它是由一個字符串包裝我不認爲它很重要 – craig1231