Yii Framework Forum: Send data through ajax - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Send data through ajax Rate Topic: -----

#1 User is offline   AleWebDesign 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 06-October 14

Posted 06 October 2014 - 02:09 PM

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
0

#2 User is offline   Rohit Suthar 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 247
  • Joined: 12-May 14
  • Location:Mumbai, India

Posted 08 October 2014 - 02:22 AM

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;
	}
 }

Thanks & regards,

ROHIT SUTHAR
Mumbai(India)

Follow me - @rohisuthar

yii.at

If you found your solutions.... Don't hesitate to click on '+1' button
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users