I am using a ajax post in my application like
$.ajax({
type: "POST",
url: "http://localhost/FormBuilder/index.php/forms/saveForm/"+user_id,
data: "formname="+formname+"&status="+status,
success: function(msg){
// alert( "Data Saved: " + msg);
}//success
});//ajax
In the above ajax post i am saving the Form with the user id
Could i able to get the Form id of the Form that i saved in the Ajax request . If so how??
I have tried with Ajax get in a separately.But here i want to mix up both post and get.. Could i do that.. EDIT:
COuld i return any value for the Ajax POST method . Since i want to return the Form id of the Form saved..
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 what the value returned i want only th 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);
}//success
});//ajax
Even i tried it with the above code as suggested, But i am not able to get the alert message..
My controller code is like
function saveForm()
{
//$userId=$this->Session->read('userId');
$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 has
<?php
$data=array();
?>
<?php foreach ($formid as $r):
array_push($data, array('id' => $r['Form']['id']));
endforeach;
echo json_encode(array("forms" => $data));
?>