我的视图中有一个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>
但是在调用父网址时失败了。 这输出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> 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>
答案 0 :(得分:1)
尝试使用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>