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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?