I have tried looking through other posts here but I haven't been able to find a solution to my particular problem.
Issue: After selecting a checkbox or more and submitting nothing happens to the images and all form elements disappear.
Goal: I want the images selected to be delete when submitted to the $_POST array. Additionally, I want the remaining files to be renamed (like an array shift) For example: If I have 6 images, Image-3, and Image-5 to be deleted, image 6 should be renamed to image 5, and etc.
Here is my current code:
PHP:
if($_POST['delete_img']){
if ($images){
if (!empty($_POST['delete'])){
$delete = $_POST['delete'];
print_r($delete);
for ($i =(count($delete) - 1); $i >= 0; $i--){
// Determine the images to be deleted
$images_to_delete = "profiles/".$user_id."/".$user_id."-".$delete($i).".jpg";
// Delete the images
echo "Deleting image: " . $i;
recursiveDelete($images_to_delete);
for ($j = $delete($i); $j < $images; $j++){
echo "checkpoint";
// Rename the files
rename("profiles/".$user_id."/".$user_id."-".$j+1 . ".jpg");
// Decrement the images number for the db
$new_images_num = $images-1;
// Update the database
$image_info = array($new_image_num,$user_id);
$result = pg_execute($conn, "update_images", $image_info);
}
}
}
else{
$error .= "You must select an image to delete.";
}
}
else{
echo "There are no images to delete";
}
}
HTML:
<form id="uploadform" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="profile_images">
<?php
echo $profile_images;
?>
</div>
<br/>
<strong> Select image to upload: </strong>
<input name="uploadfile" type="file" id="uploadfile" />
<input type="submit" value="Upload" name="upload" />
<input type="submit" value="Delete" name="delete_img" />
<input type="submit" value="Save" name="save" />
</form>
I think this is what you are trying to do which is 1) delete any selected file(s), then reset all the names to incrementing. You will have to change the directory names and naming conventions:
function getFileList($dir)
{
// Filter out the inevitable dots..
$filter = array(".","..");
// Scan the target directory
$fileList = scandir($dir);
// Just return false if folder empty
if(empty($fileList))
return false;
// Return filtered array
return array_values(array_diff($fileList,$filter));
}
$dir = __DIR__.'/testimg';
if(!empty($_POST['delete'])) {
foreach($_POST['delete'] as $i => $dVal) {
if(!empty($_POST['delete'][$i])) {
$fName = $dir.'/user'.$i.".jpg";
if(is_file($fName)) {
if(unlink($fName))
echo 'Deleted: '.$fName;
}
}
}
// See if any files remain in folder
$files = getFileList($dir);
// Rename any files in the folder
if($files) {
if(count($files) > 0) {
$i = 1;
foreach($files as $name) {
if(rename($dir."/".$name, $dir.'/user'.$i.".jpg"))
$i++;
}
}
}
}
// Check one last time
$files = getFileList($dir);
?>
<form id="uploadform" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="profile_images">
<?php
if($files) {
$i = 1;
foreach($files as $key => $value) {
if(is_file($img = $dir."/".$value)) {
echo '<img src="'.str_replace(__DIR__,"",$img).'" style="max-height: 100px;" />IMAGE '.$i.'<input type="checkbox" name="delete['.$i.']" />';
$i++;
}
}
}
?>
</div>
<br/>
<strong> Select image to upload: </strong>
<input name="uploadfile" type="file" id="uploadfile" />
<input type="submit" value="Upload" name="upload" />
<input type="submit" value="Delete" name="delete_img" />
<input type="submit" value="Save" name="save" />
</form>
Related
here you see my code example:
<?php
session_start();
$arbeit = $_GET["arbeit"];
echo "Angelegte Prüfung: ";
echo $arbeit;
$damen = 0;
$herren = 0;
if (array_key_exists('add_Fem',$_POST)) {
$damen = 1; // BESETZT DAMEN
}
elseif (array_key_exists('sub_Fem',$_POST)) {
$damen = 0; // Reset counter
}
elseif (array_key_exists('add_Mal',$_POST)) {
$herren = 1; // BESETZT HERREN
}
elseif(array_key_exists('sub_Mal',$_POST)) {
$herren = 0; // Reset counter
}
?>
<form method='post'>
<input name='add_Fem' type="submit" value='Damen' class="button">
</form>
<form method='post'>
<input name='sub_Fem' type="submit" value='Reset Damen' class="button">
<h3><em>
<?php
if ($damen == 1) {
echo "DAMEN BESETZT!";
} else {
echo "DAMEN FREI!";
}
?>
</em></h3>
</form>
<form method='post'>
<input name='add_Mal' type="submit" value='Herren' class="button">
</form>
<form method='post'>
<input name='sub_Mal' type="submit" value='Reset Herren' class="button">
<h3><em>
<?php
if ($herren == 1) {
echo "HERREN BESETZT!";
} else {
echo "HERREN FREI!";
}
?>
</em></h3>
</form>
the problem is, when i'm try to set both to 1(male and female) the other variable gets reset to 0. And i dont know why they are affect each other!
Also PHP isset not solves my problem, same situation, that was my first try.
I hope you can give me some advice.
Thank you
i want make a view of array, in the first i need fill length of array for first form (this for how much is show) and then i use that to fill value of array then i show that.
<form method="POST" action="prak_9_1.php">
input length of array <input type="text" name="bilangan">
<input type="submit" value="Hasil">
second code is different file
<?php
$bilangan = $_POST['bilangan'];
$bil = $bilangan;
$array = array();
$n = 0;
?>
<form method="POST" action="prak_9_1.php">
<?php
for ($i=0; $i < $bilangan ; $i++) {
echo "input value of array : <input type='text' name='nilai_".$i."'><br>";
}
?>
<input type="submit" value="Hasil">
</form>
<?php
for ($i=0; $i < $bil ; $i++) {
$array[$n] = $_POST['nilai_'.$i];
$n++;
}
print_r($array);?>
sorry if my english is bad... thanks for ur helps...
first file :
<form method="POST" action="second.php">
input length of array <input type="text" name="bilangan">
<input type="submit" value="Hasil">
</form>
and second file :
<?php
if (isset($_POST['bilangan'])) {
echo '<form method="POST">';
for ($i = 0; $i <= $_POST['bilangan']; $i++) {
echo "input value of array : <input type='text' name='nilai[]'><br>";
}
echo '<input type="submit" value="Hasil">';
echo '</form>';
}
if (isset($_POST['nilai'])) {
print_r($_POST['nilai']);
}
?>
For a file management I use a foreach loop in which all the files are displayed. Now I want foreach to delete files with $_POST.
When I echo pathinfo($dir.'/'.$file, PATHINFO_DIRNAME).'/'.$file; in the foreach near each file, it shows me the exact location of the files.
Now for each file I want do something like this in the foreach loop:
if(isset($_POST['delete'])) {
unlink($_POST['unlink']);
}
<form method="post">
<input type="hidden" name="unlink" value="<?php pathinfo($dir.'/'.$file, PATHINFO_DIRNAME).'/'.$file; ?>" />
<input type="submit" name="delete" value="Delete">
</form>
I know this is not correct but i hope you understand that what i want to achieve is clear.
How can I do this?
DISCLAIMER
The method outlined below is 100% insecure and if the code makes it into the public realm then you can kiss your server goodbye.
For deleting one file you can do this:
index.php
<form method="POST" action="delete.php">
<input type="hidden" name="delete_path" value="/path/to/your/file.pdf">
File.pdf <button type="submit">Delete</button>
</form>
delete.php
<?php
$delete_result = false;
$unlink_error = '';
if(isset($_POST['delete_path']) && is_file($_POST['delete_path']))
{
$delete_result = unlink($_POST['delete_path']);
$unlink_error = error_get_last();
}
if($delete_result)
{
echo 'Successfully deleted '.$_POST['delete_path'];
}
else
{
echo 'Uh oh, Spaghettios :(<br><br>';
echo 'Last known error<br>'.$unlink_error;
}
I am use path unlink(FILE)
echo $new_file_dert = FILE . '/'.$template_name;
<form method="post">
<input type="hidden" name="unlink" value="<?php echo $new_file_dert; ?>" />
<input name="delete" type="submit" class="btn-delete submitdelete" value="Delete">
</form>
<?php
echo $new_file_dert = FILE . '/'.$template_name;
if( isset( $_POST['delete'] ) ) {
unlink($_POST['unlink']);
}
In this code when Google doc file is used to generate form we want seperate submit button for all but with this we form separete submit buttons .............................................................................................................................................................................................................................................................***********************************************************************************************************************************************************************************************************
$url='https://docs.google.com/xxxxxxxxxxxxxxxxxx';
$cat ='';
if (($handle = fopen($url, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$totalrows = count($data);
for ($row=0; $row<=$totalrows; $row++)
{
if (($row % 6 == 0) && (strlen($data[$row])>0))
{
$answer = $row + 1;
$response["status"] = 1;
$response["price"] = $data[$answer+4];
$prodname = explode(' ',$response["prodname"]);
$prodname = $prodname[0];
if($_COOKIE[$prodname]) { $quantity = $_COOKIE[$prodname]; } else { $quantity = "0"; }
if($response["subcat"] != 'Sub Category')
{
if($cat != $response["subcat"]){
echo '</div>';
echo '<div data-role="collapsible" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d" data-content-theme="c">';
echo '<h3>'.$response["subcat"].'</h3>';
echo '<div class="ui-grid-a"><div class="ui-block- a">'.$response["prodname"].' ('.$response["prodnamemarathi"].') - '.$response["quantity"].'<br/>Available in '.$month.'<br/>MRP: Rs.'.$response["price"].'</div>
<div class="ui-block-b">
<form action="?user='.$user.'" method="post"><input type="number" name="quantity" data-mini="true" value="'.$quantity.'" />
<input type="hidden" name="subcat" value="'.$response["subcat"].'">
<input type="hidden" name="price" value="'.$response["price"].'">
<input type="submit" value="Submit"></form></div></div>';
$cat = $response["subcat"];
}
else {
echo '<br/><div class="ui-grid-a"><div class="ui-block- a">'.$response["prodname"].' ('.$response["prodnamemarathi"].') - '.$response["quantity"].'<br/>Available in '.$month.'<br/>MRP: Rs.'.$response["price"].'</div>
<div class="ui-block-b">
<form action="?user='.$user.'" method="post"><input type="number" name="quantity" data-mini="true" value="'.$quantity.'"/><input type="hidden" name="subcat" value="'.$response["subcat"].'">
<input type="hidden" name="prodname" value="'.$response["prodname"].'">
<input type="hidden" name="prodnamemarathi" value="'.$response["prodnamemarathi"].'">
<input type="hidden" name="price" value="'.$response["price"].'">
<input type="submit" value="Submit">
</form></div></div>';
}
}
}
}
}
fclose($handle);
}
echo '</div>';
?>
give some details
Generate the form once, and just put the text input in the loop. Give them an array-style name.
<form action="page2" method="post">
<?php
while ($i <= 5):
?> <input type="text" name="size[]">
<?php endwhile; ?>
<input type="submit" name="submit" value="Review Order">
</form>
In page2, you can access $_POST['size'] as an array, which will contain all the inputs.
i wrote a piece of code to do multiple row deletions based on ticked check boxes.
Now i need to mordify it to do multiple row updates.
i have been failing to access the textbox values for each row.
here's my code, in general.
<?php
if (isset($_POST["SaveComments"]) && isset($_POST["SaveEachComment"]))
{
while(list($key, $val) = each($_POST["SaveEachComment"]))
{
if($val == 'commentbox')
{
// do the update here. $val contains the ID
}
}
}
?>
<form id="form1" name="form1" method="post" action="test.php">
<input type="checkbox" <?php echo 'name="SaveEachComment['.$row["comment"].']" ';?> value="commentbox" id="commentbox" />
<input type="text" name="rowcomment" size="55" value="<?php echo $comment;?>" />
<input type="submit" name="SaveComments" value="submit" />
</form>
I just added a for loop to print multiple form fields. Obviously your iteration would be as many as number of rows, so you can change that part of the code. But try this:
<?php
if (isset($_POST["SaveComments"]) && isset($_POST["SaveEachComment"]))
{
while(list($key, $val) = each($_POST["SaveEachComment"]))
{
if($val == 'commentbox')
{
echo $_POST['rowcomment'][$key] . "<br />\n";
// do the update here. $val contains the ID
}
}
}
?>
<form id="form1" name="form1" method="post" action="test.php">
<?php for ($i=0; $i<11; $i++) { ?>
<input type="checkbox" <?php echo 'name="SaveEachComment['.$i.']" ';?> value="commentbox" id="commentbox" />
<input type="text" name="rowcomment[<? echo $i?>]" size="55" value="<?php echo $comment;?>" />
<br />
<?php } ?>
<input type="submit" name="SaveComments" value="submit" />
</form>