【问题标题】:Select html change other select html from onchange function选择 html 更改其他 从 onchange 函数中选择 html
【发布时间】:2018-02-22 06:57:03
【问题描述】:

我有一个包含 select sql 语句的 select html。我们称它为 Select A。如果将 select A 更改为一个值,则 select B 和 select C 将根据 select A 的 select sql 语句中的数据进行更改。 我尝试并失败的方法是围绕在 Select A 上使用 onchange 进行的。然后它将调用一个函数。但是,此函数需要从 php 变量中读取以获取值。但是我们知道 PHP 在 javascript 中是行不通的。

我能想到的另一种方法是在选择 A 的 onchange 时再次使用窗口刷新功能。然后刷新的页面将 $_GET 来自 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 调用 SelectA 的变化来更新 Select B 和 C 的值。在这种情况下你不需要担心 get 和其他人
  • 就我而言,如何应用 ajax 调用?
  • 同意@Naincy Ajax 调用将是一个更好的选择,而不是这个......
  • 如何让 ajax 调用适合我的情况?
  • @AnonymousVagrant 您正在调用 js 函数 onChange 代替它可以进行 ajax 调用并实现您想要的功能。

标签: javascript php mysql


【解决方案1】:

所以这就是在 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);
?>

【讨论】:

  • 为什么 this.readyState == 4 && this.status == 200?参数可以像merchantid=$merchantid这样由用户定义吗?
  • 即使我使用this.responseText.dropdown2 访问这些值,我如何将它放回document.getElementById("terminalposition").value=terminalposition; 我是否使用这个:document.getElementById("terminalposition").this.responseText.dropdown2;抱歉,这个 AJAX 让我很困惑
  • this.readyState == 4this.status == 200 是您的 http 请求的状态,这意味着如果请求成功执行,则执行此操作....
  • 好的,我如何将this.responseText.dropdown2 分配给document.getElementById("terminalposition").value=terminalposition; ?我试过document.getElementById("terminalposition").this.responseText.dropdown2 但它不起作用
  • 我也试过document.getElementById("terminalposition").innerHTML = this.responseText.dropdown2;,这似乎是网上可能的解决方法。但不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-13
  • 2021-09-25
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多