I use yii framework to create map application.In this application i use GeoExt and Openlayers.In this App user can draw polygon and save this for achieving this after user draw polygon and click on it(OpenLayers feature) i send polygon with ajax to save that on database but i can’t get any ajax data from controller action in yii framework.See below code!
var selectOptions = {
clickout: true,
onSelect: save
};
select = new OpenLayers.Control.SelectFeature(vectors, selectOptions);
map.addControl(select);
var saveButton = new Ext.Button({
text: 'Save',
enableToggle: true,
toggleGroup: toggleGroup,
handler: function(toggled)
{
if(toggled){
polygon.deactivate();
modify.deactivate();
select.activate();
}
}
function save(feature) {
var geojson_format = new OpenLayers.Format.GeoJSON();
var str = geojson_format.write(feature, true);
Ext.MessageBox.prompt('Name', 'Please enter district name:', "Ok");
<?php
echo CHtml::ajax(array(
'url'=>array('site/test'),
'data'=>array('polygon'=>'data'),
'type'=>'POST',
'success'=>"function(){
alert('ok');
}"
));
?>
$("#output").val(str);
}
in the save function i use yii Chtml::ajax to send polygon to Yii site controller test action in this php code i send polygon with ‘data’ for testing but in real i want to send str variable that is polygon geojson object to controller action.At the end of function i use $(“#output”).val(str); for testing geojson str var i see the output in the output textarea but i don’t know why data doesn’t send to controller action!
controller action Code
class SiteController extends Controller
{
public $polygon;
public function actionTest()
{
if(isset($_POST['polygon']))
$this->polygon=$_POST['polygon'];
$this->render("test", array('polygon'=>$this->polygon));
}
for testing functionality i create a polygon in the map and click save button after this button is toggled i click on the created polygon i see polygon geojson object in the output textarea but when i want to see polygon object in the browser with http://localhost/FleetManagement/index.php/site/test i see only Null!!
test view code:
<?php
var_dump($polygon);
?>
Below is save function in the chrome developer tools!
function save(feature) {
var geojson_format = new OpenLayers.Format.GeoJSON();
var str = geojson_format.write(feature, true);
Ext.MessageBox.prompt('Name', 'Please enter district name:', "Ok");
jQuery.ajax({'url':'/FleetManagement/index.php/site/test','data':{'polygon':'data'},'type':'POST','cache':false}); $("#output").val(str);
}
when i debug i see JQuery.ajax line execute but i don’t know this line can send data or no! because when i see test view i only see Null!
In the above code i can see success alert but i can’t see site controller polygon in test view and $polygon is Null!
I believe that your code is probably working – adding a var_dump($_POST) in actionTest() would be a good place to start.
Since you’re making an AJAX call, the $this->render() is not returning to the browser the way you’re expecting. I would imagine that in the Chrome Developer Tools, Network tab, then then Response tab, you’ll see ‘data’.
In general, to get data back, you’d want something like:
To access the returned data, you could use a callback in your AJAX, e.g. :
This would log the value of polygon to the console . . . modify the success function to output this somewhere more useful.
Your current method sounds as if it’s failing since you’re accessing
site/testdirectly in your browser via the address bar, which means that $_POST won’t have anything in it.