php中的Ajax请求
我需要创建dinamic下拉选项。第二个选择取决于第一个输入。 我想从cakephp应用程序向controller中的操作发出ajax请求,但我被ajax代码绊住了 我尝试:php中的Ajax请求,php,ajax,Php,Ajax,我需要创建dinamic下拉选项。第二个选择取决于第一个输入。 我想从cakephp应用程序向controller中的操作发出ajax请求,但我被ajax代码绊住了 我尝试: <?= $this->Form->select('country_id' ,$optionsCountry, ['id' => 'country_id'], 'onclick' => 'getcit'); ?> <?= $this->Form->select('Cit
<?= $this->Form->select('country_id' ,$optionsCountry, ['id' => 'country_id'], 'onclick' => 'getcit'); ?>
<?= $this->Form->select('Cities', $optionsCities); ?>
然后,我想创建一个函数,该函数将在选择选项更改时调用,但我被卡住了
<script>
function getcities(str){
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function (){
xhttp.open('POST', 'controller' => 'cities', 'action' => 'getcit',+str , true);
xhttp.send();
}
}
我不知道如何在html中调用函数,onchange?当选?
以及如何从第一个输入获取数据,将其发送到控制器操作,从控制器获取数据并在另一个选择中设置它
谢谢,,
致以最良好的祝愿
我发现jquery.ajax可能是一个更简单的解决方案,但我又一次绊倒了:
<script>
$(document).ready(function){
$('#country_id').change(function () {
$.ajax({
url: "",
data: "",
type: 'POST'
});
});
}
;
你能帮我吗
-如何从第一个选择中获取数据
-将其转发给控制器
-并将返回值设置为secon select
谢谢,您必须在第一次更改后创建ajax调用,该调用返回第二次更改的数据 是一个事件列表。someElement.onXXX将在正确的时间调用它。至于问题的其余部分,请添加更多关于您尝试过的内容的详细信息,以便人们能够理解您的问题。
function getcities(id) {
$.ajax({
method: 'POST',
url: 'some.php',
data: {
country_id: id
},
success: function (data, status) {
$('.second_select').html(data); // If returns as html
}
});
return;
}