Jump to content

[SOLVED] Positioning Basic Error Message (driving me insane!)


Recommended Posts

Hi All,

 

I'm trying to display an error message beneath an input field in my form.

 

This happens in the form process:

 

<?php
$filesize1=$HTTP_POST_FILES['ufile']['size'][0];

if($filesize1==0) 
{
$filesize1=FALSE;
//I'VE COMMENTED THIS OUT BECAUSE I DON'T WANT THE MESSAGE AT THE TOP OF MY PAGE
//$message_filesize1="You forgot to enter this image file!";
//echo "$message_filesize1";
}
else 
{
$filesize1=TRUE;
}
?>

 

This is how I want the error message to work:

 


    <input type="file" name="ufile[]"  id="ufile[]"/>
    <?php
     $filesize1=$HTTP_POST_FILES['ufile']['size'][0];
      if($filesize1==0) 
       {
        $filesize1=FALSE;
        $message_filesize1="You forgot to enter this image file!";
        echo "$message_filesize1";
       }
      else 
       {
        $filesize1=TRUE;
       } 
    ?>

 

Intead the error message already shows on the form before any input or hitting of the submit button. I have tried so many different angles but I can't get this to work. Please help a meagre newb.  ???

 

Did you read this mate

 

http://uk2.php.net/is_uploaded_file

 

Nope, but just did. Isn't this just checking whether or not the files were loaded?

 

I need to check whether or not the field is empty BEFORE the form begins the upload.

 

 

Here's my full code if it helps:

 

<?php

if (isset($_POST['submit']))

//PROCESS THE FORM WHEN THE SUBMIT BUTTON IS PRESSED

{ 

//VALIDATE THE INDIVIDUAL TEXT FIELDS AND DEFINE THEM AS TRUE OR FALSE BASED ON THE USER'S INPUT

if (strlen($_POST['make']) > 0)
		{$make=TRUE;}
else 	{$make=FALSE;
		$message_make=" *You forgot to enter the make!";}


if (strlen($_POST['model']) > 0)
		{$model=TRUE;}
else 	{$model=FALSE;
		$message_model=" *You forgot to enter the model!";
		}


if (strlen($_POST['price']) > 0)
		{$price=TRUE;}
else 	{$price=FALSE;
		$message_price=" *You forgot to enter the price!";}


if (strlen($_POST['engine']) > 0)
		{$engine=TRUE;}
else 	{$engine=FALSE;
		$message_engine=" *You forgot to enter the engine!";}


if (strlen($_POST['body']) > 0)
		{$body=TRUE;}
else 	{$body=FALSE;
		$message_body=" *You forgot to enter the body!";}


if (strlen($_POST['transmission']) > 0)
		{$transmission=TRUE;}
else 	{$transmission=FALSE;
		$message_transmission=" *You forgot to enter the transmission!";}


if (strlen($_POST['year']) > 0)
		{$year=TRUE;}
else 	{$year=FALSE;
		$message_year=" *You forgot to enter the year!";}


if (strlen($_POST['colour']) > 0)
		{$colour=TRUE;}
else 	{$colour=FALSE;
		$message_colour=" *You forgot to enter the colour!";}


if (strlen($_POST['mileagem']) > 0)
		{$mileagem=TRUE;}
else 	{$mileagem=FALSE;
		$message_mileagem=" *You forgot to enter the mileage in miles!";}


if (strlen($_POST['mileagekm']) > 0)
		{$mileagekm=TRUE;}
else 	{$mileagekm=FALSE;
		$message_mileagekm=" *You forgot to enter the mileage in kilometres!";}


if (strlen($_POST['owners']) > 0)
		{$owners=TRUE;}
else 	{$owners=FALSE;
		$message_owners=" *You forgot to enter the owner!";}


if (strlen($_POST['doors']) > 0)
		{$doors=TRUE;}
else 	{$doors=FALSE;
		$message_doors=" *You forgot to enter the doors!";}


if (strlen($_POST['location']) > 0)
		{$location=TRUE;}
else 	{$location=FALSE;
		$message_location=" *You forgot to enter the location!";}

        if (isset($_POST['info'])) 
        {
          $info = trim($_POST['info']);
          if ($info == "" || empty($info)) 
            {
            $message_info = "Please enter data for info!";
            }
        }



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



$filesize1=$HTTP_POST_FILES['ufile']['size'][0];

if($filesize1==0) 
{
$filesize1=FALSE;
//$message_filesize1="You forgot to enter this image file!";
//echo "$message_filesize1";
}
else 
{
$filesize1=TRUE;
}


$filesize2=$HTTP_POST_FILES['ufile']['size'][1];

if($filesize2==0) 
{
$filesize2=FALSE;
//$message_filesize2="You forgot to enter this image file!";
//echo "$message_filesize2";
}
else 
{
$filesize2=TRUE;
}


$filesize3=$HTTP_POST_FILES['ufile']['size'][2];

if($filesize3==0) 
{
$filesize3=FALSE;
//$message_filesize3="You forgot to enter this image file!";
//echo "$message_filesize3";
}
else 
{
$filesize3=TRUE;
}




///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



//IF ALL INPUT FIELDS ARE DEFINED AS TRUE
       		
if ($make && $model && $price && $engine && $body && $transmission && $year && $colour && $mileagem && $mileagekm && $owners && $doors && $location && $info && $filesize1 && $filesize2 && $filesize3)

	{	

//THEN VALIDATE FILE UPLOADS

//FIRST CHECK FILE TYPE BUT NOT SIZE

if (

     (
     //BEGIN IMAGE FILE 1
     (
     ($HTTP_POST_FILES['ufile']["type"][0] == "image/gif")
     || ($HTTP_POST_FILES['ufile']["type"][0] == "image/jpeg")
     || ($HTTP_POST_FILES['ufile']["type"][0] == "image/pjpeg")
      )
     //&&($HTTP_POST_FILES['ufile']["size"][0] < 50000)
     //END IMAGE FILE 1
     )

     &&

     (
     //BEGIN IMAGE FILE 2
     (
     ($HTTP_POST_FILES['ufile']["type"][1] == "image/gif")
     || ($HTTP_POST_FILES['ufile']["type"][1] == "image/jpeg")
     || ($HTTP_POST_FILES['ufile']["type"][1] == "image/pjpeg")
     )
     //&&($HTTP_POST_FILES['ufile']["size"][1] < 50000)
     //END IMAGE FILE 2
     )

     &&

     (
     //BEGIN IMAGE FILE 3
     (
     ($HTTP_POST_FILES['ufile']["type"][2] == "image/gif")
     || ($HTTP_POST_FILES['ufile']["type"][2] == "image/jpeg")
     || ($HTTP_POST_FILES['ufile']["type"][2] == "image/pjpeg")
     )
     //&&($HTTP_POST_FILES['ufile']["size"][2] < 50000)
     //END IMAGE FILE 3
     )

)


{

//DEFINE PATHS AS VARIABLES

$path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
$path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];
$path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2];

//NEXT CHECK IF EITHER OF THE FILES ALREADY EXIST

if (file_exists($path1) || file_exists($path2) || file_exists($path3))
     
     {
     echo "<b>ERROR</b></br></br>
     None of the selected image files have been uploaded as a result of either or both of the following incidents occurring:</br></br>
     1) One or more of the image files you attempted to upload already exist on the server. 
     Please ensure that you have not already uploaded the image file(s) and / or that each file name is unique.</br></br>
     Tip: Try naming the image files semantically after a combination of the make, model and year of the vehichle rather than a random series of letters and numbers.
     </br></br>
     Should you experience further technical difficulty please contact the web master:</br></br>
     [place button here to link to web master cms error log form]
     ";
     }


//NEXT POST THE IMAGES TO THE DESIGNATED FOLDER ON THE SERVER

else {

copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);

//PICK UP AND DEFINE INPUT AS INDIVIDUAL VARIABLES

                  $make = $_POST['make'];
                  $model = $_POST['model'];
                  $price = $_POST['price'];
                  $engine = $_POST['engine'];
                  $body = $_POST['body'];
                  $transmission = $_POST['transmission'];
                  $year = $_POST['year'];
                  $colour = $_POST['colour'];
                  $mileagem = $_POST['mileagem'];
                  $mileagekm = $_POST['mileagekm'];
                  $owners = $_POST['owners'];
                  $doors = $_POST['doors'];
                  $location = $_POST['location'];

//DEFINE ADDITIONAL VARIABLES

                  $now_datetime = date('Y-m-d h:i:s');
                  $ipaddress = getenv('REMOTE_ADDR');

//CONNECT TO RELEVANT DATABASE

                  include("dbinfo.php");
          mysql_connect(localhost,$username,$password);
          @mysql_select_db($database) or die( "Unable to establish a connection to the relevant database.");

//INSERT THE INPUT INTO DATABASE

$query = "INSERT INTO test VALUES ('','$make','$model','$price','$engine','$body','$transmission','$year','$colour','$mileagem','$mileagekm','$owners','$doors','$location','$info','$now_datetime','$ipaddress','$path1','$path2','$path1')";
mysql_query($query);

//NEXT DISPLAY A SUMMARY OF WHAT HAS BEEN UPLOADED


echo "This item has been successfully submitted to the server.</br></br>
The following image file have been appended to your uploaded:</br></br>
";

echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>"; 
//echo "File Size :".$HTTP_POST_FILES['ufile']['size'][0]." KB<BR/>"; 
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>"; 
//echo "<img src=\"$path1\" height=\"150\">";
echo "<P>";

echo "File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>"; 
//echo "File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>"; 
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>"; 
//echo "<img src=\"$path2\" height=\"150\">";
echo "<P>";

echo "File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>"; 
//echo "File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>"; 
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>"; 
//echo "<img src=\"$path3\" height=\"150\">";

echo "</br>It's best practice to confirm that your item has been submitted as desired</br></br>
[place button here to link to view relevant section]</br></br>
If you notice an error as a result of human input please find and delete the item indefinitely and reattempt its submission. 
Items should also be deleted as soon as they become redundant (e.g. vehichle sold)</br></br>
Should you experience further technical difficulty please contact the web master:</br></br>
[place button here to link to web master cms error log form]
";
}

}
else
  {
  echo "<b>Error</b></br></br>
  None of the selected image files have been uploaded as a result of either or both of the following incidents occurring:</br></br>
  1) One or more of the submitted file types is not *.gif, *.jpeg or *.pjpeg.</br></br>
  Click the back button on your browser to continue.
  ";
  }

//VERY IMPORTANT! EXIT(); WILL NO LONGER DISPLAY THE FORM
               exit();

	}

} 

//END FORM PROCESS

?>

<HTML>
<HEAD>
</HEAD>
<BODY>

<!--BEGIN FORM AND SET IT TO PROCESS SELF AND HANDLE MULTIPART/FORM-DATA-->

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">

<fieldset>

<?php

// IF ANY INDIVIDUAL INPUT FIELD IS DEFINED AS FALSE THEN DISPLAY ERROR MESSAGE 

/*
DO NOT INCLUDE || $filesize1 || $filesize2 || $filesize3
OTHERWISWE THE FORM WILL PRESENT THE BELOW ERROR MESSAGE TO THE USER
THE BELOW CONDITIONS CHECK FOR ERROR MESSAGES DEFINED IN THE FORM PROCESS. 
THE ERROR MESSAGES FOR FILE UPLOAD INPUT ELEMENTS ARE NOT DEFINED IN THE FORM PROCESS
*/

if ($message_make || $message_model || $message_price || $message_engine || $message_body || $message_transmission || $message_year || $message_colour || $message_mileagem || $message_mileagekm || $message_owners || $message_doors || $message_location || $message_info) 
echo '*Your request could not be sent because some of the information is missing.</br></br>'; 

?>
   
<ol>

<!--BEGIN REGULAR TEXT FIELDS-->

    <li>
    <label for="make">Make:</label>
    <input type="text" name="make" id="make" class="text" value="<?php if (isset($_POST['make'])) echo $_POST['make']; ?>"/>
    <?php if ($message_make) echo ''.$message_make.''; ?></br>
    </li>

    <li>
    <label for="model">Model:</label>
    <input type="text" name="model" id="model" class="text" value="<?php if (isset($_POST['model'])) echo $_POST['model']; ?>"/>
    <?php if ($message_model) echo ''.$message_model.''; ?></br>
    </li>

    <li>
    <label for="price">Price:</label>
    <input type="text" name="price" id="price" class="text" value="<?php if (isset($_POST['price'])) echo $_POST['price']; ?>"/>
    <?php if ($message_price) echo ''.$message_price.''; ?></br>
    </li>	

    <li>
    <label for="engine">Engine:</label>
    <input type="text" name="engine" id="engine" class="text" value="<?php if (isset($_POST['engine'])) echo $_POST['engine']; ?>"/>
    <?php if ($message_engine) echo ''.$message_engine.''; ?></br>
    </li>

    <li>
    <label for="body">Body Type:</label>
    <input type="text" name="body" id="body" class="text" value="<?php if (isset($_POST['body'])) echo $_POST['body']; ?>"/>
    <?php if ($message_body) echo ''.$message_body.''; ?></br>
    </li>

    <li>
    <label for="transmission">Transmission:</label>
    <input type="text" name="transmission" id="transmission" class="text" value="<?php if (isset($_POST['transmission'])) echo $_POST['transmission']; ?>"/>
    <?php if ($message_transmission) echo ''.$message_transmission.''; ?></br>
    </li>

    <li>
    <label for="year">Year:</label>
    <input type="text" name="year" id="year" class="text" value="<?php if (isset($_POST['year'])) echo $_POST['year']; ?>"/>
    <?php if ($message_year) echo ''.$message_year.''; ?></br>
    </li>

    <li>
    <label for="colour">Colour:</label>
    <input type="text" name="colour" id="colour" class="text" value="<?php if (isset($_POST['colour'])) echo $_POST['colour']; ?>"/>
    <?php if ($message_colour) echo ''.$message_colour.''; ?></br>
    </li>

    <li>
    <label for="mileagem">Mileage M:</label>
    <input type="text" name="mileagem" id="mileagem" class="text" value="<?php if (isset($_POST['mileagem'])) echo $_POST['mileagem']; ?>"/>
    <?php if ($message_mileagem) echo ''.$message_mileagem.''; ?></br>
    </li>

    <li>
    <label for="mileagekm">Mileage KM:</label>
    <input type="text" name="mileagekm" id="mileagekm" class="text" value="<?php if (isset($_POST['mileagekm'])) echo $_POST['mileagekm']; ?>"/>
    <?php if ($message_mileagekm) echo ''.$message_mileagekm.''; ?></br>
    </li>

    <li>
    <label for="owners">Owners:</label>
    <input type="text" name="owners" id="owners" class="text" value="<?php if (isset($_POST['owners'])) echo $_POST['owners']; ?>"/>
    <?php if ($message_owners) echo ''.$message_owners.''; ?></br>
    </li>

    <li>
    <label for="doors">Doors:</label>
    <input type="text" name="doors" id="doors" class="text" value="<?php if (isset($_POST['doors'])) echo $_POST['doors']; ?>"/>
    <?php if ($message_doors) echo ''.$message_doors.''; ?></br>
    </li>

    <li>
    <label for="location">Location:</label>
    <input type="text" name="location" id="location" class="text" value="<?php if (isset($_POST['location'])) echo $_POST['location']; ?>"/>
    <?php if ($message_location) echo ''.$message_location.''; ?></br>
    </li>

<!--BEGIN TEXT AREA-->

<li>
<label for="info">Additional Information:</label></br>
<textarea name="info" rows="5" cols="50"/>
<?php if (isset($_POST['info'])) echo stripslashes($_POST['info']); ?>
</textarea>
<?php if ($message_info) echo ''.$message_info.''; ?></br>
</li>


<!--BEGIN FILE UPLOADS-->

    <li>
    <label for "ufile[]">Image File 1:</label>
    <input type="file" name="ufile[]"  id="ufile[]"/>
    <!--NOTICE THAT THIS ERROR DOES NOT CALL ON A VARIABLE IN THE FORM PROCESS LIKE THE TEXT FIELDS.--> 
    <?php 
     $filesize1=$HTTP_POST_FILES['ufile']['size'][0];
      if($filesize1==0) 
       {
        $filesize1=FALSE;
        $message_filesize1="You forgot to enter this image file!";
        echo "$message_filesize1";
       }
      else 
       {
        $filesize1=TRUE;
       } 
    ?>
    </li>


    <li>
    <label for "ufile[]">Image File 2:</label>
    <input type="file" name="ufile[]"  id="ufile[]"/>
    <!--NOTICE THAT THIS ERROR DOES NOT CALL ON A VARIABLE IN THE FORM PROCESS LIKE THE TEXT FIELDS.-->
    <?php 
     $filesize2=$HTTP_POST_FILES['ufile']['size'][1];
      if($filesize2==0) 
       {
        $filesize2=FALSE;
        $message_filesize2="You forgot to enter this image file!";
        echo "$message_filesize2";
       }
      else 
       {
        $filesize2=TRUE;
       }
    ?>   
    </li>

    <li>
    <label for "ufile[]">Image File 3:</label>
    <input type="file" name="ufile[]"  id="ufile[]"/>
    <!--NOTICE THAT THIS ERROR DOES NOT CALL ON A VARIABLE IN THE FORM PROCESS LIKE THE TEXT FIELDS.--> 
    <?php 
     $filesize3=$HTTP_POST_FILES['ufile']['size'][2];
      if($filesize3==0) 
       {
        $filesize3=FALSE;
        $message_filesize3="You forgot to enter this image file!";
        echo "$message_filesize3";
       }
      else 
       {
        $filesize3=TRUE;
       } 
    ?>
    </li>


    
    <li>
    <input name="submit" type="submit">
    </li>



</ol>

</fieldset>

</form>	

</BODY>
</HTML>

anyway back to your original question leave this line where it is:

$message_filesize1="You forgot to enter this image file!";

 

but this line:

echo "$message_filesize1";

 

replace with

if(isset($message_filesize1))
{
echo "$message_filesize1";
$message_filesize1 = "";

}

 

and put that section where you want the error message to appear

That worked blueman378. Much thanks.

 

I also tried this and it worked:

 

<?php 
     $filesize1=$HTTP_POST_FILES['ufile']['size'][0];
      if($filesize1==0 && isset($_POST['submit'])) 
       {
        $message_filesize1="You forgot to enter this image file!";
        echo "$message_filesize1";
       }
    ?>

 

 

You'll notice I just added && isset($_POST['submit']) to the if part of the statement.

 

Is this way correct too?

 

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.