Jump to content

[SOLVED] Error messages showing when not supposed to be...


webmaster1

Recommended Posts

Hi All,

 

I have a minor problem with my error messages. The second error message (checking for non-numeric input) pops up even when the text field is blank.

 

<?php
//ERROR 1: FIRST CHECK THAT THE FIELD IS NOT EMPTY
if (strlen($_POST['price']) > 0)
{$price=TRUE;}
else {$price=FALSE; $message_price=" *You forgot to enter the price!"; echo "$message_price";}
//ERROR 2: NEXT CHECK THAT THE INPUT IS NUMERIC
if (is_numeric($_POST['price']))
{$price=TRUE;}
else {$price=FALSE; $message_price2=" *Please enter numeric values only!"; echo "$message_price2";}
?>

 

I tried playing around with isset but to no effect. Any takers?

Link to comment
Share on other sites

so...you expect a variable that doesn't exist to be numeric?

 

Nope. I expect a variable that doesn't exist to result in the first error message.

 

If a variable does exist then I expect it to be numeric which will be flagged by the second error message.

 

Or did that just go over my head?

Link to comment
Share on other sites

By chance I tried this out and it works:

 

<?php
//ERROR 1
        if (strlen($_POST['price']) > 0)
		{$price=TRUE;}
else 	{$price=FALSE;
		$message_price=" *You forgot to enter the price!";}
//ERROR 2
        if ($price)
             {
              if (is_numeric($_POST['price']))
              {$pricenumericcheck=TRUE;}
              else {$pricenumericcheck=FALSE;
      $message_pricenumericcheck=" *Please enter numeric values only!";
              echo "$message_pricenumericcheck";}
             }

?>

 

It just seems so long winded.

Link to comment
Share on other sites

well your original code has 2 independent if statements. Unless you tie the second one into the first one somehow, the second one is going to be evaluated regardless of what the first one is.

 

But anyways, not sure why you using multiple boolean vars for one variable check.  I suspect that you're eventually going to have some kind of if statement that looks something like this:

 

if ($price == true && $pricenumericcheck == true && $pricerangecheck == true && $priceformatcheck == true && $thepriceisright == true) {

  // we're good to go

}

 

well if any one of them fail then user has to start over in the first place, from this code you show, so just use one boolean.  Or better yet, simply check to see if you have an error message variable (and on that note, for the same reason, why are you using multiple error vars like that?  At least use an array...)

 

my suggestion:

if (!$_POST['price'])) {
  $error[] = "You forgot to enter a price";
}
if (!is_numeric($_POST['price'])) {
  $error[] = "Please enter numeric values only";
}

if($error) {
   // display the error(s) using a loop, send $error back to form and list it there, or whatever you do in the event of fail
} else {
   // the user is not a moron, please proceed here
}

Link to comment
Share on other sites

I suspect that you're eventually going to have some kind of if statement that looks something like this

 

Yep, you're exactly right.

 

I managed to get the two together like so (an if within an if but multiple booleans as you noted nonetheless):

 

<?php
if (strlen($_POST['price']) > 0)
		{$price=TRUE;
                                if (is_numeric($_POST['price']))
                                {$pricenumericcheck=TRUE;}
                                else {$pricenumericcheck=FALSE;
                        $message_pricenumericcheck=" *Please enter numeric values only!";
                                echo "$message_pricenumericcheck";}
                       }
else 	{$price=FALSE;
		$message_price=" *You forgot to enter the price!";}
?>

 

// display the error(s) using a loop, send $error back to form and list it there, or whatever you do in the event of fail

 

Your suggestion is so much neater and shorter. To learn this method what keywords should I google? I tried "display error using loop" but that doesn't seem to pull up the results I need.

 

 // the user is not a moron, please proceed here

 

Classic.  ;D

 

Here's my full code btw. Not because I want you to examine anything in particular, I just wanted to give you an idea of how longwinded an approach I've taken with the error messages.

 

<?php

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

//PROCESS THE FORM WHEN THE SUBMIT BUTTON IS PRESSED

{ 

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

//REGULAR TEXT FIELDS
        
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;
                                if (is_numeric($_POST['price']))
                                {$pricenumericcheck=TRUE;}
                                else {$pricenumericcheck=FALSE;
                        $message_pricenumericcheck=" *Please enter numeric values only!";
                                echo "$message_pricenumericcheck";}
                       }
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!";}

//TEXT AREA FIELD

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

//UPLOAD FILE FIELDS

//CHECK IF UPLOAD FILE FIELD 1 IS EMPTY

        $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;
        }

//CHECK IF UPLOAD FILE FIELD 1 IS EMPTY

        $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;
        }

//CHECK IF UPLOAD FILE FIELD 3 IS EMPTY

        $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;
        }

//CHECK IF ANY THE FILES ARE THE SAME:

        $filenamecheck1= $HTTP_POST_FILES['ufile']['name'][0];
        $filenamecheck2= $HTTP_POST_FILES['ufile']['name'][1];
        $filenamecheck3= $HTTP_POST_FILES['ufile']['name'][2];

        $filesize1=$HTTP_POST_FILES['ufile']['size'][0];
        $filesize2=$HTTP_POST_FILES['ufile']['size'][1];
        $filesize3=$HTTP_POST_FILES['ufile']['size'][2];

        if 
          (
               (($filenamecheck1 == $filenamecheck2) ||  ($filenamecheck2 == $filenamecheck3)  || ($filenamecheck1 == $filenamecheck3))
               && ((!$filesize1==0 && isset($_POST['submit'])) && (!$filesize2==0 && isset($_POST['submit'])) && (!$filesize3==0 && isset($_POST['submit'])))
          )
            {
             $filenamecheck_message = "one ore more of the files are the same";
             //echo "$message_filenamecheck";
             $message_filenamecheck = TRUE;
             $filenamecheck1= FALSE;
             $filenamecheck2= FALSE;
             $filenamecheck3= FALSE;
            }  

        else 
           {
            $message_filenamecheck = FALSE;
            $filenamecheck1= TRUE;
            $filenamecheck2= TRUE;
            $filenamecheck3= TRUE;         
           }

//CHECK IF FILE 1 IS NOT A *.JPEG OR *.GIF

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

      if   (   (
           ($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)
      )

      { 
       $filetypecheck1=TRUE;
      }

      else
       {
      if (!($filesize1 == 0))
       {
       $filetypecheck1=FALSE;
       $message_filetypecheck1="The first of the sumbitted files is not of a *.JPG ! *.GIF file type!";
       //echo "$message_filetypecheck1";
       }
      }

//CHECK IF FILE 2 IS NOT A *.JPEG OR *.GIF

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

      if   (   (
           ($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)
      )

      { 
       $filetypecheck2=TRUE;
      }

      else
      {
       if (!($filesize2 == 0))
      {
       $filetypecheck2=FALSE;
       $message_filetypecheck2="The second of the sumbitted files is not of a *.JPG ! *.GIF file type!";
       //echo "$message_filetypecheck2";
      }
      }

//CHECK IF FILE 3 IS NOT *.JPEG OR *.GIF

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

      if   (   (
           ($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)
     )

     { 
      $filetypecheck3=TRUE;
     }

     else
     {
      if (!($filesize3 == 0))
      {
      $filetypecheck3=FALSE;
      $message_filetypecheck3="The third of the sumbitted files is not of a *.JPG ! *.GIF file type!";
      //echo "$message_filetypecheck3";
      }
      }


//CHECK IF FILE 1 EXISTS ALREADY
    
     $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
     $filesize1=$HTTP_POST_FILES['ufile']['size'][0];

     if (
        (file_exists($path1)) && 
        (!($filesize1 == 0))
       )
      {
        $fileexistcheck1=FALSE;
        $message_fileexistcheck1="The first file already exist on the server!";
        //echo "$message_fileexistcheck1";
      }
     else
      {
        $fileexistcheck1=TRUE;
      }

//CHECK IF FILE 2 EXISTS ALREADY
    
     $path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];
     $filesize2=$HTTP_POST_FILES['ufile']['size'][1];

     if (
        (file_exists($path2)) && 
        (!($filesize2 == 0))
       )
      {
        $fileexistcheck2=FALSE;
        $message_fileexistcheck2="The second file already exist on the server!";
        //echo "$message_fileexistcheck2";
      }
     else
      {
        $fileexistcheck2=TRUE;
      }

//CHECK IF FILE 3 EXISTS ALREADY
    
     $path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2];
     $filesize3=$HTTP_POST_FILES['ufile']['size'][2];

     if (
        (file_exists($path3)) && 
        (!($filesize3 == 0))
       )
      {
        $fileexistcheck3=FALSE;
        $message_fileexistcheck3="The third file already exist on the server!";
        //echo "$message_fileexistcheck3";
      }
     else
      {
        $fileexistcheck3=TRUE;
      }
  
//IF ALL INPUT FIELDS ARE DEFINED AS TRUE / VALIDATED
       		
if 
($make && $model && $price && $pricenumericcheck && $engine && $body && $transmission && $year && $colour && $mileagem && $mileagekm && $owners && $doors && $location && $info && $filesize1 && $filesize2 && $filesize3 && $filenamecheck1 && filenamecheck2 && filenamecheck3 && $filetypecheck1 && $filetypecheck2 && $filetypecheck3 && $fileexistcheck1 && $fileexistcheck2 && $fileexistcheck3)

	{

//FIRST 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 POST THE IMAGES TO THE DESIGNATED FOLDER ON THE SERVER

                  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

//CONNECT TO RELEVANT DATABASE

//THE CONNECTION MUST PRECEDE THE mysql_real_escape_string FUNCTION.

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

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

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


//DEFINE ADDITIONAL VARIABLES

/*
PHP uses unix timestamps for all its date functionality. 
It has methods to convert these timestamps into pretty much any text format you could want but internally it uses the timestamp format.
A timestamp is simply an unsigned integer. 
Specifically, it&#8217;s the number of seconds that have elapsed since midnight on January 1st 1970 (greenwich mean time).
MySQL has three date types for use in columns. These are DATETIME, DATE, and TIMESTAMP.
DATETIME EXAMPLE: YYYY-MM-DD HH:MM:SS (e.g. 2006-12-25 13:43:15)
DATE EXAMPLE: YYYY-MM-DD (e.g. 2006-12-25)
*/


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

//CONNECTION WAS 'ERE

//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(),'$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]
                  ";

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

               exit();

//CLOSE: IF ALL INPUT FIELDS TRUE:

	}

//CLOSE: OVERALL VALIDATION:

} 

?>

<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 

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 || $message_filesize2 || $message_filesize1  || $message_filesize2 || $message_filesize3 || $message_filesize1 || $message_filesize2 || $message_filesize3 || $message_filenamecheck || $message_filetypecheck1 || $message_filetypecheck2 || $message_filetypecheck3 || $message_fileexistcheck1 || $message_fileexistcheck2 || $message_fileexistcheck3) 
//
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 
    //CHECK IF UPLOAD FILE FIELD 1 IS EMPTY
      $filesize1=$HTTP_POST_FILES['ufile']['size'][0];
      if($filesize1==0 && isset($_POST['submit'])) 
       {
        $filesize1=FALSE;
        $message_filesize1="You forgot to enter this image file!";
        echo "$message_filesize1";
       }
      else 
       {
        $filesize1=TRUE;
       } 
    
      //CHECK IF FILE 1 IS NOT A *.JPEG OR *.GIF

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

      if   (   (
           ($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)
      )

      { 
       $filetypecheck1=TRUE;
      }

      else
       {
      if (!($filesize1 == 0))
       {
       $filetypecheck1=FALSE;
       $message_filetypecheck1="The first of the sumbitted files is not of a *.JPG ! *.GIF file type!";
       echo "$message_filetypecheck1";
       }
      } 
     
     //CHECK IF FILE 1 EXISTS ALREADY
    
     $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
     $filesize1=$HTTP_POST_FILES['ufile']['size'][0];

     if (
        (file_exists($path1)) && 
        (!($filesize1 == 0))
       )
      {
        $fileexistcheck1=FALSE;
        $message_fileexistcheck1="The first file already exist on the server!";
        echo "$message_fileexistcheck1";}
     else
      {
        $fileexistcheck1=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 && isset($_POST['submit'])) 
       {
        $filesize2=FALSE;
        $message_filesize2="You forgot to enter this image file!";
        echo "$message_filesize2";
       }
      else 
       {
        $filesize2=TRUE;
       }

     //CHECK IF FILE 2 IS NOT A *.JPEG OR *.GIF

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

      if   (   (
           ($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)
      )

      { 
       $filetypecheck2=TRUE;
      }

      else
      {
       if (!($filesize2 == 0))
      {
       $filetypecheck2=FALSE;
       $message_filetypecheck2="The second of the sumbitted files is not of a *.JPG ! *.GIF file type!";
       echo "$message_filetypecheck2";
      }
      }
     //CHECK IF FILE 2 EXISTS ALREADY
    
     $path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];
     $filesize2=$HTTP_POST_FILES['ufile']['size'][1];

     if (
        (file_exists($path2)) && 
        (!($filesize2 == 0))
       )
      {
        $fileexistcheck2=FALSE;
        $message_fileexistcheck2="The second file already exist on the server!";
        echo "$message_fileexistcheck2";}
     else
      {
        $fileexistcheck2=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 && isset($_POST['submit'])) 
       {
        $filesize3=FALSE;
        $message_filesize3="You forgot to enter this image file!";
        echo "$message_filesize3";
       }
      else 
       {
        $filesize3=TRUE;
       }
    //CHECK IF FILE 3 IS NOT *.JPEG OR *.GIF

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

      if   (   (
           ($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)
     )

     { 
      $filetypecheck3=TRUE;
     }

     else
     {
      if (!($filesize3 == 0))
      {
      $filetypecheck3=FALSE;
      $message_filetypecheck3="The third of the sumbitted files is not of a *.JPG ! *.GIF file type!";
      echo "$message_filetypecheck3";
      }
      }
    //CHECK IF FILE 3 EXISTS ALREADY
    
     $path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2];
     $filesize3=$HTTP_POST_FILES['ufile']['size'][2];

     if (
        (file_exists($path3)) && 
        (!($filesize3 == 0))
       )
      {
        $fileexistcheck3=FALSE;
        $message_fileexistcheck3="The third file already exist on the server!";
        echo "$message_fileexistcheck3";}
     else
      {
        $fileexistcheck3=TRUE;
      }
    ?>


    <?php
    //CHECK IF ANY THE FILES ARE THE SAME:

        $filenamecheck1= $HTTP_POST_FILES['ufile']['name'][0];
        $filenamecheck2= $HTTP_POST_FILES['ufile']['name'][1];
        $filenamecheck3= $HTTP_POST_FILES['ufile']['name'][2];

        $filesize1=$HTTP_POST_FILES['ufile']['size'][0];
        $filesize2=$HTTP_POST_FILES['ufile']['size'][1];
        $filesize3=$HTTP_POST_FILES['ufile']['size'][2];

        if 
          (
               (($filenamecheck1 == $filenamecheck2) ||  ($filenamecheck2 == $filenamecheck3)  || ($filenamecheck1 == $filenamecheck3))
               && ((!$filesize1==0 && isset($_POST['submit'])) && (!$filesize2==0 && isset($_POST['submit'])) && (!$filesize3==0 && isset($_POST['submit'])))
          )
            {
             $filenamecheck_message = "one ore more of the files are the same";
             echo "$filenamecheck_message";
             $filenamecheck_message = TRUE;
             //$filenamecheck1= FALSE;
             //$filenamecheck2= FALSE;
             //$filenamecheck3= FALSE;
            }  
        else {
        $filenamecheck_message = FALSE;
        //$filenamecheck1= TRUE;
        //$filenamecheck2= TRUE;
        //$filenamecheck3= TRUE;         
             }
    ?>
    </li>
    
    <li>
    <input name="submit" type="submit">
    </li>



</ol>

</fieldset>

</form>	

</BODY>
</HTML>

 

I've ended up posting the more or less the exact same php blocks code in the process and form section.

 

 

Link to comment
Share on other sites

Oh God.  Ouch.  Yeah you can eliminate a whole lot of that by using some arrays and loops.  But anyways, since it looks like your form is in the same script, you would just do something like this for the error messages:

 

foreach ($error as $e) {
   echo "$e<br/>";
}

 

Now I do notice that you seem to have a block of html/php code for each individual error message display...and that's the trick.  Most of that code for each of those error message displays are the same exact thing. You're just using a different variable and label or whatever for each one.  You can use an array to store those individual pieces of info and just make a loop to spit out the same code over and over.  Find the pattern, make a loop.

 

The good news is that you're at least figuring out how to make it functional on your own.  That's the hardest part.  Well, one could argue that finding patterns and streamlining your code is where the artform is, but business people don't really care what's going on under the hood, as long as the car drives, and since they're the ones who sign the paycheck, that's all that really matters ;)

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.