我是 cake 和 mysql 的新手,正在尝试创建一个简单的工作跟踪应用程序。我想为每个工作都有一个下拉框,其中包含一个工作状态列表。当用户更改框中的事件项时,我想将其保存到数据库中。
如能提供有关如何处理此问题的任何帮助,我们将不胜感激。以下是我到目前为止所做的尝试:
我如何使用从我的数据库表中的枚举中获取的选项在 View 中创建一组表单:
<?php $id = count($jobs)-1; ?>
<?php for ($job = count($jobs)-1; $job >= 0; --$job): ?>
<tr>
<td>
<?php echo $this->Form->input('status'.(string)$id, array('type'=>'select', 'class' => 'statusSelect','label'=>'', 'options'=>$states, 'default'=>$jobs[$job]['Job']['Status'])); ?>
</td>
我正在使用 jquery 脚本为每个下拉菜单设置一个更改监听器并在我的 Controller 中调用一个操作:
$(".statusSelect").change(function(){
//Grab job number from the id of select box
var jobNo = parseInt($(this).attr('id').substring(6));
var value = $(this).val();
$.ajax({
type:"POST",
url:'http://localhost/projectManager/jobs',
data:{ 'id': jobNo,
'status':value},
success : function(data) {
alert(jobNo);// this alert works
},
error : function() {
//alert("false");
}
});
});
我的 Controller 中有这个功能:
public function changeState($id = null, $status = null) {
//I don't think the id and status are actually
//being placed as arguments to this function
//from my js script
}
谢谢!!!
最佳答案
您正在发布到 /projectManager/jobs
,它对应于 ProjectManagerController::jobs()
。
您的函数声明为 public function changeState($id = null, $status = null)
。假设 changeState(..)
是 ProjectManagerController
中的一个函数,这对应于 /projectManager/changeState/$id/$status
。
您需要切换 AJAX 发送到的 URL。您可以执行以下操作:
url:'http://localhost/projectManager/changeState/'+jobNo+'/'+value'
,删除 data{}
并将函数保留为是,或者你可以做
url:'http://localhost/projectManager/changeState'
,保留数据{}
,把你的函数改成changeState()
然后在 changeState()
中使用 $this->request->data
访问数据。
我猜你还有另一个函数,jobs()
,这就是 AJAX 正常工作并且生成 alert
的原因。
关于javascript - 将对下拉框的更改保存到 CakePHP 中的数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19865976/