Send data through ajax


(Sclapez) #1

Hi all!

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


(Rohisuthar) #2

You need to add this in your controller to identify ajax request -


Yii::app()->request->isAjaxRequest

So, just update your controller like this -


public function actionGuardarProceso(){


	if(Yii::app()->request->isAjaxRequest){


          $nombre = $_POST['post_nombre'];

          $empresa = $_POST['post_empresa'];

          $fechaI = $_POST['post_fechaI'];

          $fechaF = $_POST['post_fechaF'];

          $descripcion = $_POST['post_descripcion'];


           echo $nombre;

	}

 }