I have a Form, that looks like this:
echo $this->Form->control('deliveryradius',['type'=>'number','min'=>1,'max'=>20,'step'=>1,'value'=>$salespoint->deliveryradius,'onchange'=>"updategeonames(this.value)"]);
<div id="geonames">
<?php
echo $this->Form->control('geonames._ids',[
'options'=>$geonames->map(function($geoname,$key) {
return [
'value'=>$geoname->id,
'text'=>$geoname->country.' '.$geoname->postalcode.' '.$geoname->placeName.__(' calculated Distance: ').$this->Number->precision($geoname->distance,2).' km'
];
}),
'type'=>'select','multiple'=>'checkbox','hiddenField'=>false,'label'=>false,'value'=>$selectedgeonames]);
?>
</div>
The Javascript:
<script> function updategeonames(val) { //window.alert(val); var csrfToken = <?= json_encode($this->request->getAttribute('csrfToken')) ?>; var xmlhttp=new XMLHttpRequest(); xmlhttp.onreadystatechange=function() { if (this.readyState==4 && this.status==200) { document.getElementById("geonames").innerHTML=this.responseText; } } xmlhttp.open("GET","<?php echo $this->Url->build(['controller'=>'Salespoints','action'=>'updategeonames?q=']); ?>"+val,true); xmlhttp.setRequestHeader('X-CSRF-Token', csrfToken); xmlhttp.send(); } </script>
On changing the value of
deliveryradius
a request is sent to the controller. (I checked it with a simple debug).
What I like to know, is how to get the updated variables $geonames and $selectedgeonames into my select input field.