0

View urlCreate code

$ajaxSaveTestGrid = Yii::$app->urlManager->createUrl('testgrid/ajaxsave');

This Is My View Ajax Code

function saveRow(id, index) {    
    if(id == 'tbl_testGrid') {                
        save(id, index);
    }
 }

function save(id, index) {
    var testGrid_name = $('#testGrid_name' + index).val();
    var testGrid_qty = $('#testGrid_qty' + index).val();
    var testGrid_price = $('#testGrid_price' + index).val();


    var url = '$ajaxSaveTestGrid';

       // alert(testGrid_name+testGrid_qty+testGrid_price);

    $.ajax({
        type: 'GET',
        url: url,
        data: {
                testGrid_name:testGrid_name, 
                testGrid_qty:testGrid_qty,
                testGrid_price:testGrid_price
              },

        contentType: 'application/json; charset=utf-8', 
        dataType: 'json',

        success: function (response) { 
            if(response == 'error') {
                alert('Fail to save! Please try again'); 
            } else {   

                $('#testGrid_name' + index).attr(\"disabled\",true);             
                $('#testGrid_qty' + index).attr(\"disabled\",true); 
                $('#testGrid_price' + index).attr(\"disabled\", true); 
                $('#testGrid_save_button' + index).attr(\"class\", \"hidden\"); 
                $('#testGrid_delete_button' + index).attr(\"class\", \"hidden\"); 
                $('#testGrid_edit_button' + index).attr(\"class\", \"show\");  
                $('#hid_testGrid_id' + index).val(response[0].testgrid.id);
                $('html,body').animate({scrollTop: $('#btn_testGrid').offset().top});
            }
        }

   });
}

This is my Controller

public function actionAjaxsave() {
    $result = array();
    $testGrid_name = $_GET['testGrid_name'];
    $testGrid_qty = $_GET['testGrid_qty'];
    $testGrid_price = $_GET['testGrid_price'];

    $model = new Testgrid();

    $model->name = $testGrid_name;
    $model->price = $testGrid_price;
    $model->qty = $testGrid_qty;


    if ($model->save()) {
        array_push($result, ['testgrid' => $model]);
        $result = Json::encode($result);
        echo $result;
    } else {
        echo 'error';
    }
}

It occurring Internal Server Error

I want to save json Data to model.

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

Internal Server Error means that your code has fatal errors and error displaying is turned off. If you want to see the error itself, you must enable error displaying.

Check out the following question with its answers: How do I get PHP errors to display?

After you see the error, you can fix it.

PS:

$testGrid_name = $_GET['testGrid_name'];

This is not a recommended way to access GET variables. Use Yii::$app->request->get('testGrid_name') instead

Community
  • 1
  • 1
SilverFire
  • 1,582
  • 13
  • 22