Question

I am very new to yii framework. I know how to use ajax with core PHP. I have a input data which needs to be inserted into the database through ajax.

This is my html code

<div id="ins-list">
    <input type="text" id="ins-text"/>
    <input type="submit" id="add-list" value="Add"/>
</div>

ajax code:

$("#add-list").click(function(){    

var task=$("#ins-text").val();

var data1 = 'dval=' + task;
$.ajax({
        type: 'POST', 
        url: 'insertdata.php',      
        data: data1,         
        success: function (da)
        {
           alert(da);
        }
    });     

I know this not how ajax works in yii. Please help me understand what are the files i need to create and in what folders for ajax to work.

I have made changes to my code as per the suggestion.

ajax code:

var data1 = 'dval=' + task;
$.ajax({ 
      type: 'POST', 
      url: 'createUrl('AjaxRequest'); ?>',
      data: data1,
      success: function(msg){ 
              alert("Sucess"+msg); },
      error: function(err){   
              alert("failure"+err); } 
 });

I created a controller action in SiteController:

 public function actionAjaxRequest()
 {

   if(isset($_POST['dval'])
    $val1 = $_POST['dval'];

    echo "works";

    Yii::app()->end();
  }

Now when the send ajax request i always get the "failure" message. Do i need create my own controller file instead of putting action in SiteController. M not even sure where i have gone wrong.

Was it helpful?

Solution 2

What is insertdata.php file? In Yii you have only one input file - index.php. You need to create action in some controller.

This way you are using ajax is also acceptable and in some cases it's better to use then ajaxLinks, ajaxButtons.

OTHER TIPS

The client side is similar, but you should probably use the yii ajax functions.

CHtml::ajax() - http://www.yiiframework.com/doc/api/1.1/CHtml#ajax-detail

CHtml::ajaxLink() - http://www.yiiframework.com/doc/api/1.1/CHtml#ajaxLink-detail

CHtml::ajaxButton() - http://www.yiiframework.com/doc/api/1.1/CHtml#ajaxButton-detail

These will help to start the ajax process, when you identify what url to use in the ajax, pass the controller and action to use.

Then inside that controller, check to see you received an ajax request using Yii::app()->request->isAjaxRequest().

Then do whatever you need to do to validate or manipulate.

Insert or reject, return appropriate error messages back to view.

Thanks I figured it out after some trial and error. Made a new controller file named MyController with this function:

public function actionAjaxRequest()
{

    if(isset($_POST['dval'])
     { 
       $val1 = $_POST['dval'];

       echo "works";
     }

   Yii::app()->end();
}

In my ajax request I have following:

var data1 = 'dval=' + task;
$.ajax({ 
  type: "POST", 
  url: "<?php echo Yii::app()->createUrl('My/AjaxRequest'); ?>",
  data: data1,
  success: function(msg){ 
          alert("Success "+msg); },
  error: function(err){   
          alert("failure "+err); } 
 });

Works like charm :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top