Page 1 of 1

Display error message on website page

Posted: Tue Jun 26, 2018 11:16 am
by Dash
I am using PHP to upload images from a website page. The script I have gives user external error and success messages as an 'echo' statement. I would rather display the messages on the same page just below the upload button. I have tried multiple online searches with no success. Yet I have seen many websites that do exactly what I want. How can I do this?

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>

Re: Display error message on website page

Posted: Tue Jun 26, 2018 1:40 pm
by hyper
Put your echo statement after the button(s)

Re: Display error message on website page

Posted: Tue Jun 26, 2018 3:46 pm
by Dash
Won't that cause the message to display on a new window? I don't want a new window. I want the message to print within the web page rather than causing a new window to open with the message.

Re: Display error message on website page

Posted: Tue Jun 26, 2018 6:46 pm
by hyper
Won't that cause the message to display on a new window?
Why would it?

Try it, see what happens!

Re: Display error message on website page

Posted: Thu Jun 28, 2018 12:13 pm
by Dash
I'm new to PHP and I am not sure what you are telling me to do. The PHP code I showed above is called from the HML and is contained in a file called upload.php the HTML looks like:

<div class="upload-area">
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</div>

Can you show me what you mean?

Re: Display error message on website page

Posted: Thu Jun 28, 2018 12:45 pm
by hyper

Re: Display error message on website page

Posted: Thu Jun 28, 2018 1:21 pm
by Dash
Thanks. I have read a lot of that site already, but I still do not follow exactly what you are suggesting. I tried taking the 'if' statements where the error checking has the echos out of the upload.php file and placing them as stand-alone PHP after the buttons. But, it caused a lot of syntax errors. Can you lead me a little further?

Re: Display error message on website page

Posted: Thu Jun 28, 2018 3:08 pm
by hyper
But, it caused a lot of syntax errors. Can you lead me a little further?
Fix the syntax errors and learn from the experience, forms can get quite complex to deal with, you might be better off taking a step back and looking at the basics of PHP before you try to tackle something which can destroy your web site quickly. Unless you can do the basics, you will struggle with this.

Re: Display error message on website page

Posted: Thu Jun 28, 2018 3:34 pm
by Dash
Okay, thanks. I will see what I can do.