使用HTML变量调用PHP函数

时间:2017-06-03 07:26:23

标签: php html mysql

当选择组合框时,我正在尝试显示数据库中的前n项

  • 我试图用select标签中所选项目的值调用PHP函数“produse($ _ POST)”,代码是否正确?
  • SELECT SQL中的限制可以是代码中的限制吗? (“$ 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 个答案:

答案 0 :(得分:0)

使用jQuery单独使用它:

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页面(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);
  }
?>