我刚刚创建了检查按钮,其中onclick执行ajax脚本,该脚本将数据发送到php和php到数据库。我需要它会发送右task_log_task
(它是任务的id)。在编辑右行时,我在此文件中找到的类似脚本就在这里:
$canEdit = getPermission('tasks', 'edit', $a['task_id']);
$canViewLog = getPermission('task_log', 'view', $a['task_id']);
if ($canEdit) {
$s .= ("\n\t\t".'<a href="?m=tasks&a=addedit&task_id=' . $a['task_id'] . '">'
. "\n\t\t\t".'<img src="./images/icons/pencil.gif" alt="' . $AppUI->_('Edit Task')
. '" border="0" width="12" height="12" />' . "\n\t\t</a>");
}
正如您所见task_id=' . $a['task_id'] . '
。所以我创建了检查按钮
$currentTasken=$a['task_id'];
$currentUser=$AppUI->user_id;
echo $currentTasken;
if ($canEdit) {
$s .= ("\n\t\t".'<a href="#">'
. "\n\t\t\t".'<img src="./images/icons/tick.png" alt="' . $AppUI->_('Check')
. '" border="0" width="12" height="12" onclick="javascript:insertData('. $currentTasken .', '.$currentUser.')" />' . "\n\t\t</a>");
}
$s .= "\n\t</td>";
我做了这个$currentTasken=$a['task_id']; $currentUser=$AppUI->user_id;
并尝试将当前数据发送到我的ajax脚本,如javascript:insertData('. $currentTasken .', '.$currentUser.')"
但是当我在FireBug中看到时:
Parametersapplication/x-www-form-urlencodedDo not sort
currentTasken
$currentTasken
currentUser
$currentUser
Source
currentUser=$currentUser ¤tTasken=$currentTasken
这是。这是我的ajax脚本:
<script type="text/javascript">
function insertData($currentTasken, $currentUser)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","modules/tasks/datafile.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("currentUser=$currentUser ¤tTasken=$currentTasken");
}
</script>
答案 0 :(得分:1)
您的JavaScript代码无法访问这两个变量,因为JS和PHP位于不同的位置(分别位于用户的浏览器和服务器上)。
因此,您需要做的是使JavaScript可以访问这些PHP变量。为此,一种简单的方法是在<script>
标记内打印它们,如下所示。
在xmlhttp.send
中,您还需要确保使用这些新变量:
<script type="text/javascript">
// Note that you should use `json_encode` to make sure the data is escaped properly.
var currentTasken = <?php echo json_encode($currentTasken=$a['task_id']); ?>;
var currentUser = <?php echo json_encode($currentUser=$AppUI->user_id); ?>;
function insertData(currentTasken, currentUser)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","modules/tasks/datafile.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
// Here, use the JS variables but, likewise, make sure they are escaped properly with `encodeURIComponent`
xmlhttp.send("currentUser=" + encodeURIComponent(currentUser) + "¤tTasken=" + encodeURIComponent(currentTasken));
}
</script>
编辑:我发现你实际上已经通过PHP发送这些变量了,但同样地,你需要用json_encode
来逃避这些变量(目前我还没有认为生成的JS代码是有效的),然后在xmlhttp.send