如何将PHP代码编写到JavaScript / Ajax中?

问题描述:

I want to know how to write a pice of PHP code into JavaScript/Ajax.

This is my PHP code:

   if ($folder = opendir('data/Tasklist/')) {

  while (false !== ($file = readdir($folder))) {
  if ($file != '.' && $file != ".."){


   $data=file_get_contents("data/Tasklist/".$file);

   $poc=explode(";",$data);

  echo '<li class="taskli">
    <button  id="'. $file . '"   class="Del"> Delete </button>
   '. $poc[0] . " " . $poc[1] . '<div class="hidinfo">' . $poc[2] . '</div></li>';

    }
    }
  closedir($handle);
 }

And i want to write : id="'. $file . '" inside this code:

 $.post( "data/remove.php",{HERE})      

Since you're storing the $file variable in the <button> id, you can grab it from there:

$('.Del').click(function(){
    var file = $(this).attr('id');
    $.post( "data/remove.php",{id:file});
    return false;
});