我有一个选择的html,其中包含SELECT SQL语句。让我们调用此选择A。如果选择A已更改为值,则选择B和SELECT C将更改取决于Select A的SELECT SQL语句的数据。我尝试和失败的方法是围绕在选择A上使用Onchange的旋转。然后将调用一个函数。但是,此功能需要从PHP变量读取以获取值。但是我们知道PHP在JavaScript中不起作用。
我可以想到的另一种方法是在选择A的Onchange上再次使用窗口刷新函数。然后,刷新页面将$ _从URL链接中获取值(例如:terminalassign.php?id=1&terminalposition=here&
...(但是它再次使用Onchange,因此无法在JavaScript中获得PHP值。
有人有任何想法吗?
这是我的代码:
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>
身体
<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>
,因此,借助 ajax
,这就是如何更容易的事情第一个调用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();
}
在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);
?>