[英]Select html change other select html from onchange function
I have a select html which contains select sql statement. 我有一个选择html,其中包含选择sql语句。 Let's call this Select A. If select A is changed to a value, select B and select C will change dependent on the data from select A's select sql statement. 我们将其称为SelectA。如果将select A更改为一个值,则选择select B,然后select C将根据select A的select sql语句中的数据进行更改。 The method that i tried and failed is revolves around using onchange on Select A. It will then call a function. 我尝试并失败的方法是围绕在Select A上使用onchange进行的。然后它将调用一个函数。 This function however needs to read from a php variable to get the value. 但是,此函数需要从php变量读取以获取值。 But we know that PHP does not work in javascript. 但是我们知道PHP在javascript中不起作用。
Another method that i can think of is to use a window refresh function again on onchange of Select A. The refreshed page will then $_GET the value from the url link (eg: terminalassign.php?id=1&terminalposition=here&
...) But again it uses onchange and therefore unable to obtain PHP value in javascript. 我可以想到的另一种方法是在Select A发生变化时再次使用窗口刷新功能。刷新后的页面将从URL链接中获取$ _GET的值(例如: terminalassign.php?id=1&terminalposition=here&
...)但是,它再次使用onchange,因此无法在javascript中获取PHP值。
Anyone have any idea to go about this? 有人对此有想法吗?
This is my code: 这是我的代码:
The function on the onchange onchange函数
<script language="Javascript" type="text/javascript">
function terminalupdate() {
var terminalposition = <?php echo (json_encode($terminalposition)) ;?>
var terminalstatus = <?php echo (json_encode($terminalstatus)); ?>
document.getElementById("terminalposition").value=terminalposition;
document.getElementById("terminalstatus").value=terminalstatus;
}
</script>
Body 身体
<td><p id="spacing" align="left">Terminal ID</p></td>
<?php
$sql2 = "SELECT * FROM terminal WHERE merchantid IS NULL ORDER BY terminalaccountno ASC";
$RS2 = mysql_query($sql2, $db);
$rows = array();
while ($row = mysql_fetch_array($RS2))
$rows[] = $row;
?>
<td id="spacing" style="padding-left:100px">
<select name="terminalid" onchange="terminalupdate()" required>
<?php
foreach ($rows as $row) {
echo "<option value='$row[terminalid]'>$row[terminalaccountno]</option>";
$terminalstatus = $row['terminalstatus'];
$terminalposition = $row['terminalposition'];
}
?>
</select>
<?php if (!empty($merchantaddresserror)): ?><br>
<span class="spanstyle"><?php echo $merchantaddress;?></span>
<?php endif; ?><br>
</td>
</tr>
<tr>
<td><p id="spacing" align="left">Status</p></td>
<td id="spacing" style="padding-left:100px">
<select name="terminalstatus" id="terminalstatus" required>
<option value="" <?php if($terminalstatus=="") echo 'selected="selected"'?>> </option>
<option value="active" <?php if($terminalstatus=="active") echo 'selected="selected"'?>>Active</option>
<option value="inactive" <?php if($terminalstatus=="inactive") echo 'selected="selected"'?>>Inactive </option>
<option value="lost" <?php if($terminalstatus=="lost") echo 'selected="selected"'?>>Lost</option>
</select>
</td>
</tr>
<tr>
<td><p id="spacing" align="left">Terminal Position</p></td>
<td id="spacing" style="padding-left:100px">
<select name="terminalposition" required>
<option value="" <?php if($terminalposition=="") echo 'selected="selected"'?>> </option>
<option value="Deployed" <?php if($terminalposition=="Deployed") echo 'selected="selected"'?>>Deployed</option>
<option value="Used By Developer" <?php if($terminalposition=="Used By Developer") echo 'selected="selected"'?>>Used By Developer</option>
<option value="Sent For Repair" <?php if($terminalposition=="Sent For Repair") echo 'selected="selected"'?>>Sent For Repair</option>
<option value="Write-Off" <?php if($terminalposition=="Write-Off") echo 'selected="selected"'?>>Write-Off</option>
<option value="Stolen" <?php if($terminalposition=="Stolen") echo 'selected="selected"'?>>Stolen</option>
</select>
So this is how things can be easier with the help of Ajax 因此,借助Ajax,事情可以变得更轻松
First call the JavaScript function on change of first dropdown, which contains ajax call. 第一次更改下拉列表时首先调用JavaScript函数,其中包含ajax调用。
function loadData() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// populate both dropdown using result of getData.php file
// You can acccess the values using this.responseText.dropdown2 and this.responseText.dropdown3
console.log(this.responseText);
}
};
xhttp.open("GET", "getData.php?param=abc", true);
xhttp.send();
}
In getData.php
file :- 在getData.php
文件中:-
<?php
$param = $_GET['param']; // will give you 'abc'
//mysql queries goes here and get result for second and third dropdown
//then echo the result of the query.
echo array('dropdown2'=>$dropdown2, 'dropdown3'=>$dropdown3);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.