Jump to content

Problems executing PHP after a form has been submitted..


basquille

Recommended Posts

Hi all,

 

First post so be easy on me!

 

Below is a PHP page containing a form, some basic HTML and a PHP script (the PHP script is used to upload an image). The script and page works in general in that it has successfully render the page and the form area can upload the image and add the location of it to the database.

 

But the problem is that after carrying out the upload, I need to reload the page again for the image to appear. For reasons I won't go into here, I don't want to use the javascript "location.reload();" method to refresh the entire page.

 

I have figured out that when I load the page, it successfully enters the resultset containing the image locations, and does the task subsequently being drawn. But when I carry out an upload of an image on the form, that resultset is not entered again. Is it a problem with re-connecting to the database or that the connection has died?

 

The important area is where there is am '!!!! IMPORTANT !!!!' comment above it.. the rest is merely just to show the process. The area in question will work (as it enters the resultset) when the page is first loaded, but not when the form is submitted and the page is subsequently reloaded (as it doesn't enter the resultset).

 

Any suggestions or comments are very much welcome and appreciated.

 

PS - my database settings, that are correct, are stored in the 'db_include.php' file.

 

Any suggestions

 

<?php

include 'db_include.php';
include_once('thumbnail.inc.php');

if (isset($_POST['propertyID']) == true)
{
$propertyID = $_POST['propertyID'];
}
else if (isset($_GET['propertyID']) == true)
{
$propertyID = $_GET['propertyID'];
}

$errorMsg = "";

if ($_SERVER['REQUEST_METHOD'] == "POST")
{
    $uploaddir = "uploads";
$valid = true;

    $imgfile = $_FILES["file"];
    $imgfile_size = $_FILES["file"]["size"];
    $imgfile_name = $_FILES["file"]["name"];
$imgfile_tmp = $_FILES["file"]["tmp_name"];

    $pext = getFileExtension($imgfile_name);
    $pext = strtolower($pext);
    if (($pext != "jpg")  && ($pext != "jpeg") && ($pext != "gif") && ($pext != "png"))
    {
       $errorMsg .= "<b>ERROR</b>: Image Extension Unknown.<br>";
       $errorMsg .= "The file you uploaded had the following extension: $pext";
       
       $valid = false;
    }

  	$final_filename = time() . "." . $pext;
    $newfile = $uploaddir . "/" . $final_filename;
    $newthumb = $uploaddir . "/th_" . $final_filename;
    if (!copy($imgfile_tmp,$newfile)) 
    {
       $errorMsg = "<b>ERROR</b> Could not copy file";
       unlink($imgfile);
       
       $valid = false;
    }
    
    $thumb = new Thumbnail($newfile);
$thumb->resize(120,90);
$thumb->save($newthumb,100);
            
    /*== delete the temporary uploaded file ==*/
    unlink($imgfile);
    
// insert into a database
    if ($valid == true)
    {
    	$query = "INSERT INTO images_table (property_id,image_fname,image_full_location,image_thumb_location) VALUES (";
	$query .= "$propertyID,";
	$query .= "'$imgfile_name',";
	$query .= "'$newfile',";
	$query .= "'$newthumb')";
	mysql_query($query) or die($query);
    }
    
   	mysql_close();
}

function getFileExtension($str)
{
        $i = strrpos($str,".");
        if (!$i) { return ""; }

        $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);

        return $ext;
}

?>
</head>
<body>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000">
<input type="hidden" name="propertyID" id="propertyID" value="<?php echo $propertyID; ?>">

<table border=0 cellspacing=3 cellpadding=5>
<tr>
    <td width=50%><font face='Arial' color='#000000' size=3>Upload Image:</font></td>
    <td width=50%><input type="file" name="file" id="file"></td>
    </tr>
    <br>
    <tr>
    <td colspan=2 align=right><input type="submit" value="Upload Image"></td>
    </tr>
</table>
    </form>

<p><?php echo $errorMsg; ?></p>
<table border=0 cellpadding=6 cellpadding=4>
<tr><td colspan=3><font face='Arial' color='#000000' size=3><b>Images:</b></font></td></tr>

<?php

        // !!!!!! IMPORTANT !!!!!!!!!!!!!
        // WORKS WHEN PAGE IS FIRST LOADED,
        // BUT NOT WHEN FORM IS SUBMITTED!
$query = "SELECT * FROM images_table ";
$query .= "WHERE property_id = ";
$query .= "'$propertyID' AND is_deleted = 0";
echo $query;
$result = mysql_query($query);
$i = 0;

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
	echo "<h1>IN RESULTSET!</h1>";

	$i = $i + 1;
	$imgID = $row['image_id'];
	$imgFullLoc = $row['image_full_location'];
	$imgThumbLoc = $row['image_thumb_location'];
        $imgIsPrimary = $row['primary_image'];[/b]

	if($i == 1)
	{
		echo "<tr>";
	}

	echo "<td><a target=_blank TITLE=Enlarge href=" . $imgFullLoc . ">";
	echo "<img border=0 src=" . $imgThumbLoc . " WIDTH=120 HEIGHT=90 ALT=Image></a><br/>";
	echo "<input style='width: 120px' type=button value=Delete onclick=DeleteImage($imgID)><br/>";

	if($imgIsPrimary == 0)
		echo "<font size=2 face=Arial color=#000000>Set As Primary</font> <input type=radio id=" . $imgID . " name=primary ONCLICK=UpdatePrimaryImg('$imgID','$propertyID');>";		
	else
		echo "<font size=2 face=Arial color=#000000>Set As Primary</font> <input type=radio id=" . $imgID . " name=primary CHECKED ONCLICK=UpdatePrimaryImg('$imgID','$propertyID');>";		

	echo "</td>";

	if($i == 3)
	{
		echo "</tr>";
		$i = 0;
	}
}

?>

</table>
</body>
</html>

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.