i had a problem with my php script. im using php to show all my picture in folder "gambar" and then i want to add button command in every picture i've showed to other folder i already make. im using POST syntax and had trouble with it. button show up but not executing command to moving file i've choose. this is script i've use, could somebody help me with this?
<?php
$folder = "gambar"; //folder tempat gambar disimpan
$handle = opendir($folder);
echo '<table cellspacing="5" cellpadding="10" width="90%" >';
echo '<tr>';
$i = 1;
while(false !== ($file = readdir($handle))){
if($file != '.' && $file != '..'){
echo '<td style="border:1px solid #000000;" align="center" >
<div class="hvrbox">
<img src="gambar/'.$file.'" alt="Mountains" class="hvrbox-layer_bottom"
width="100%" />
<div class="hvrbox-layer_top">
<div class="hvrbox-text"> '.$file.'.</div>
</div>
</div>
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="js/hover-box.js"></script>
</select>
<form method="post">
<input type="hidden" select name="datae" value="'.$file.'" />
>>> <input type="submit" name="upload" value="Upload" />
---
<input type="submit" name="delete" value="Delete" /><<<
</form>
<br /><b>NAMA FILE:</b> '.$file.'</td>';
if(($i % 1) == 0){
echo '</tr><tr>';
}
$i++;
}
}
echo '</tr>';
echo '</table>';
?>
and to progress POST im using this script
<?php
// Turn off all error reporting
error_reporting(0);
if (isset($_POST['upload']) && isset($_POST['datae'])) {
rename('gambar/'.$_POST['datae'], 'Upload/'.$_POST['datae']);
echo "<meta http-equiv='refresh' content='1'>";
}
if (isset($_POST['delete']) && isset($_POST['datae'])) {
rename('gambar/'.$_POST['datae'], 'delete/'.$_POST['datae']);
echo "<meta http-equiv='refresh' content='1'>";
}
?>
cz when i use
<form method="post">
<input type="hidden" select name="datae" value="'.$file.'" />
so did
($_POST['datae']
still not moving the files to other folder, its like the script was working but no files moved to destination folder.
PS. im sorry cz my English is BAD. but i wish you could understand what i mean.
You forgot to include the <form>
. So when you click your "submit" button there's nothing to submit. Wrap a form element around your buttons and include an input for the file name:
echo '
...
<form method="post">
<input type="hidden" name="file" value="'.$file.'" />
>>> <input type="submit" name="upload" value="Upload" />
---
--- <input type="submit" name="delete" value="Delete" /><<<
</form>
...';
Also in your server-side code you are incorrectly getting a value from the POST array:
$_POST['.$file.']
The syntax is broken, and it appears that you're trying to use the file name itself as the key in the POST array. Posted data is in key/value pairs. You want to use the name of the value in order to get the value. Notice the name
attribute in my hidden input above. That's what you'd use:
$_POST['file']
A few important things to note which are outside the scope of the question being asked but definitely relevant:
action
attribute of the <form>
element and handle going back and forth between the pages.