我正在尝试运行脚本。该脚本需要向我显示数据库中的数据。在我的脚本中,我正在使用1 dropdown1 textbox。当我在下拉菜单中更改所选值(产品)时,它需要显示所选值的价格。价格需要在文本框中显示。

该脚本不起作用。我试图找出问题所在。我使用了浏览器的开发者控制台工具。开发者控制台工具给我错误:


  未捕获的ReferenceError:未定义getPrice | onchange @(索引):1


有人可以帮我解决这个问题吗?

我用于此脚本的页面是以下页面:

index.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM forms";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<select class='form-control select2' id='product1' name='product1' onChange='getPrice(this.value)' style='width: 100%;'>";
     echo "<option selected disabled hidden value=''></option>";
     // output data of each row
     while($row = $result->fetch_assoc()) {
                      echo "<option value='" . $row["id"]. "'>" . $row["name"]. "</option>";
     }
echo "</select>";
} else {
     echo "0 results";
}

$conn->close();

?>
<html>
<body>
<!-- Your text input -->
<input id="product_name" type="text">

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    function getPrice() {

        // getting the selected id in combo
        var selectedItem = jQuery('.product1 option:selected').val();

        // Do an Ajax request to retrieve the product price
    jQuery.ajax({
        url: 'get.php',
        method: 'POST',
        data: 'id=' + selectedItem,
        success: function(response){
            // and put the price in text field
            jQuery('#product_name').val(response);
        },
        error: function (request, status, error) {
            alert(request.responseText);
        },
    });
    }
    </script>
    </body>
    </html>


get.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname) ;
// Check connection
if ($conn->connect_error)
    {
    die('Connection failed: ' . $conn->connect_error) ;
    }
else
    {
    $product1 = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT) ;

    $query = 'SELECT price FROM forms WHERE id=" . $product1 . " ' ;

    $res = mysqli_query($conn, $query) ;
    if (mysqli_num_rows($res) > 0)
    {
    $result = mysqli_fetch_assoc($res) ;
        echo "<input type='text' value='";
            echo json_encode($result['price']);
        echo "'>";
    }
    else
        {
        echo "<input type='text' value='";
        echo json_encode('no results') ;
        echo "'>";
        }

    }
?>

最佳答案

首先关闭<script>标签:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


您的scripts标签应该在</html>之前和<body>标签内:

<html>
    <body>
    <!-- Your text input -->
    <input id="product_name" type="text">

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script>
        function getPrice() {

        // getting the selected id in combo
        var selectedItem = jQuery('.product1 option:selected').val();

        // Do an Ajax request to retrieve the product price
        jQuery.ajax({
          url: 'get.php',
          method: 'POST',
          data: 'id=' + selectedItem,
          success: function(response){
            // and put the price in text field
            jQuery('#product_name').val(response);
          },
          error: function (request, status, error) {
            alert(request.responseText);
          },
        });
      }
        </script>
    </body>
</html>


PHP条件可能很简单,您不需要任何json编码,例如:

if (mysqli_num_rows($res) > 0)
{
    $result = mysqli_fetch_assoc($res) ;
    echo $result['price'];
}else{
    echo 'no results';
}


希望这可以帮助。

关于javascript - 未捕获的ReferenceError:未定义getPrice,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35530478/

10-12 06:59