drelgkxl93433 2017-06-03 07:26
浏览 112
已采纳

使用HTML变量调用PHP函数

When the combobox is select, i'm trying to display first n items from database

  • i'm trying to call the PHP function "produse($_POST)" with the value of the selected item in the select tag, the code is correct?
  • the limit in the SELECT SQL can be the one in the code? ("$sql = "SELECT * FROM table LIMIT $number";")

<body>

    <select name="n" onchange="document.write('<?php produse($_POST); ?>')">
        <option disabled selected value> -- select an option -- </option>
        <option value=2>2</option>
        <option value=3>3</option>
        <option value=4>4</option>
    </select>

    <br> <br> <br>

    <?php
        function produse($number){

            $servername = "localhost:3306";
            $username = "root";
            $password = "";
            $dbname = "produse";

            // Create connection
            $conn = mysqli_connect($servername, $username, $password, $dbname);
            // Check connection
            if (!$conn) {
                die("Connection failed: " . mysqli_connect_error());
            }
            $number = intval($_GET['number']);
            $sql = "SELECT * FROM table LIMIT $number";
            $result = mysqli_query($conn, $sql);

            if (mysqli_num_rows($result) > 0) {
            // output data of each row
                while($row = mysqli_fetch_assoc($result)) {
                    echo "id_produs: " . $row["id_produs"]. " - Denumire: " . $row["Denumire"]. " - Pret " . $row["Pret"]. " - Descriere" . $row["Descriere"] ."<br>";
                }
            } else {
                echo "0 results";
            }

            mysqli_close($conn);
        }
    ?>  


</body>
  • 写回答

1条回答 默认 最新

  • douqi3195 2017-06-03 08:00
    关注

    Use it separately using jQuery like this:

    HTML (html_page.html) :

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <title>Your HTML Page title</title>
      </head>
      <body>
        <div>
          <select id="my_select">
            <option value="" selected>Choose</option>
            <option value="1">Option1</option>
            <option value="2">Option2</option>
          </select>
        </div>
        <hr />
        <div class="dynamic_content"></div>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script>
          $('#my_select').change(function() {
            showResult($(this).val());
          });
    
          function showResult(value) {
    
            if (value) {
              $('.dynamic_content').load('php_page.php?number=' + value);
            }
          }
        </script>
      </body>
    </html>
    

    PHP Page (php_page.php):

    <?php
    
      //intval($_GET['item']) will return 0 and considered as FALSE if the requested parameter is not a number
      if (isset($_GET['number']) && !empty($_GET['number']) && intval($_GET['number']) {
    
        $number = intval($_GET['number']);
    
        //I DID NOT TEST YOUR PHP CODE
    
        $servername = "localhost:3306";
        $username = "root";
        $password = "";
        $dbname = "produse";
    
        // Create connection
        $conn = mysqli_connect($servername, $username, $password, $dbname);
        // Check connection
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }
        $sql = "SELECT * FROM table LIMIT $number";
        $result = mysqli_query($conn, $sql);
    
        if (mysqli_num_rows($result) > 0) {
        // output data of each row
            while($row = mysqli_fetch_assoc($result)) {
                echo "id_produs: " . $row["id_produs"]. " - Denumire: " . $row["Denumire"]. " - Pret " . $row["Pret"]. " - Descriere" . $row["Descriere"] ."<br>";
            }
        } else {
            echo "0 results";
        }
    
        mysqli_close($conn);
      }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置