如何使用ajax容纳属性,以使其成为控制器中的变量

问题描述 投票:1回答:1

我有一个简单的应用程序,使用php native mvc进行搜索,我想通过使用curl调用表剩余服务以执行删除操作来删除表数据,我想根据用户名进行删除,但我无法获取基于数据的数据关于用户名,所以我认为使用ajax可以保存用户数据,然后可以将其称为变量并由控制器执行,我已经尝试过但无法运行,请帮助我,我的编码是否错误?

这是我的视图html代码

<?php
$main_controll = new App_UserManagement_Control_UserManagement();
$data_from_ctr = $main_controll->usermanagement();
?>

<script type="text/javascript">

$('#delete').change(function(){
    //$('#pesan-sukses').hide()

    var username = $(this).val();
    if(username != 0){

        $.ajax({

            url: '<?php echo BASE_URL ?>app.php/usermanagement/usermanagement',
            type: 'post',
            data:{username:username},
            dataType: 'json',
            success:function(data){
                if(response.status == true){

                            return data();
                            //$('pesan-sukses').html(response.pesan).fadeIn().delay(10000).fadeOut();

                            //location.href = '<?php// echo BASE_URL ?>app.php/usermanagement/user_management';
            }
        })
    }

});

}


</script>


<div id="pesan-sukses" class="alert alert-success" style="margin: 10px 20px;"></div>
<table cellspacing="2" cellpadding="2" border="0" align="left" id="tablecontent">                      
    <thead style="background-color:#eee;">
        <th width="25">#</th>
        <th width="80">Username</th>
        <th width="117">Name</th>
        <th width="117">Company Code</th>
        <th width="117">Company Name</th>
        <th width="80">User Access</th>
        <th width="80">Login Status</th>
        <th width="80">User Status</th>
        <th width="200">Action</th>

    </thead>
    <tbody>
        <?php if($data_from_ctr['user'] !== NULL): ?>
        <?php $i = 1; foreach ($data_from_ctr['user'] as $row):
         ?>


        <tr>
        <td><?php echo $i++; ?></td>
        <td><?php echo $row ['_user'] ?></td>
        <td><?php echo $row ['_fullName'] ?></td>
        <td><?php echo $row ['_pyrCode'] ?></td>
        <td><?php echo $row ['_desc'] ?></td>
        <td><?php echo $row ['group_user'] ?></td>
        <!--<td><?php //echo $row ['_flag'] ?></td>-->
        <td>
        <?php
        $status = $row['_flag'];
        if($status == 0){
            echo "Offline";
        }elseif ($status == "") {
            echo "Online";
        }else{
            echo "Online";
        }
        ?>
        </td>
        <td>
        <?php
        $active = $row ['active'];
        if($active == 1){
            echo "Active";
        }else{
            echo "Non Active";
        }       

        ?>
        </td>
        <td>
                <a href="<?php echo BASE_URL. "app.php/usermanagement/edit_user"?>">Edit</a>
                &nbsp;
                <a id="delete" name="delete" onclick="return confirm('Are you sure want to delete ?')" href="<?php echo BASE_URL. "app.php/usermanagement/delete"?>">Delete</a>
                &nbsp;
                <a onclick="///return confirm('Are you sure want reset {{ row._fullName }} password ?')" href="#">Reset Password</a>
            </td>       </tr>
          <?php endforeach; ?>
          <?php endif; ?>
    </tbody>
</table>
</fieldset>  

这是我的控制器

<?php

class App_UserManagement_Control_UserManagement extends UserControl{

public function delete(){



if (Request::isPost()){
        $username = Request::post('username');

        echo json_encode($username);

    }
        if($username){

        $url="http://localhost:8585/delete-usermanagement/$username";

        //echo $url; die;

        //$link_url = urlencode($url); 
        $curl = curl_init($url);

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $result = curl_exec($curl);
        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        curl_close($curl);

        Route::redirect('app.php/usermanagement/user_management');

        return $result;

        } else {
            echo "<script language = \"javascript\">window.alert(\"\Data tidak dapat dihapus\");";
            echo "javascript:history.back();</script>";
            return false;
        }

    }
}
php ajax codeigniter curl model-view-controller
1个回答
-1
投票

只需替换

dataType: 'json',

它将起作用:)

© www.soinside.com 2019 - 2024. All rights reserved.