Reputation: 213
I've just created check button, which onclick do ajax script which sends data to php and php to database. I need that it would send with right task_log_task
(it's id of the task). Similar script I found in this file is here, when editing right row:
$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>");
}
As you can see task_id=' . $a['task_id'] . '
. So ive created that check button
$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>";
I made this $currentTasken=$a['task_id']; $currentUser=$AppUI->user_id;
And tried to send current data to my ajax script like this javascript:insertData('. $currentTasken .', '.$currentUser.')"
But when I see in FireBug:
Parametersapplication/x-www-form-urlencodedDo not sort
currentTasken
$currentTasken
currentUser
$currentUser
Source
currentUser=$currentUser ¤tTasken=$currentTasken
Is this. Here is my ajax script:
<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>
Upvotes: 1
Views: 69
Reputation: 90834
Your JavaScript code does not have access to these two variables because JS and PHP live in different places (respectively on the user's browser and the server).
So what you need to do is make these PHP variables accessible to JavaScript. For this, a simple way is to print them inside your <script>
tag as shown below.
In xmlhttp.send
, you also need to make sure you use these new variables:
<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>
Edit: I see you're actually already sending these variables via PHP but, likewise, you need to escape them with json_encode
(currently, I don't think the generated JS code is valid), and then insert them properly in xmlhttp.send
Upvotes: 1