gpt4 book ai didi

javascript - Ajax数据库选择值返回 '0'而不是字符串

转载 作者:行者123 更新时间:2023-12-03 00:43:30 25 4
gpt4 key购买 nike

我有一个二手车网站,该网站使用以下 SQL 在页面上列出了所有当前库存:“SELECT * FROM inventory ORDER BY timestamp ASC”。

当下拉菜单的选项发生更改时,我尝试使用 Ajax 数据库操作的 W3Schools ( https://www.w3schools.com/php/php_ajax_database.asp ) 方法来过滤我的结果。

javascript 和 php 都运行正确,但发送的值和新 sql 语句中使用的值显示为“0”,而不是应发送的值。

此下拉列表的值是从同一个表中提取的,以动态生成菜单,每个值应为“福特”、“奥迪”、“道奇”等。在由 javascript 发送之前,该值可以正常工作。

<select name="filtermake" class="css-dropdowns" tabindex="1" onchange="showMake(this.value)">
<option value="">All Makes</option>
<?php
$sql = "SELECT DISTINCT make FROM inventory ORDER BY make ASC";
$result = mysqli_query($conn, $sql);
while ($row = $result->fetch_assoc()) {
echo '<option value="'.$row['make'].'">'.$row['make'].'</option>';
}
?>
</select>

动态替换的JS是:

<script>
function showMake(str) {
if (str == "") {
document.getElementById("inventory").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("inventory").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","includes/inc.inventorymake.php?q="+str,true);
xmlhttp.send();
}
}
</script>

脚本运行的 PHP 代码是:

<?php
include_once ('inc.dbh.php');
$q = intval($_GET['q']);
$inventorysql = "SELECT * FROM inventory WHERE make = '".$q."'";
$result = mysqli_query($conn, $inventorysql);
while ($row = $result->fetch_assoc()) {
echo '<div id="inventory" class="inventory margin-bottom-20 clearfix scroll_effect fadeIn"><a class="inventory" href="inventory-listing.php?vin=' . $row['vin'] . '"><div class="title">'.$row['year'].' '.$row['make'].' '.$row['model'].'</div><img src="images/inventory/'.$row['vin'].'-main.jpg" class="preview" alt="preview"><table class="options-primary"><tr><td class="option primary">Body Style:</td><td class="spec">'.$row['body'].'</td></tr><tr><td class="option primary">Drivetrain:</td><td class="spec">'.$row['drivetrain'].'</td></tr><tr><td class="option primary">Transmission:</td><td class="spec">'.$row['transmission'].'</td></tr><tr><td class="option primary">Mileage:</td><td class="spec">'.ltrim ($row['mileage'], '0').'</td></tr></table><table class="options-secondary"><tr><td class="option secondary">Exterior Color:</td><td class="spec">'.$row['exteriorcolor'].'</td></tr><tr><td class="option secondary">Interior Color:</td><td class="spec">'.$row['interiorcolor'].'</td></tr><tr><td class="option secondary">VIN Number:</td><td class="spec">'.$row['vin'].'</td></tr></table><div class="price"><b>Price:</b><br><div class="figure">$'.ltrim ($row['price'], '0').'<br></div><div class="tax">Plus Tax, Tag, and Title</div></div><div class="view-details gradient_button"><i class="fa fa-plus-circle"></i> View Details </div><div class="clearfix"></div></a></div>';
}
?>

最后被javascript替换的div是:

<div id="inventory">
<?php
$inventorysql = "SELECT * FROM inventory ORDER BY timestamp ASC";
$result = mysqli_query($conn, $inventorysql);
while ($row = $result->fetch_assoc()) {
echo '<div id="inventory" class="inventory margin-bottom-20 clearfix scroll_effect fadeIn"><a class="inventory" href="inventory-listing.php?vin=' . $row['vin'] . '"><div class="title">'.$row['year'].' '.$row['make'].' '.$row['model'].'</div><img src="images/inventory/'.$row['vin'].'-main.jpg" class="preview" alt="preview"><table class="options-primary"><tr><td class="option primary">Body Style:</td><td class="spec">'.$row['body'].'</td></tr><tr><td class="option primary">Drivetrain:</td><td class="spec">'.$row['drivetrain'].'</td></tr><tr><td class="option primary">Transmission:</td><td class="spec">'.$row['transmission'].'</td></tr><tr><td class="option primary">Mileage:</td><td class="spec">'.ltrim ($row['mileage'], '0').'</td></tr></table><table class="options-secondary"><tr><td class="option secondary">Exterior Color:</td><td class="spec">'.$row['exteriorcolor'].'</td></tr><tr><td class="option secondary">Interior Color:</td><td class="spec">'.$row['interiorcolor'].'</td></tr><tr><td class="option secondary">VIN Number:</td><td class="spec">'.$row['vin'].'</td></tr></table><div class="price"><b>Price:</b><br><div class="figure">$'.ltrim ($row['price'], '0').'<br></div><div class="tax">Plus Tax, Tag, and Title</div></div><div class="view-details gradient_button"><i class="fa fa-plus-circle"></i> View Details </div><div class="clearfix"></div></a></div>';
}
?>
</div>

通过测试发送到变量 $q 的输出,我得到“0”,而不是“奥迪”或“福特”等。因此 sql 语句最终是 SELECT * FROM inventory WHERE make = '0' 和我需要它是 SELECT * FROM inventory WHERE make = 'Audi'

我希望这已经足够清楚了,我知道对于这样一个看似很小的问题需要消化很多信息,但我从过去的经验中知道,提供的信息太少不仅令人沮丧,而且需要更多时间才能获得适当的帮助。

谢谢。

最佳答案

我修好了。因此,当 php 设置 $q 时,它使用 intval() 函数,该函数用 0 覆盖字符串。删除该函数允许字符串通过并正确加载页面!

我更正后的 php 代码是:

<?php
include_once ('inc.dbh.php');
$q = $_GET['q'];
$inventorysql = "SELECT * FROM inventory WHERE make = '".$q."'";
$result = mysqli_query($conn, $inventorysql);
while ($row = $result->fetch_assoc()) {
echo '<div id="inventory" class="inventory margin-bottom-20 clearfix scroll_effect fadeIn"><a class="inventory" href="inventory-listing.php?vin=' . $row['vin'] . '"><div class="title">'.$row['year'].' '.$row['make'].' '.$row['model'].'</div><img src="images/inventory/'.$row['vin'].'-main.jpg" class="preview" alt="preview"><table class="options-primary"><tr><td class="option primary">Body Style:</td><td class="spec">'.$row['body'].'</td></tr><tr><td class="option primary">Drivetrain:</td><td class="spec">'.$row['drivetrain'].'</td></tr><tr><td class="option primary">Transmission:</td><td class="spec">'.$row['transmission'].'</td></tr><tr><td class="option primary">Mileage:</td><td class="spec">'.ltrim ($row['mileage'], '0').'</td></tr></table><table class="options-secondary"><tr><td class="option secondary">Exterior Color:</td><td class="spec">'.$row['exteriorcolor'].'</td></tr><tr><td class="option secondary">Interior Color:</td><td class="spec">'.$row['interiorcolor'].'</td></tr><tr><td class="option secondary">VIN Number:</td><td class="spec">'.$row['vin'].'</td></tr></table><div class="price"><b>Price:</b><br><div class="figure">$'.ltrim ($row['price'], '0').'<br></div><div class="tax">Plus Tax, Tag, and Title</div></div><div class="view-details gradient_button"><i class="fa fa-plus-circle"></i> View Details </div><div class="clearfix"></div></a></div>';
}
echo $q;
?>

关于javascript - Ajax数据库选择值返回 '0'而不是字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53342041/

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