Jump to content

Unstable result from form


CGTroll

Recommended Posts

I have a form that gets a image name from a table if present, if not certain checks should be done when the form is passed.

I have three options for placement of the image on the page (1, 2 and 3), and the option not to have an image (4). If a user already have an image and pick not to have one, the old file should be deleted. If the user just wish to change the placement of the image on the page, the old image is kept, but the layout value is updated in the DB. The problem is now that when I wish to only change the placement of the image, it sometimes work and sometimes don't. It seems like the name of the old image not always is passed, and it works and fails on all three options.

The check on the passed form is done depending on the value of the imagename. If($imagename) then do so and so, if not, do so and so. Seems like the if($filename) sometimes fails to be passed from the form so that the form thing the placemnt value should be 4 since no image is present, event when it is. Any idea what this could be?

 

(In the form I have a JS run on the radio button options of the  placement, to hide the option to upload a new image or not. If 4 (no image is checked) the option to pick a file to upload is hidden, 1, 2 or 3 = show.)

 

 

Thanks

Link to comment
Share on other sites



<table width="80%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" width="20%"><img src="graphics/layout_1_left.png" alt="" width="56" height="56"><br />
<input type="radio" id="imgOptFlag1" name="fpage_layout" value="1" <?PHP if ($fpage_layout == 1) { echo ' checked="CHECKED"' ; } ?> onclick="toggleImgOpt(event)"></td>
<td align="center" width="20%"><img src="graphics/layout_2_right.png" alt="" width="56" height="56"><br />
<input type="radio" id="imgOptFlag1" name="fpage_layout" value="2" <?PHP if ($fpage_layout == 2) { echo ' checked="CHECKED"' ; } ?> onclick="toggleImgOpt(event)"></td>
<td align="center" width="20%"><img src="graphics/layout_3_center.png" alt="" width="56" height="56"><br />
<input type="radio" id="imgOptFlag1" name="fpage_layout" value="3" <?PHP if ($fpage_layout == 3) { echo ' checked="CHECKED"' ; } ?> onclick="toggleImgOpt(event)"></td>
<td align="center" width="20%"><img src="graphics/layout_4_no_image.png" alt="" width="56" height="56"><br />
<input type="radio" id="imgOptFlag0" name="fpage_layout" value="4" <?PHP if ($fpage_layout == 4) { echo ' checked="CHECKED"' ; } ?> onclick="toggleImgOpt(event)"></td>
</tr>
<tr>
<td align="center">Bilde til venstre</td>
<td align="center">Bilde til høyre</td>
<td align="center">Bilde i senter</td>
<td align="center">Ingen bilde</td>
</tr>
</table>
<p>
<?PHP
if ($fpage_image_old) { 
    echo "
	Following image is already uploaded:<br>
	<table>
                <tr>
                <td>
	<table width='100' border='0' cellspacing='2' cellpadding='2'>
                 <tr>
                 <td align='left' valign='top' class='credits1'><img src='../images/fpage/$fpage_image_old' width='100'>
                 </td>
                 <tr>
                 </table>
	Name of image file: $fpage_image_old
	</td>
                <tr>
                </table>
	<input name='old_image' type='hidden' value='$fpage_image_old'>
	" ; 
} else { 
      echo "No image is uploaded! " ; 
} ?>
</p>
 <?PHP if($fpage_layout = 1 || 2 || 3) { ?>
                 <div id="imageUploadOption" style="display:none; margin-left:20px">
	<p> Change image:<br>
	<input name="myFile" type="file">
	</p>
	</div>
	<?PHP 
	} ?>

That was the important part from the form. JS is not shown, but you see where it applies.

 

Here are the section from the processing script that the error occures in.



// Check if file was uploaded	
if (is_uploaded_file($_FILES['myFile']['tmp_name'])) 
	{
		$fileCheck = "1";
	} 
else 
	{
		$fileCheck = "2";
	}

// Check if old image
if($old_image)
	{
	// Check if it needs to be deleted
		$checkVars2 = array(4, null);
		if(in_array($layout, $checkVars2))
			{
			// Delete image
				if (!unlink($DirToDeleteIn.$old_image))
					{
					// If it failed to delete
   						echo ("OBS! Klarte ikke slette $DirToDeleteIn$old_image , ERROR line 241");
									
					// Pop-up-message
						echo "<script>";
						echo "alert(\"Advarsel! Innholdet ble IKKE oppdatert fordi tilhoereden fil $DirToDeleteIn$imageToDelete heller ikke kunne slettes. Vennlisgt kontakt webmaster for hjelp!\");";
						echo "window.location = \"administrate.php?pid=$pid\";";
						echo "</script>";
						exit;
   					} 
				else
					{
					// If file was deleted, show message
						echo ("Deleted $DirToDeleteIn$old_image");
					}
			}
	} 
else 
	{
	// If there are no old file, check if layout is set wrong.
		$checkVars3 = array(1, 2, 3);
		if(in_array($layout, $checkVars3))
			{
			// If layout is set to 1, 2 or 3, check if this is correct by checking if new file is present
				if($fileCheck == 2)
					{
					// No new file was added, layout was set incorectly, fix it.
						$layout = 4;
						echo "New layout was set to $layout";
					}
			}
}

Its the if($old_image) that sometimes works and sometimes fails. When it fails it changes the layout value to 4 and thinks there are no old image present. 

 

 

I'm sure there are better way to do this, but this is as far as my PHP knowledge reaches. :)

 

Thanks!

Link to comment
Share on other sites

It's a page consisting of multiple sections of script, so I took out what I though was important. Here is the section that handles the data from the form:

				// Get data from form
					$content_id = $_POST["content_id"];
					$layout = $_POST["fpage_layout"];
					$fpage_title = $_POST["fpage_title"];
					$myFile = $_FILES['myFile'];
					$old_image = $_POST['old_image'];
					$fpage_content = $_POST["fpage_content"];
					$date = $_POST["date"];
					
				// Define options for deleting image file
					$table = "content_frontpage";
					$image = 'fpage_image';
					define("UPLOAD_DIR", "../images/fpage/");
					$imageToDelete = $r["fpage_image"];
					$DirToDeleteIn = '../images/fpage/';
				// END Check if file was uploaded

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.