我已經爲兩個下拉框編寫了代碼。當我從兩個框中選擇值時,這些值應該使用GET方法進入一個php文件。當這兩個框被選中時,這些值應該到php文件。如何將兩個值傳遞給使用JavaScript的PHP文件Ajax
這裏是我的代碼:
<script>
function CatName(str1, str2) {
if (str == "") {
document.getElementById("album_name").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 (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("album_name").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET", "update.php?q=" + str1 + "&q1=" + str2, true);
xmlhttp.send();
}
}
</script>
<div class="col-xs-6 col-sm-6 col-md-4 col-md-offset-1">
<div class="form-group">
<select class="form-control" name="category_name" id="id-of-the-first-select-box-here">
<option value="" selected>Please Select</option>
<option value="Birds">Birds</option>
<option value="Animals">Animals</option>
<option value="Notinlist">Category Not in list</option>
</select>
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-4 col-md-offset-1">
<div class="form-group" >
<select class="form-control" name="album_name" onchange="CatName(this.value, document.getElementById('id-of-the-first-select-box-here').value)">
<option value="" selected>Please Select</option>
<option value="sadb">sadb</option>
<option value="Animals">Animals</option>
<option value="Notinlist">Category Not in list</option>
</select>
</div>
</div>
只有你在上面的代碼中添加了jquery liabrary –