使用mysql表数据填充Javascript OptionArray

时间:2014-10-24 07:07:49

标签: javascript php mysql arrays

<!----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"];
} 

1 个答案:

答案 0 :(得分:0)

新行是因为它们是逗号&lt;,&gt;在函数 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'] . '"';
};

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