简体   繁体   中英

sending parameters to controller via ajax

I want to do this:

I have 2 grids. I want to select idideya (from first grid) and idperson (from second grid) and send them to the actionGonder() . With this action data'll be insert to table.

The success 'section' of ajax function works fine (it correctly alerts $perid and ideaid every time). But the data isn't sends to the controller.

Pleaseee help me guys. I need help. Thanks.

Best regards.

view file

<?php
    $this->widget('yiiwheels.widgets.grid.WhGridView', [
        'id' => 'layiheler-c',
        'dataProvider'=>$dataProvider,
        'filter' => $filtersForm,
        'ajaxUpdate'=>true,
        'responsiveTable' => true,
        'fixedHeader' => true,
        'headerOffset' => 40,
        'type' => 'striped bordered',
        'columns' => [
            [
                'name' => 'idideya',
                'headerHtmlOptions'=>['width'=>120],
                'header' => '#'
            ],
            [
                'name' => 'ideyaad',
                'headerHtmlOptions'=>['width'=>120],
                'header' => 'İdeya adı'
            ],
            [
                'name' => 'tarix',
                'headerHtmlOptions'=>['width'=>120],
                'header' => 'Təqdim olunma tarixi'
            ],
        ],
    ]);
?>

<button id="lrl" style="position: fixed">daxil et</button>
<br><br>

<?php
    $this->widget('yiiwheels.widgets.grid.WhGridView', [
        'id' => 'ekspertler-c',
        'dataProvider' => $dataProvider2,
        'filter' => $filtersForm2,
        'ajaxUpdate' => true,
        'responsiveTable' => true,
        'fixedHeader' => true,
        'headerOffset' => 40,
        'type' => 'striped bordered',
        'columns' => [
            [
                'name' => 'idperson',
                'headerHtmlOptions'=>['width'=>120],
                'header' => '#'
            ],
            [
                'name' => 'adi',
                'headerHtmlOptions'=>['width'=>120],
                'header' => 'Ad'
            ],
            [
                'name' => 'familya',
                'headerHtmlOptions'=>['width'=>120],
                'header' => 'Soyad'
            ],
        ],
    ]);
?>

<button id="eks" style="position: fixed;left: 69%;top: 11%;">daxil et</button>

<div id="zirrama">
    <input type="text" readonly id="idea_text" />
    <input type="text" readonly id="person_text" />
</div>

<script>
$siraI;
$siraP;

$("#lrl").click(function (){
    $("#layiheler-c tr.selected").each(function () {
        $setir = $(this).find('td');
        if($setir.length != 0) {
            $hazirki = $setir.eq(1).text();
            $siraI = $setir.eq(0).text();
            //  alert($toplam);
            //  alert($mircalal);
            // alert('seçilmiş ideya adı: ' + $hazirki);
            $("#idea_text").val($hazirki).before("<input type='text' style='display: none;' id='idea_text' />");
        }
    });
});

$("#eks").click(function (){
    $("#ekspertler-c tr.selected").each(function () {
        $setir = $(this).find('td');
        if($setir.length != 0) {
            $hazirki = $setir.eq(1).text();
            $siraP = $setir.eq(0).text();
            // alert($siraP);
            // alert('seçilmiş ekspert adı: ' + $hazirki);
            $("#person_text").val($hazirki).before("<input type='text' style='float: right; display: none' id='person_text' />");
        }
    });
});

function updatefunc(){
    $perid  = $siraP;
    $ideaid = $siraI;
    //alert($perid + " " + " " + $ideaid);
    // alert(perid);
    $.ajax({
        URL: "gonder/perid=.'$perid'./ideaid=.'$ideaid'",
        type: "POST",
        cache: false,
        data: {
            'perid': $perid,
            'ideaid': $ideaid
        },
        success:function(data){
            alert ($perid + " " + " " + $ideaid); /*  this line works fine. it alerts which idea and person I selected.  */
        },
   });
}
</script>

<?php  echo CHtml::submitButton('gonder', array('onclick'=>'updatefunc();')); ?>
<?php /* echo CHtml::button('gonder', array('submit' => array('onclick'=>'updatefunc();'))); */ ?>

controller file

public function actionGonder ()
{
    // die();
    echo "fsdfsdfsdfwtgikkyurtretgyuikuytryhjuhytryhk6ujkjyttyuj";
    if (isset($_POST['perid']) && isset($_POST['ideaid'])){
        $quer = Yii::app()->db->createCommand()
                ->insert ('mqrup', array(
                    'idperson'=> $perid,
                    'idideya'=>$ideaid ));
    // var_dump($quer);
    }
    // die();

    $this->redirect(array('umumi'));
}

Try this way

function updatefunc(){
$perid  = $siraP;
$ideaid = $siraI;
$.ajax({
    Url: <?php echo Yii::app()-     createUrl("controllerName/  gonder"),
    type: "POST",
    data: {
        'perid': $perid,
        'ideaid': $ideaid
    },
    success:function(data){
        alert (data);
     },
 });
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM