public function actionAjaxUpdate()
{
$action = $_GET['act'];
if(!isset($_POST['selectedId']))
{
echo '<script> alert("No row is selected"); </script>';
}
else
{
$selectedAll = $_POST['selectedId'];
if(count($selectedAll)>0)
{
foreach($selectedAll as $selectedId)
{
$model=$this->loadCompositeModel($selectedId);
switch ($action) {
case 'Delete':
$model['account']->delete();
break;
case 'Active':
$model['account']->active = '1';
break;
case 'Inactive':
$model['account']->active = '0';
break;
default:
break;
}
$model['account']->save();
}
}
}
}
- 列表项
在此代码中,如果没有选择id, Alert将不起作用。谁来帮帮我。我试过很多,但js不工作。我在php中使用了js这是第一次当js不能在php
如果没有看到发出HTTP请求的代码,就很难说问题出在哪里。
它们最可能的解释是,当没有选择id时,selectedId
被设置为为空字符串。条件!isset($_POST['selectedId'])
将不会给您想要的结果。使用empty()
代替(并确保0
不是一个可接受的id值)。
除非您将响应注入回页面,否则Javascript将永远不会被计算。
如果你要做……
document.write('<script src="/path/to/phpscript.php">')
则响应将被附加到页面并评估。
但是,如果你……
$.ajax({
success: function(response) {
//Blah....
}
});
之类的东西,响应永远不会被求值,它作为字符串传递给你的成功函数。因此,您可以让PHP打印1
或0
以显示成功/失败-或者更好地返回Json对象
<?php
...
if(!isset($_POST['selectedId']))
{
$Ret->Success = false;
$Ret->Reason = "No row is selected";
header("Content-Type: application/json");
print json_encode($Ret)
}
?>
然后使用$.getJson
Ajax调用作为Js对象检索响应(参见本手册页)
似乎$_POST['selectedId']
是一个数组。那么你应该使用isset
来处理$_POST['selectedId']
的元素,例如if (!isset($_POST['selectedId'][0]))
编辑1:试试这个
$array_id = array_filter($_POST['selectedId']);
则使用if (!empty($array_id))
代替if (!isset($_POST['selectedId']))