I am using ajax post in my application, for example
$.ajax({
type: "POST",
url: "http://localhost/FormBuilder/index.php/forms/saveForm/"+user_id,
data: "formname="+formname+"&status="+status,
success: function(msg){
}
});
In the ajax post above, I save the form with user id
Can I get the form form identifier that I saved in the Ajax request. If so, how?
I tried with Ajax to get separately. But here I want to mix and message and get .. Can I do this. EDIT:
COuld I returns any value for the Ajax POST method. Since I want to return the form identifier of the saved form.
Edit:
alert("Data Saved: "+msg); gives as
Data Saved: {"forms":[{"id":"41"},{"id":"35"},{"id":"34"},{"id":"33"},{"id":"32"},{"id":"22"},{"id":"3"},{"id":"2"},{"id":"1"}]}
The above is that the return value I only want is id 41, how should I get it?
EDIT:
$.ajax({
type: "POST",
url: "http://localhost/FormBuilder/index.php/forms/saveForm/"+user_id,
datatype: 'json',
data: "formname="+formname+"&status="+status,
success: function(json){
alert( "id is : " + json.forms[0].id);
}
});
Even I tried it with the code above, as suggested, but I can not get the warning message.
My controller code is similar to
function saveForm()
{
$this->data['Form']['name']=$this->params['form']['formname'];
$this->data['Form']['created_by']=$this->Session->read('userId');
$this->data['Form']['status']=$this->params['form']['status'];
$this->data['Form']['access']="Private";
$userId=$this->Form->saveForms($this->data);
$formid = $this->Form->find('all', array('fields' => array('Form.id'),
'order' => 'Form.id DESC' ));
$this->set('formid',$formid);
}
And my save_form.ctp file has
<?php
$data=array();
?>
<?php foreach ($formid as $r):
array_push($data, array('id' => $r['Form']['id']));
endforeach;
echo json_encode(array("forms" => $data));
?>