在JavaScript中传递echo PHP

时间:2019-03-28 09:42:51

标签: javascript php

根据问题,我在javscript中实现了动态形式,该条件要求选择中选择的第一个选项必须与新选择中的另一个选项匹配:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Documento senza titolo</title>
        <script>
            function random_function()
            {
                var a=document.getElementById("input").value;
                if(a==="INDIA")
                {
                    var arr=["Maharashtra","Delhi"];
                }
                else if(a==="USA")
                {
                    var arr=["Washington","Texas","New York"];
                }

                var string="";

                for(i=0;i<arr.length;i++)
                {
                    string=string+"<option>"+arr[i]+"</option>";
                }
                string="<select name='any_name'>"+string+"</select>";
                document.getElementById("output").innerHTML=string;
            }
        </script>
</head>
    <body>
        <select id="input" onchange="random_function()">
            <option>select option</option>
            <option>INDIA</option>
            <option>USA</option>
        </select>
        <div id="output">

        </div>
    </body>
</html>

到目前为止,没有什么奇怪的,但是现在我想知道,以前已经创建了一个数据库,如何以静态方式代替城市来实现<?php echo $row_regioni['regione']; ?>

<select>
<?php do { ?>
<option>
<?php echo $row_regioni['regione']; ?>
</option>
<?php } while ($row_regioni = mysqli_fetch_assoc($regioni)); ?>
</select>
</form>

2 个答案:

答案 0 :(得分:0)

html的代码如下。

<select>
    <?php
    while(($row = mysqli_fetch_assoc($regioni)) !== null){
        print(
            "<option value='".htmlentities($row['regione'])."'>".
                htmlentities($row['regione']).
            "</option>"
         );
    }
    ?>
</select>

要将其传递给javascript,有多种解决方案。最简单的方法(由于将javascript与php混淆而无法选择)是:

<?php
    $regions = array();
    while(($row = mysqli_fetch_assoc($result)) !== null){
        array_push($regions, '"'.str_replace('"', '\\"', $row['regione']).'"');
    }
?>
<script>regions = [<?php print(implode(",\n\t", $regions)); ?>];</script>

这将设置数组regions,然后可以在javascript中访问它。您可以将js代码修改为:

function random_function(){
    // ... 
    var string = "";
    for(var i = 0; i < regions.length; i++){
        string += "<option value='" + regions[i] + "'>" + regions[i] + "</option>";
    }
    string = "<select name='any_name'>" + string + "</select>";
    //...
}

您的示例如下所示:

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Documento senza titolo</title>
    <?php
        $regions = array();
        while(($row = mysqli_fetch_assoc($result)) !== null){
            array_push($regions, '"'.str_replace('"', '\\"', $row['regione']).'"');
        }
    ?>
    <script>regions = [<?php print(implode(",\n\t", $regions)); ?>];</script>
    <script>
        function random_function(){
          var a = document.getElementById("input").value;
          var arr;
          
          if(a === "INDIA"){
            arr = ["Maharashtra", "Delhi"];
          }
          else if(a === "USA"){
            arr = ["Washington", "Texas", "New York"];
          }
          
          var options = "";
          
          for(var i = 0; i < regions.length; i++){
            options += "<option value='" + regions[i] + "'>" + regions[i] + "</option>";
          }
          
          var select ="<select name='any_name'>" + options + "</select>";
          document.getElementById("output").innerHTML = select;
        }
      </script>
  </head>
  <body>
    <select id="input" onchange="random_function()">
      <option>select option</option>
      <option>INDIA</option>
      <option>USA</option>
    </select>
    <div id="output">

    </div>
  </body>
</html>

我更喜欢用php创建select。如果这不可能,我将使用ajax请求从数据库中获取值。但是我假设您对js不太熟悉,所以我不会为此发布代码。那是先进的技术。

答案 1 :(得分:0)

您可以这样做

<select id="input">
  <option value="">select option</option>
  <?php foreach(mysqli_fetch_assoc($regioni) as $region){ ?>
    <option value="<?php echo $region['regione']; ?>"><?php echo $region['regione']; ?></option>
  <?php } ?>
</select>

如果需要为选择器设置默认值,则可以与一些变量或字符串进行比较

<option value="<?php echo $region['regione']; ?>"<?php ($region['regione'] == 'INDIA') ? echo 
 ' selected="selected"' : '' ?>><?php echo $region['regione']; ?></option>