I have some code that is currently working fine.
On my main page I have an input form that calls an image upload script that populates a div and Ajax displays the image without reloading the page.
What I would like to do now is have a button on the page that says display the previous image rather than load a new one. This is what i have so far (but it doesn’t work)
Calling HTML/PHP page
[php]
Choose image to upload
<?php
//Select previous or no image
if (isset($_POST['none']))
{
$newimage = "none" ;
$_SESSION['newimage']= $newimage;
}
if (isset($_POST['previous']))
{
if(isset($_SESSION['newimage']))
$newimage = $_SESSION['newimage'];
else
{
$newimage="No Image";
}
}
?>
[/php]
Ajaximage.php
[php]
<?php
session_start();
$session_id='1'; //$session id
$path = "timage/";
$newimage = $_SESSION['newimage'] ;
//New stuff
if (isset($_POST['previous']))
{
echo "
![]()
";
}
//end of new stuff
$valid_formats = array("jpg", "png", "gif", "bmp");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(2056*1500))
{
$tfn = str_replace(" ", "_", $txt);
$tfn = substr($tfn, 0, 8);
$actual_image_name = time().$tfn.".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
//mysql_query("UPDATE users SET profile_image='$actual_image_name' WHERE uid='$session_id'");
echo "
![]()
";
$newimage = $actual_image_name ;
$_SESSION['newimage']= $newimage;
}
else
echo "failed";
}
else
echo "Image file size max 0.5 MB";
}
else
echo "Invalid file format..";
}
else
echo "Please select image..!";
exit;
}
?>
[/php]
All of the Ajaximage,php code executes instead of just the first bit. The bits that I have added are in the //New stuff comments. The rest of the code works fine when there is a new image to upload but I don’t want to fill the server with loads of identical images.
Any thoughts help / advice most welcome. (I am pretty new at PHP)
Thanks
Colin