javascript - 在zend中的html选择中选择更改后如何更新数据库

标签 javascript php zend-framework onchange

我的 View 中有一个 html 选择下拉列表

    <?php $level = $this->escapeHtml($user->level); ?>

    <?php if ($level == 1): ?>
         <option value="1" selected ="selected">Administrator</option>
         <option value="2" <?php IsSelected($level,2); ?>> Manager</option>
         <option value="3" <?php IsSelected($level,3); ?>>HR Staff</option>
     <?php elseif ($level == 2): ?>
         <option value="1" <?php IsSelected($level,1); ?>>Administrator</option>
         <option value="2" selected ="selected">Manager</option>
         <option value="3" <?php IsSelected($level,3); ?>>HR Staff</option>
       <?php elseif ($level == 3): ?>
         <option value="1" <?php IsSelected($level,1); ?>>Administrator</option>
         <option value="2" <?php IsSelected($level,2); ?>>Manager</option>
         <option value="3" selected ="selected">HR Staff</option>
        <?php endif; ?>
    </select>

我想要的是,每次我在下拉列表中选择时,它都必须更新数据库中用户表中的级别列。

我尝试过使用javascript的onchange事件

 <script type='text/javascript'>


function changeLevel(level){
    var user_id = level.value; 
    $.ajax({
            type: 'GET',
            url: '/editLevel/'+user_id,
            cache: false,
            success: function(response) {
                    $("#test").html(response);
                }
        });
    alert(user_id);
}

</script>

但调用父 URL 失败。 这输出 http://localhost/ijm/public/%7B%7BURL()%7D%7D/editLevel/2 而不是http://localhost/ijm/public/user/editLevel/2

除此之外,我如何从该 JavaScript 函数更新我的用户表?

这是我在 userController 中的函数

public function editLevelAction()
    {
        //return $this->$view;
        $id = (int) $this->params()->fromRoute('id', 0);
        $data = array(
                'level' => $level
        );
        $this->update('user', $data, 'id = ' .'39');

         //return $this->redirect()->toRoute('user');


    }

我需要做什么才能让它发挥作用?提前致谢。

这是完整的代码。

<?php
$title = "Manage Users ";
$this->headtitle($title);

$identity = $this->identity()->level;
?>
<!-- these should be between the <head> tags, but make sure they are at least after the head if they can't be in the head-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<div class="page-header">
    <h1><?php echo $this->escapeHtml($title); ?><small>User list</small></h1>
</div>

<ul class="nav nav-tabs">
    <li class="pull-right"><a href="<?php echo $this->url('user/add'); ?>"><i class="icon-plus"></i>&nbsp;Add new user</a></li>
</ul>
<?php if (count($users)): ?>
    <table class="table table-hover">
    <thead>
    <tr>
        <th>Name</th>
        <th>E-mail</th>
        <th>Level</th>
        <th></th>
      </tr>
    </thead>
    <?php foreach ($users as $user): ?>
   <tr>
        <td><a href="<?php echo $this->url('user/edit', array('id' => $user->id)); ?>"><?php echo $this->escapeHtml($user->first_name . ' ' . $user->last_name); ?></a></td>
        <td><?php echo $this->escapeHtml($user->email); ?></td>
        <td>
            <div id="test"></div>
            <?php $level = $this->escapeHtml($user->level); ?>
            <select name="level" id= "level" onchange="changeLevel(this)">
                 <option value="1" <?php IsSelected($level,1); ?>>Administrator</option>
                 <option value="2" <?php IsSelected($level,2); ?>> Manager</option>
                 <option value="3" <?php IsSelected($level,3); ?>>HR Staff</option>
            </select>
        </td>
        <td style="text-align: right;">
            <a href="<?php echo $this->url('user/delete', array('id' => $user->id)); ?>" class="btn btn-mini btn-danger" rel="tooltip" title="Delete this user"><i class="icon-remove icon-white"></i></a>
        </td>
    </tr>
    <?php endforeach; ?>
    </table>
<?php else: ?>
<h3>There are no registered users available.</h3>
<?php endif; ?>

<script type='text/javascript'>
    function changeLevel(level){
        var user_id = level.value; 
        $.ajax({
                type: 'GET',
                url: '/editLevel/'+user_id,
                cache: false,
                success: function(response) {
                        $("#test").html(response);
                        alert(user_id);
                    }
            });

      }
</script>

最佳答案

尝试使用 AJAX(这里是 jQuery)。这只是一个基于您的简单示例:

jQuery 托管库

<!-- jQUERY Libraries -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>

表单:选择

<!-- Just a div to display results if you want to -->
<div id="test"></div>

<form>
    <?php
        // Here is just a quick function to help fix your redundant 
        // select check. This could probably be done better, but you
        // don't need to echo out all 3 sets of options 3 times 
        function IsSelected($value = 1,$level = 1) {
                echo ($value == $level)? 'selected="selected"':"";
            }

        $level = $this->escapeHtml($user->level); ?>

    <select name="level" id= "level" onchange="changeLevel(this)">  
         <option value="1"<?php IsSelected($level,1); ?>>Administrator</option>
         <option value="2"<?php IsSelected($level,2); ?>>Manager</option>
         <option value="3"<?php IsSelected($level,3); ?>>HR Staff</option>
    </select>
</form>

jQuery AJAX

<script type='text/javascript'>

    function changeLevel(level){
        var user_id = level.value; 
        $.ajax({
                type: 'GET',
                url: '/editLevel/'+user_id,
                cache: false,
                success: function(response) {
                        $("#test").html(response);
                    }
            });

      }
</script>

关于javascript - 在zend中的html选择中选择更改后如何更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28575591/

相关文章:

php - zend 站点也需要别名

javascript - 在 BackboneJS 中调用 render() 的适当方式

javascript - 在下拉列表中选择相同的值时触发事件?

php - 分析 API : automatic authentification

PHP-选择然后插入时是否需要fetch_array

php - Laravel:如何决定使用 server.php 还是 public/index.php

zend-framework - Zend .htaccess 配置在 Ubuntu 中不起作用

javascript - 状态设置方法没有影响主对象 - ReactJS

javascript - Nodejs : Promise object not resolving

mysql - Zend 主从 mysql