简体   繁体   中英

JavaScript / dont know how to use variable

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&amp;a=addedit&amp;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'] . ' task_id=' . $a['task_id'] . ' 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; $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 &currentTasken=$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 &currentTasken=$currentTasken");
        }

</script>

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) + "&currentTasken=" + 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM