JavaScript/不知道如何使用变量
我刚刚创建了check按钮,它单击doajax脚本,将数据发送到php,并将php发送到数据库。我需要它将发送右JavaScript/不知道如何使用变量,javascript,php,Javascript,Php,我刚刚创建了check按钮,它单击doajax脚本,将数据发送到php,并将php发送到数据库。我需要它将发送右任务\日志\任务(它是任务的id)。编辑右行时,我在该文件中找到的类似脚本如下: $canEdit = getPermission('tasks', 'edit', $a['task_id']); $canViewLog = getPermission('task_log', 'view', $a['task_id']); if ($canEdit) { $s .= ("\n\
任务\日志\任务
(它是任务的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>");
}
我做了这个$currentTasken=$a['task_id']$currentUser=$AppUI->user\u 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>
函数insertData($currentTasken,$currentUser)
{
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
open(“POST”,“modules/tasks/datafile.php”,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
send(“currentUser=$currentUser¤tTasken=$currentTasken”);
}
您的JavaScript代码无法访问这两个变量,因为JS和PHP位于不同的位置(分别位于用户的浏览器和服务器上)
因此,您需要做的是使这些PHP变量可供JavaScript访问。为此,一种简单的方法是在
标记中打印它们,如下所示
在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>
//请注意,您应该使用“json_encode”来确保正确转义数据。
var currentTasken=;
var currentUser=;
函数insertData(currentTasken、currentUser)
{
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
open(“POST”,“modules/tasks/datafile.php”,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
//在这里,使用JS变量,但同样地,请确保使用'encodeURIComponent'正确转义它们`
xmlhttp.send(“currentUser=“+encodeURIComponent(currentUser)+”¤tTasken=“+encodeURIComponent(currentTasken));
}
编辑:我看到您实际上已经通过PHP发送了这些变量,但同样,您需要使用
json_encode
对它们进行转义(目前,我认为生成的JS代码无效),然后将它们正确插入xmlhttp.send
尝试:onclick=“insertData({$currentTasken},{$currentUser}”)“/>这里是所有日志id 1493 1494 1465 1491 1496 1380 1381 1382 1383 1385 1386 1387 1389 1408 1409 1410 1411 1412 1413 1414 1415。即使我试着计算电流,仍然需要las one 1415。。。这是照片
<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>