-3

我将变量从按钮传递到 myFunction。我可以创建一个显示正确信息的警报框,但是当我在复制命令中使用这些变量时,它什么也不做。我的目标是在目录中显示图像。用户可以单击包含该图像的按钮,然后将图像文件从 /storage/ 目录复制到 /attach/ 目录。任何人都可以帮我解决这个问题....谢谢

我创建的完整文件是:

<!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>
4

1 回答 1

1

您不能在 php 代码上使用 javascript 变量。您应该使用 php 函数来执行此操作。使用此代码:

<!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>

要删除你可以使用这个:

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

但我更喜欢将文件移动到其他目录而不是删除它(记录想要删除文件的人)或使用 db 来管理图像。

于 2012-12-12T12:41:24.590 回答