gpt4 book ai didi

javascript - 使用 mysql 表数据填充 Javascript OptionArray

转载 作者:行者123 更新时间:2023-11-29 23:31:33 25 4
gpt4 key购买 nike

<!----HERE IS ALL MY CODE--->
<?php

$con = mysqli_connect("localhost","theswapt_Swapper","$1Swapper","theswapt_TheSwap2Shop");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
};
$result2 = mysqli_query($con,"SELECT * FROM class_swap_categories WHERE parent='2093'");

function whileloop($result2) {
while($row1 = mysqli_fetch_array($result2)) {echo '"' . $row1['id'] . '|' . $row1['catname'] . '"' . ',';};
};
?>

<script type="text/javascript">
function populate(s1,s2){
var s1 = document.getElementById(s1);
var s2 = document.getElementById(s2);
s2.innerHTML = "";
if(s1.value == "2093"){
var optionArray = [<?php echo whileloop($result2);?>];
} else if(s1.value == "1843"){
var optionArray = ["|","avenger|Avenger","challenger|Challenger","charger|Charger"];
} else if(s1.value == "Ford"){
var optionArray = ["|","mustang|Mustang","shelby|Shelby"];
}
for(var option in optionArray){
var pair = optionArray[option].split("|");
var newOption = document.createElement("option");
newOption.value = pair[0];
newOption.innerHTML = pair[1];
s2.options.add(newOption);
}
}
</script>

<form method="post">


Choose Category:
<select id="slct1" name="slct1" onchange="populate(this.id,'slct2')" style="height: 40px; margin: 5px; width: 100%; max-width: 250px; color: #00B8FF;">
<option value=""></option>
<?php

$result = mysqli_query($con,"SELECT * FROM class_swap_categories WHERE parent='0'");

while($row = mysqli_fetch_array($result)) {

echo '<option value="' .$row['id']. '">' .$row['catname']. '</option>';

};

?>
</select>
Choose Style:
<select id="slct2" name="slct2" style="height: 40px; margin: 5px; width: 100%; max-width: 250px; color: #00B8FF;"></select>

</form>

我的问题是我无法正确加载第一个 2093,无论我做什么,php while 语句都会在语句末尾添加一个新行,并且当我运行它时,javascript 数组看起来像这样。而不是正确显示的 1843 选项,因为它不是动态的。请帮忙。

 if(s1.value == "2093"){
var optionArray = ["2124| Pants & Jeans
","2118| Swim
","2112| Tanks & Tees
","2106| Dress Shirts
","2100| Polos & Casual Shirts
","2094| Bundled Outfit(s)
","2149| Shorts
","2157| Hoodies & Sweatshirts
","2163| Sweaters & Cardigans
","2169| Jackets & Sports Coats
","2175| Suits & Suit Separates
","2181| Active Wear
","2187| Lounge Wear & Robes
",];
} else if(s1.value == "1843"){
var optionArray = ["|","avenger|Avenger","challenger|Challenger","charger|Charger"];
} else if(s1.value == "Ford"){
var optionArray = ["|","mustang|Mustang","shelby|Shelby"];
}

最佳答案

新行是因为函数whileloop的输出末尾有一个逗号<,>,使用下面的函数将其删除:

function whileloop($result2) {
$finalOpt = "";
while($row1 = mysqli_fetch_array($result2))
{
if($finalOpt=="")
$finalOpt = $finalOpt.'"' . $row1['id'] . '|' . $row1['catname'] . '"';
else
$finalOpt = $finalOpt.',"' . $row1['id'] . '|' . $row1['catname'] . '"';
};

从你的问题中我没有得到你面临的其他问题,请解释一下。

关于javascript - 使用 mysql 表数据填充 Javascript OptionArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26543079/

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