Reputation: 15
First of all thanks all for your help, it's very useful. Im starting with yii and Im a bit lost yet.
I have create a jquery script where I validate a form and then I send it to my controller to work with it and save in the db.
But Im doing it wrong I think I cant connect with my controller. Here is the code:
Jquery script(after all the validate stuff, the variables are fine):
$.ajax({
type: "POST",
url: "<?php echo Yii::app()->request->baseUrl; ?>/proceso/guardarproceso",
data:
{
post_nombre: nombre,
post_empresa: empresa,
post_fechaI: fechaI,
post_fechaF: fechaF,
post_descripcion: descripcion
},
success: function(result)
{
alert(result);
}
});
And my controller ProcesoController
:
public function actionGuardarProceso(){
$nombre = $_POST['post_nombre'];
$empresa = $_POST['post_empresa'];
$fechaI = $_POST['post_fechaI'];
$fechaF = $_POST['post_fechaF'];
$descripcion = $_POST['post_descripcion'];
echo $nombre;
}
Im not working with the db yet, I only want to see if I have done it well and the alert(result)
shows me the content of $nombre
, but instead of that the alert shows me all the html code of the view(yes all xD)
I have done it too:
public function accessRules()
{
return array(
array(
'allow',
'actions'=>array('index','guardarproceso'),
'users'=>array('*'),
),
);
}
But nothing...
Anyone cuold help me or give me some idea? Thank you all again
Upvotes: 1
Views: 7655
Reputation: 4801
1st error: url: "<?php echo Yii::app()->request->baseUrl; ?>/proceso/guardarproceso",
replace guardarproceso
with guardarProceso
2nd error:
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
dataType
is missing; it should be json
read here http://api.jquery.com/jquery.post/
also, in the controller action actionGuardarProceso
, use:
echo json_encode(array('key'=>$nombre));
exit
Upvotes: 1
Reputation: 1132
$.ajax({
type: "POST",
url: "<?php echo Yii::app()->request->baseUrl; ?>/proceso/guardarproceso",
data:
{
post_nombre: nombre,
post_empresa: empresa,
post_fechaI: fechaI,
post_fechaF: fechaF,
post_descripcion: descripcion
},
success: function(result) {
alert(result);
},
error:function(jqXHR, textStatus, errorThrown){
alert('error::'+errorThrown);
}
});
first try this you will get post data or not
public function actionGuardarProceso(){
echo "<pre>";
print_r($_POST);
exit;
}
if not getting any data , try with your model
public function actionGuardarProceso(){
$model = new modelname;
echo $_POST['modelname']['post_nombre'];
exit;
}
else try with the following
public function actionGuardarProceso(){
echo Yii::app()->request->getPost("post_nombre");
exit;
}
i hope you will get now from the above any methods
Upvotes: 0
Reputation: 219
If a ajax or jquery result prints the html of the page its usually a error in the url. check your network jumps to see if it goes to the controller action you want. also baseurl is slower than createUrl. try Yii::app()->createUrl and edit that until it goes to the correct destination.. But the base of my theory is that your url is incorrect.
Upvotes: 0