Creating A Record Using Ajax

Hi, I want to create a record in my DB with information from a form, i want to send it via AJAX

Here’s my view




<?php

/* @var $this UserController */

/* @var $model User */


//registering jQuery 

Yii::app()->clientScript->registerCoreScript('jquery');


?>

<h1>User: <?php echo $model->username; ?></h1>


<h2>Entries</h2>

<ul>

  <?php foreach($model->entries as $entry):?>

    <div class="view">

      <li class="entries"><h2> <?php echo $entry['title']; ?> </h2></li>

      <li class="entries"><?php echo $entry['content']; ?></li><br/>

      <li class="entries">Created on <?php echo date("d M - g:i A ",strtotime($entry['creation_date'])); ?></li><br/>

    </div>

    <?php endforeach; ?>

</ul>

<?php $this->beginWidget('system.web.widgets.CClipWidget', array('id'=>'sidebar')); ?>

<h1>Tweet Feed</h1>

<ul>

  <?php $form=$this->beginWidget('CActiveForm', array('id'=>'hide-tweet',)); ?>

    <?php $i=1; foreach($tweets as $tweet):?>		

      <div id="tw-<?php echo $i; ?>">

	<h1><?php echo $tweet->id_str; ?></h1>

	<div class="view">

	  <li class="entries"><?php echo '<b>'.$tweet->user->name.'</b> @'.$tweet->user->screen_name.'  '.date("d M",strtotime($tweet->created_at)); ?></li><br/>

	  <li class="entries"><?php echo $tweet->text; ?></li>						

	  <?php if($model->usr_id == Yii::app()->user->Id):?>

	  <br/><li class="entries">

	    <?php echo CHtml::ajaxLink(

	    'Click',

	    array('tweet/create'),

	    array(

	      'update'=>'#content',

	      'type'=>'POST',											

	      'dataType'=>'json',

	      'data'=>array(

	      'tw_tweet_id'=>$tweet->id_str,

	      'tw_author'=>$model->usr_id,

	      'tw_name'=>$tweet->user->name,

	      'tw_creation_date'=>$tweet->created_at,

	      'tw_content'=>$tweet->text,

	       ),

              ));

	   ?>

	 </li>

	<?php endif; ?>

	</div>

     </div>		

    <?php $i++; endforeach; ?>

   <?php $this->endWidget(); ?>

</ul>

<?php $this->endWidget();?>



And in my tweet controller in the actionCreate function I have the following




public function actionCreate()

	{

		$model=new Tweet;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Tweet']))

		{

			echo "arrived at controller!!!";

			var_dump($_POST['Tweet']);

			//$model->attributes=$_POST['Tweet'];

			//if($model->save())

				//$this->redirect(array('view','id'=>$model->tw_id));

		}


		/*$this->render('create',array(

			'model'=>$model,

		));*/

	}



When I click on the link it the POST return a 200 OK status but neither the echo and the var_dump show in my #content, I think I’m doing it wrong, can anyone please give some clearance in this matter, at least I want to be clear that the POST is working, having that I think I can do the rest.

Ok, I’ve seen something in the model the variable is $_POST[‘Tweet’] which for some reason I don’t know doesn’t exist when I send the form, but if just var_dump($_POST) all the information appears, but I cannot use to save it in the DB how or what can I do in the form to return $_POST[‘Tweet’] ?




              'data'=>array(

                'Tweet'=>array(

                  'tw_tweet_id'=>$tweet->id_str,

                  'tw_author'=>$model->usr_id,

                  'tw_name'=>$tweet->user->name,

                  'tw_creation_date'=>$tweet->created_at,

                  'tw_content'=>$tweet->text,

                ),

              ),



You’re doing it wrong.

This way you’ll get your data right inside $_POST, i. e. $_POST[‘tw_tweet_id’] etc.

Also I can’t find any inputs in your form, so I wonder how user should write his tweets :)

UPD: ninja’d by Tsunami :))

Thanks Tsunami it worked but the data isn’t saved :confused:

and ORey actually I’m taking the data from Twitter, how can I use that data and put it in a input? can i make hidden inputs and populate with that data?

if so, how can i do that?

I’m really new with Yii (a noob) and if you guys could help me here that would mean a lot!

Thanks

I just did var_dump($model->save()) and it dumped a boolean false and with another controller it was true, can someone explain me why’s that? what am I doing wrong?

Thanks


var_dump($model->save(), $model->errors);

$model->errors will contain any validation errors after $model->save() is called.