未捕获的ReferenceError:未定义getPrice

时间:2016-02-20 23:42:23

标签: javascript php jquery mysqli

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

脚本无效。我试图找出问题所在。我使用浏览器的开发者控制台工具。开发人员控制台工具给出了错误:

  

未捕获的ReferenceError:未定义getPrice | onchange @(index):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 "'>";
        }

    }
?>

1 个答案:

答案 0 :(得分:1)

首先关闭<script>代码:

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

您的脚本标记应位于</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';
}

希望这有帮助。