我得到了一个包含两个选择的表单,选择1和选择2,它们都填充了mysql数据库中的选项,它们之间存在关系。ej:select 1=车型select 2=汽车品牌当我选择一款车型时,ej:focus我需要将当前品牌改为ford。在数据库中给出了ford与focus之间的关系,我如何询问数据库并根据数据库的响应更改选择值?,我试过了http://www.w3schools.com/php/php_ajax_database.asp该教程,但我不知道如何修改它以返回一个值,而不是打印结果。
真实案例:
数据库:表名:区域(id_Region,nombre_Region)表名:Provincia(nombre_provicia、id_provicia和id_region)
形式:
<label>Region: </label>
<select name="region" id="region">
<?php include('lista_region.php'); ?> //this makes the list asking the database, works fine.
</select>
<label>Provincia: </label>
<select name="provincia" id="provincia" onchange="HERE_I_NEED_HELP()">
<?php include('lista_provincia.php'); ?> //this makes the list asking the database, works fine.
</select>
在"HERE_I_NED_HELP()"中,我需要从数据库中选择id_region,其中"provincia"的值与数据库中的值匹配,然后将"region"的值更改为我刚刚从数据库中获得的值,
好吧,我解决这个问题的方法是调用一个调用Web服务的函数(它处理服务器并返回一个带有答案的json),通过这个返回,我将其更改为新值,这个函数起到了神奇的作用:
<script>
function buscarregion(){
var oReq = new XMLHttpRequest();
oReq.onload = function () {
var resultado = JSON.parse(this.responseText);
document.getElementById("region".value = reulstado.data;
};
var service = "webservice.php?param="
var x = document.getElementById("provincia").value
x = web.concat(x);
oReq.open("get", x, true);
oReq.send();
}
仅此而已!!,webservice使用x中的参数send进行搜索,并返回我想要的值,我将其打印在"region"中,希望这能帮助解决我同样问题的人。谢谢你的评论。
ps:buscarregion()替换HERE_I_NED_HELP()