gpt4 book ai didi

javascript - Uncaught ReferenceError : getPrice is not defined

转载 作者:行者123 更新时间:2023-11-27 23:19:16 26 4
gpt4 key购买 nike

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

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

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 "'>";
}

}
?>

最佳答案

首次收盘<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';
}

希望这有帮助。

关于javascript - Uncaught ReferenceError : getPrice is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35530478/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com