2016-02-20 168 views
1

我正在嘗試運行腳本。該腳本需要向我顯示數據庫中的數據。在我的腳本中,我使用的是1 dropdown1 textbox。當我在下拉菜單中更改選定的值(產品)時,它需要顯示所選值的價格。價格需要顯示在文本框中。未捕獲ReferenceError:getPrice未定義

該腳本無法正常工作。我試圖找出問題所在。我使用了瀏覽器的開發人員控制檯工具。開發者控制檯工具給我的錯誤:

Uncaught ReferenceError: getPrice is not defined | 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

首先關閉<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'; 
} 

希望這有助於。

+1

難道你錯過了一個關閉腳本標記嗎?我實際上認爲*那*是問題,而不是腳本的位置。 –

+0

感謝您的介紹@cale_b,應該關閉腳本標記,但代碼應該放在正確的位置。 –