Jump to content

Confusion With Forms And Form Methods


enO87

Recommended Posts

Hello,

 

I have a page called uploadimage.php where I'm displaying a list of pictures for a real estate listing. Each picture has a radio button and a checkbox to either make it the new main image, or to delete it. The code works, but the problem is that when I click the submit button, the images don't properly update to reflect the changes until I refresh the page. And in the case of the deletions, the checkboxes seem to be sticky, so that when I do end up reloading the page, my code deletes other images.

 

Here is my code:

 

<?php
// Group 12
// Course Code: INTN3201
// Date: 27/09/2012

$title = "Image Upload";
$date = "November 11, 2012";
$filename = "uploadimage.php";
$banner = "";
$description = "This page allows an agent to upload a picture.";

require 'header.php';

if($_SERVER["REQUEST_METHOD"] == "GET"){

unset($_POST['delete']);
unset($_POST['main']);

if(!is_authed()){
$_SESSION['message'] = "You must be logged in to upload an image. Please log in and try again.";
header('location: ./login.php');
}

$_SESSION['listingid'] = $_GET['listing_id'];

$imagename = "";	


}else if($_SERVER["REQUEST_METHOD"] == "POST"){

$conn = db_connect();
$sql = "SELECT id, image_count FROM estate_listings WHERE id =" . $_SESSION['listingid'];
$result = pg_query($conn, $sql);


$img_count = pg_fetch_result($result, 0, 'image_count');

$main = $_POST["main"];
$deletions = $_POST['delete'];


/*MAKE NEW MAIN IMAGE*/

if($main!=""){ //radio button is checked



$old_img_path = 'images/' . $_SESSION['listingid'] . '/' . $_SESSION['listingid'] . '_' . $main . '.jpg';

rename($old_img_path, 'images/' . $_SESSION['listingid'] . '/temp.jpg');

for($i = ($main - 1); $i >= 1; $i--){

 $old = $i;
 $new = $i + 1;
 $old_img_path = 'images/' . $_SESSION['listingid'] . '/' . $_SESSION['listingid'] . '_' . $old . '.jpg';
 $new_img_path = 'images/' . $_SESSION['listingid'] . '/' . $_SESSION['listingid'] . '_' . $new . '.jpg';
 rename($old_img_path, $new_img_path);		
}

rename('images/' . $_SESSION['listingid'] . '/temp.jpg', 'images/' . $_SESSION['listingid'] . '/' . $_SESSION['listingid'] . '_1.jpg');

header('location: ./uploadimage.php');

}


/*DELETE IMAGES*/

if(count($deletions) > 0){

for($i = 0;$i < count($deletions);$i++){

 $delete_path = 'images/' . $_SESSION['listingid'] . '/' . $_SESSION['listingid'] . '_' . $deletions[$i] . '.jpg';

 unlink($delete_path);
						 }

for($i = 1; $i <= $img_count; $i++){

 if(!file_exists('images/' . $_SESSION['listingid'] . '/' . $_SESSION['listingid'] . '_' . $i . '.jpg')){
 for($j = ($i + 1); $j <= $img_count; $j++){

 if(file_exists('images/' . $_SESSION['listingid'] . '/' . $_SESSION['listingid'] . '_' . $j . '.jpg')){

 rename('images/' . $_SESSION['listingid'] . '/' . $_SESSION['listingid'] . '_' . $j . '.jpg', 'images/' . $_SESSION['listingid'] . '/' . $_SESSION['listingid'] . '_' . $i . '.jpg');
 }
 }
 }	
}
$img_count = $img_count - count($deletions);



/*for($i = 1;$i < $new_img_count);$i++){

 if(file_exists('images/' . $_SESSION['listingid'] . '/' . $_SESSION['listingid'] . '_' . $i)){
 temp[$i] =
 }
}*/
$conn = db_connect();
$sql = "UPDATE estate_listings SET image_count = '$img_count' WHERE id = '" . $_SESSION['listingid'] . "'";
pg_query($conn,$sql);
}


unset($_SESSION["listingid"]);// = $listing_id;
unset($_POST['delete']);
unset($_POST['main']);
}
?>
<p>Select an an existing house listing from the list below to modify its images. </p>
<p> </p>
<table align="center">
<form method="get" name="upddate_listing_form" action="./uploadimage.php">
<tr>
<td>
Listing ID:
</td>
<td>
<?php
$conn = db_connect();
$sql = "SELECT id FROM estate_listings WHERE userid = '".$_SESSION['userid']."'";

$result = pg_query($conn, $sql);

if(pg_num_rows($result) > 0){
$html = "<select name='listing_id'>
<option value='-1'>--Select Listing--</option>";

while ($row = pg_fetch_row($result))
{
$html .= "<option value=".$row[0].">".$row[0];
}
$html .= "</select>";
}

echo $html;

?>
</td><td>
<input class="button" type="submit" value="Select Listing"/>
</td>
</tr>
</form>
</table>


<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<?php create_imagelist(); ?>
<input type="submit" value=" Submit " name="modify" />
</form>

</div>

<?php include './footer.php'; //FOOTER ?>

 

I'm guessing the problem has something to do with this:

 

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<?php create_imagelist(); ?>
<input type="submit" value=" Submit " name="modify" />
</form>

 

create_imagelist() is a function that creates the list of images with the radio buttons and checkboxes.

 

edit: here is the code for create_imagelist():

 

function create_imagelist()
{


$html = "";
if($_SESSION['listingid'] != ""){
 $conn = db_connect();
 $sql = "SELECT id, image_count FROM estate_listings WHERE id =" . $_SESSION['listingid'];
 $result = pg_query($conn, $sql);


 $img_count = pg_fetch_result($result, 0, 'image_count');
 $imagename = $_SESSION['listingid'] . "_";
 if(pg_num_rows($result) > 0){


  $html = "<table>"; 
  for ($i = 1; $i <= $img_count; $i++)
  {
   $html .= "<tr>";

   $html .= "<td><img src=\"./images/" . $_SESSION['listingid'] . "/" . "$imagename";
   $html .= $i;
   $html .= ".jpg\" alt=\"sample image\" height=\"125\" width=\"200\" /></td>  
  <td><input type='radio' name=\"main\" value='$i' />
  <label>Make main image.</label><br/>
  <input type='checkbox' name='delete[]' value='$i' />
  <label>Delete image.</label></td>";

   $html .= "</tr>";
  }
  $html .= "</table>";


 }

 echo $html;
}
}

 

I know I've only just registered but I've been trying to fix this for hours and I'm really getting desperate. Any help would be greatly appreciated.

 

-enO

Edited by enO87
Link to comment
Share on other sites

Retrieving the list of images should be one of the first things you do on this page since it is the feature of the page.

 

The create_imagelist function should return $html... not echo it. Echo the return of the function..... dont echo inside the function.

 

Try doing that and see what happens. I'm not positive that will indeed fix it... definitely a shot in the dark.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.