Image lost when form validates.


#1

Hey everyone,

On my page, I have a form to the left, and the form content echoed to the right. This includes the image, which is echoed correctly. The image is found using the folder directory, and a database name reference.

I have written a script to validate my form. On validation, the errors collected are echoed above my form.
This works, but stops my image from being displayed. Instead, the image try’s to locate the text ‘Array’.

Before validation, the correct image name is found, and the image is displayed. If I remove the validation, the image stays echoed correctly, and my form information is also updated.

Not sure why this is happening :frowning:


#2

Well, I would guess it is a coding error.

Perhaps if you show some of the code around the form and the validation code, we could find the error for you.

Please place the code inside of the # and PHP tags above. Help us help you…


#3

Validation!

[php]
//Validation to stop incorrect data from being input into the database.
$errorsMessage = “”;

//If there is no image, run an error. If there is an image, run a check to see if the file exists.
if($_FILES['banner_image']['name'] == "") {
}else if(file_exists($target)){
	$errorsMessage .= "File Exists!";
}else if(!preg_match("/.(gif|jpg|png)$/i", $banner_image['name'])){
	$errorsMessage .= "Wrong image format or file";
}else{
//Check the image size is under 500KB.
if($banner_size > 500000){
	$errorsMessage .= "File is larger than 500KB";
}
}



//Create if and else statements to validate each piece of form data.
//Checks to see if the banner_text field is empty.
if (empty($banner_text)){
	$errorsMessage .= "Banner Body Required!";
		}else{
			if(strlen($banner_text) > 350) {
				$errorsMessage .= "The <b>Banner Body</b> is too Long!";
		}
}

//if !(No) there are no errors.
if (!empty($errorsMessage)){

//Code that collects and displays the arrays errors is found in the html body. This is above the form code.
				  
}else{

//run rest of script
[/php]

Form

[php]

<?php if($errorsMessage){ //for every error collected in the $errors array. Echo each each error in a local variable of $error. //The errors are echoed above the site at the top left of the page. These are contained in a styled box so the error is easy to see. echo '
' . $errorsMessage . '
'; }
						?>
    					<form action="banner_home.php" method="post" enctype="multipart/form-data" >
      						<div>
        						<label for="bannerImage">Banner Image:</label>
       							 <br />
        						<input type="file" name="banner_image" id="banner_image" />
      						</div>
      							<br />
     						<div>
       							<label for="body">Banner Body:</label>
       							 <br />
        						<textarea name="banner_text" id="banner_text" rows="16" cols="40"><?php echo $banner_text; ?></textarea>
     						 </div>
     							 <br />
     							 <input type="submit" name="submit" value="Update"/>
    					</form>
  					</div>
				</div>
				<div id='contentRight'>
 					<div id='content2'>
   						<div style="color:#FFF;"><p><b>Banner - Home Page:</b></p></div>
						<?php
							echo "<img src='images/banner/$banner_image' title='lol' alt='lol' />";
                  			echo $banner_text; 
						?>
 					</div>

[/php]

There you go :slight_smile:


#4

Well, first, this code:
[php]
//If there is no image, run an error. If there is an image, run a check to see if the file exists.
if($_FILES[‘banner_image’][‘name’] == “”) {
}else if(file_exists($target)){
$errorsMessage .= “File Exists!”;
}else if(!preg_match("/.(gif|jpg|png)$/i", $banner_image[‘name’])){
$errorsMessage .= “Wrong image format or file”;
}else{
//Check the image size is under 500KB.
if($banner_size > 500000){
$errorsMessage .= “File is larger than 500KB”;
}
}
[/php]
doesnt do anything if no input is given… Something like this would work:
[php]
//If there is no image, run an error. If there is an image, run a check to see if the file exists.
if($_FILES[‘banner_image’][‘name’] == “”) {
$errorsMessage .= “No banner image given!”;
}else if(file_exists($target)){
$errorsMessage .= “File Exists!”;
}else if(!preg_match("/.(gif|jpg|png)$/i", $banner_image[‘name’])){
$errorsMessage .= “Wrong image format or file”;
}else{
//Check the image size is under 500KB.
if($banner_size > 500000){
$errorsMessage .= “File is larger than 500KB”;
}
}
[/php]
Also, this code is blank… Was this just a sample or the actual code? :
[php]
//if !(No) there are no errors.
if (!empty($errorsMessage)){
//Code that collects and displays the arrays errors is found in the html body. This is above the form code.
}else{
//run rest of script
[/php]
If this is live code, it has no ending bracket and nothing is done in this section.
(delete it or show what it is doing!)

Lastly, this code:
[php]

<?php echo "lol"; echo $banner_text; ?>

[/php]
simply shows some text, and whatever is inside $banner_text … Try this for this part:
[php]

<?php echo "lol"; echo $banner_text; ?>

Have no idea if these comments will help, but, should…
[/php]


#5

For the first piece of code, I want to write an input that gives the user an option to select or not select an image, as they are only updating the banner text, or image if they want to change it.

For the 2nd piece of code the actual code that went in there was found above the form.
[php]


<?php
if($errorsMessage){
//for every error collected in the $errors array. Echo each each error in a local variable of $error.
//The errors are echoed above the site at the top left of the page. These are contained in a styled box so the error is easy to see.
echo ‘
’ . $errorsMessage . ‘
’;
}
						?>[/php]

Because I wanted the information to echo there instead. Not sure thats right.

The final bit of code I changed, but the image still disappears on validation.


#6

Well, in this code, there are still some errors:
//If there is no image, run an error. If there is an image, run a check to see if the file exists.
if($_FILES[‘banner_image’][‘name’] == “”) {
$errorsMessage .= “No banner image given!”;
}else if(file_exists($target)){
$errorsMessage .= “File Exists!”;
}else if(!preg_match("/.(gif|jpg|png)$/i", $banner_image[‘name’])){
$errorsMessage .= “Wrong image format or file”;
}else{
//Check the image size is under 500KB.
if($banner_size > 500000){
$errorsMessage .= “File is larger than 500KB”;
}
}
Should be:
[php]
//If there is no image, run an error. If there is an image, run a check to see if the file exists.
if($_FILES[‘banner_image’][‘name’] == “”) {
$errorsMessage .= “No banner image given!”;
}else if(!file_exists($target)){
$errorsMessage .= “File Does Not Exist!”;
}else if(!preg_match("/.(gif|jpg|png)$/i", $banner_image[‘name’])){
$errorsMessage .= “Wrong image format or file”;
}elseif($banner_size > 500000){
$errorsMessage .= “File is larger than 500KB”;
}else{
//FILE IS GOOD! HERE you need to save the file and change the $banner_image to be the new filename
}
[/php]
So, it appears that after the new banner is selected, you didn’t change the variable pointing to it. I think it is $banner_image. In the posts you gave us, I could not see where this was set… Good luck…


#7

Is there an alternative I can use?

I want to run a check, that allows the user to select an image if they want to change it. But if they don’t want to change it. No image needs to be picked.

The rest of the code also runs in the else of this

[php]
//if !(No) there are no errors.
if (!empty($errorsMessage)){
//Code that collects and displays the arrays errors is found in the html body. This is above the form code.
}else{
//run rest of script
[/php]

So I don’t want it to be part of the image upload validation. I want it to run after all of the validation checks.

That was why I left an error out after: if($_FILES[‘banner_image’][‘name’] == “”) {

But, I know thats not a good way of coding. Is there a way of writing a validation that gives the user the option of selecting an image. If they don’t select an image, the validation is ignored and the rest of the code is run. If they do pick an image, the file exists, file type, and file size validation is run.

If I can figure this out, I might be one step closer to fixing my problem. Thanks again Ernie!


#8

Sorry, Patov2, I have been crazy busy. Trying to help some programmers again… Did you get your project working? Let me know if you still need help.