Search code examples
phpunlink

Passing a variable from button to js within the same file


This code is supposed to pass the variable in the button delFunction to the delFunction script and delete the file associated with the button. It is not passing the variable. I insert two alert statements below the script and they display on the resulting page as text with no variable name in the command. I have searched stackoverflow and every technique i've tried did not work. Please help...David

<!DOCTYPE html>
<html>
<head>
<title>Mark Nutt</title>
<script>
function delFunction(source)
{
    window.location = "?source="+source;
}
</script>
<?php
////////////////////////////////////
      echo "alert('copy({$_GET['source']},{$_GET['source']})');";
      echo "alert('unlink({$_GET['source']})');";
if(isset($_GET['source'])){
   if(unlink($_GET['source'])){
      echo '<script language="javascript">';
      echo "alert('{$_GET['source']} detached from email!');";
      echo'</script>';
   }

}
?>
</head>
<body>
<a href="#" onclick="MyWindow=window.open('http://www.davidsdomaindesign.com/marknutt/emails/emails.php','_self'); return true;"><font size="2" color="white"><input type="button" value="I'm Done" /></font></a><br />
<?php
 $files = glob("/home/davidsdo/public_html/marknutt/emails/attach/*.*");
 asort($files);
 for ($i=0; $i<count($files); $i++)
 {$num = $files[$i];
  $file = substr($num,50);
?>
<button onclick="delFunction('<?php echo $source='/home/davidsdo/public_html/marknutt/emails/attach/'; echo $file; ?>')" >
<input type="button" value="<?php echo $file ?>" /><br /><img src="http://www.davidsdomaindesign.com/marknutt/emails/attach/<?php echo $file ?>" alt="<?php echo $file ?>" width="125" height="125">
</button>
<?php
 }
?>
</body>
</html>

Solution

  • Instead of setting the variable inside the echo statement, try changing the definition of your button to this.

    $source = '/home/davidsdo/public_html/marknutt/emails/attach/' . $file;
    <button onclick="delFunction('<?php echo $source; ?>')" >