-3

I am passing variables from a button into a myFunction. I can create an alert box showing the correct information but when I use these variables in a copy command it does nothing. My goal is to display images in a directory. The user can click on the button with that image in it and have it copy the image file from the /storage/ directory to the /attach/ directory. Can anyone help me with this please....Thanks

My complete file i've created is:

<!DOCTYPE html>
<html>
<head>
<title>Mark Nutt</title>
<script>
 function myFunction(greeting,greeting2,source,destination)
 {
<?php
    echo copy('"+source+"','"+destination+"');
?> 
    alert("copy('"+source+"','"+destination+"')");
    alert(greeting2 +' attached to email!');
    }
 </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/photos/storage/*.jpg");
 asort($files);
 for ($i=0; $i<count($files); $i++)
 {$num = $files[$i];
  $file = substr($num,51);
?>
<button onclick="myFunction('<?php echo $file ?>','<?php echo substr_replace($file,"",-4); ?>','<?php echo $source='/home/davidsdo/public_html/marknutt/photos/storage/'; echo $file; ?>','<?php echo $destination='/home/davidsdo/public_html/marknutt/emails/attach/'; echo $file; ?>')" >
<input type="button" value="<?php echo substr_replace($file,"",-4); ?>" /><br /><img src="http://www.davidsdomaindesign.com/marknutt/photos/storage/<?php echo $file ?>" alt="<?php echo $file ?>" width="125" height="125">
</button>
<?php
 }
?>
</body>
</html>
  • 1
    PHP runs on the server first. It outputs HTML and Javascript which then runs on the client! They never run together side-by-side. Submit a form as usual or use AJAX. – deceze Dec 12 '12 at 12:15
  • Duplicate of [Reference: Why does the PHP code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-code-in-my-javascript-not-work/13840431#13840431) – deceze Dec 12 '12 at 13:03

1 Answers1

1

You can not use javascript variable on php code. You should use a php function to do it. use this code insted:

<!DOCTYPE html>
<html>
<head>
<title>Mark Nutt</title>
<script>
function myFunction(greeting,greeting2,source,destination)
{
window.location = "?greeting="+greeting+"&greeting2="+greeting2+"&source="+source+"&destination="+destination;
}
</script>

<?php
////////////////////////////////////
if(isset($_GET['greeting'])){
   if(copy($_GET['source'],$_GET['destination'])){
      echo '<script language="javascript">';
      echo "alert('copy({$_GET['source']},{$_GET['destination']})');";
      echo "alert('{$_GET['greeting2']} attached to 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/photos/storage/*.jpg");
 asort($files);
 for ($i=0; $i<count($files); $i++)
 {$num = $files[$i];
  $file = substr($num,51);
?>
<button onclick="myFunction('<?php echo $file ?>','<?php echo substr_replace($file,"",-4); ?>','<?php echo $source='/home/davidsdo/public_html/marknutt/photos/storage/'; echo $file; ?>','<?php echo $destination='/home/davidsdo/public_html/marknutt/emails/attach/'; echo $file; ?>')" >
<input type="button" value="<?php echo substr_replace($file,"",-4); ?>" /><br /><img src="http://www.davidsdomaindesign.com/marknutt/photos/storage/<?php echo $file ?>" alt="<?php echo $file ?>" width="125" height="125">
</button>
<?php
 }
?>
</body>
</html>

to delete you can use this:

<script>
function delFunction(source)
{
    window.location = "?delete="+source;
}
</script>
<?php
if(isset($_GET['delete']){
    unlink($_GET['source']);
}
?>

but I prefer to move file to other directory instead of remove it(to log that who want to delete file) or use db to manage images.

Maysam
  • 34
  • 3
  • what would be the syntax if i wanted to delete (unlink) the file instead of copying it? – David Burke Dec 13 '12 at 05:44
  • the unlink didn't work, i had to add another right parand to the isset to match them out. i also changed my button statement to this below. please help. – David Burke Dec 13 '12 at 10:22
  • May you do not have sufficient privilege to remove file(check chmod). please check [this](http://stackoverflow.com/questions/5548986/unlink-file-exists-and-file-not-found) – Maysam Dec 13 '12 at 20:52
  • yes, i have total access to my directories – David Burke Dec 13 '12 at 22:58