Jump to content

Recommended Posts

I have a form that has: multiple text/integer fields; a dynamically populated drop down list; a file upload; a static check button group; and a dynamically populated check button group. What i cannot seem to do is to get the form to keep the values for each input type once the form has been submitted, I can get the text and integer fields to return their values however it is the other types that I cannot deal with.

 

Here is the code, it is structured in validation then main code, the form is created twice, once for on first page load and a different form is created after submit has been pressed. I will deal with the "upload section" after this has all been sorted. I also return values for all fields, but I also have a slight image dimension restriction problem which means it still allows any .jpeg of any size to be accepted.

 

<?php

include "connect_details.php";

$CBSizes = array("XS","Small", "Medium", "Large", "XL", "XXL", "3XL");
$Submit = $_POST['SubmitB'];

if($_POST['SubmitB'] =="Submit")
{
   $ClothingCode = $_POST['ClothingCode'];

   $Description = $_POST['Description'];

   $ItemType = $_POST['ItemType'];

   $Price1 = $_POST['Price1'];
  
   $Price2 = $_POST['Price2'];

   $NUSPrice1 = $_POST['NUSPrice1'];
  
   $NUSPrice2 = $_POST['NUSPrice2'];

   $ProductImage = $_FILES['ProductImage'];
   $Max_Size = 51200;
   $Img_Dimensions = getimagesize($_Files['ProductImage']['tmp_name']);
   $dirupload = "../images/products/";
   $NotExec = true;

   $AColours = $_POST['Colours'];

   $ASizes = $_POST['Sizes'];
   
   //ClothingCode Validation

   if(empty($ClothingCode))
   {
      ?>
      <div style="position:absolute; top:10px; left:200px; width:550px">   
        <span class="errmsg">Please Enter the Clothing Code</span>
      </div>
      <?php     
      $Valid_form = false;
      $Valid_ClothingCode = false;
   }


   //Description Validation

   if(empty($Description))
   {
      ?>
      <div style="position:absolute; top:25px; left:200px; width:550px">   
        <span class="errmsg">Please Enter the Description</span>
      </div>
      <?php     
      $Valid_form = false;
      $Valid_Description = false;
   }

   //Item Type Validation
   
   if(empty($ItemType))
   {
      ?>
      <div style="position:absolute; top:40px; left:200px; width:550px">   
        <span class="errmsg">Please Select an Item Type</span>
      </div>
      <?php
      $Valid_ItemType = false;
      $Valid_form = false;
   }



   //Price Validation

   if((empty($Price1)) || empty($Price2))
   {
      ?>
      <div style="position:absolute; top:55px; left:200px; width:550px">   
        <span class="errmsg">Please Enter the Price</span>
      </div>
      <?php     
      $Valid_form = false;
      $Valid_Price = false;
   }
   elseif((is_numeric($Price1) == false) || (is_numeric($Price2) == false))
   {
      ?>
      <div style="position:absolute; top:55px; left:200px; width:550px">   
        <span class="errmsg">That is not a valid Price!</span>
      </div>
      <?php     
      $Valid_form = false;
      $Valid_Price = false;
   }
   else
   {
      $Price = $Price1.".".$Price2;
      $Valid_form = true;
      $Valid_Price = true;   
   }

   //NUS Price Validation

   if((empty($NUSPrice1)) || empty($NUSPrice2))
   {
      ?>
      <div style="position:absolute; top:70px; left:200px; width:550px">   
        <span class="errmsg">Please Enter the NUS Price</span>
      </div>
      <?php     
      $Valid_form = false;
      $Valid_NUSPrice = false;
   }
   elseif((is_numeric($NUSPrice1) == false) || (is_numeric($NUSPrice2) == false))
   {
      ?>
      <div style="position:absolute; top:70px; left:200px; width:550px">   
        <span class="errmsg">That is not a valid NUS Price!</span>
      </div>
      <?php     
      $Valid_form = false;
      $Valid_NUSPrice = false;
   }
   else
   {
      $NUSPrice = $NUSPrice1.".".$NUSPrice2;
      $Valid_form = true;
      $Valid_NUSPrice = true;   
   }

   //Product Image Validation

   if (($_FILES['ProductImage']['type'] == "image/jpg") || ($_FILES['ProductImage']['type'] == "image/jpeg") || ($_FILES['ProductImage']['type'] == "image/jpe") && ($_FILES['ProductImage']['size'] < $Max_Size) && (($Img_Dimensions[0] <= 300) && ($Img_Dimensions[1] <=250)))
   {
     if (file_exists($dirupload . $_FILES['ProductImage']['name']."jpg"))
     {
        ?>
        <div style="position:absolute; top:85px; left:200px; width:550px">   
           <span class="errmsg">That file already exists!</span>
        </div>
        <?php
        $Valid_ProductImage = false;
        $Valid_form = false;
     }

   }
   else
   {
      $Valid_ColourImage = false;
      $valid_form = false;
      ?>
      <div style="position:absolute; top:85px; left:200px; width:550px">   
         <span class="errmsg">Please Select a .jpg image file that is no more than 300x250 pixels in size, and is less than 50kb</span>
      </div>
      <?php
   }

   //Available Sizes Section

   if(count($ASizes) > 0)
   {
      foreach($ASizes AS $Sizes)
      {
         $Valid_ASizes=True;
         $AvailableSizes .=$Sizes."?";
         echo"<br>";
      }
   }
   else 
   {
      $Valid_ASizes=False;
      ?>
      <div style="position:absolute; top:100px; left:200px; width:550px">   
         <span class="errmsg">Please select a size!</span>
      </div>
      <?php
   }


   //Available Colours Section

   if(count($AColours) > 0)
   {
      foreach($AColours AS $Colours)
      {
         $Valid_AColours=True;
         $AvailableColours .=$Colours."?";
         echo"<br>";
      }
   }
   else 
   {
      $Valid_AColours=False;
      $Valid_form = False;
      ?>
      <div style="position:absolute; top:115px; left:200px; width:550px">   
         <span class="errmsg">Please select a Colour!</span>
      </div>
      <?php
   }

   // Upload Section

   if(Valid_form==true)
   {

   }
}

?>
<html>
<head>
  <title>Clothing Line</title>
  <link href="stylesheetCL.css" rel="stylesheet">
  <?php require('jscript.inc') ?>
</head>


</head>

<body>

  <?php require('menu.inc') ?>


<div style="position:absolute; top:75px; left:200px; width:550px">
<form method="post" enctype="multipart/form-data" action='add_product_form.php'/>

<?php
if($_POST['SubmitB'] != "Submit")
{
   ?>   
    <br>
    Clothing Code: 
    <br>
    <input type="text" MAXLENGTH="6" name="ClothingCode"/>
    <br>
    <br>
    Description: 
    <br>
    <input type="text" MAXLENGTH="30" name="Description"/>
    <br>
    <br>
    
    Item Type: 
    <br>
    <select name="ItemType"/>
    <option value="">Please Select an Item Type</option>
    <?php
   
     //Establish a connection to the Database
     $conn = mysql_connect($Host,$Username,$Password) or die(mysql_error());

     //Select the MySQL database

     $db = mysql_select_db($Dbname, $conn);

     $sql = "SELECT ItemType FROM ItemType ORDER BY ItemType";
  
     $rs = mysql_query($sql, $conn) or die(mysql_error());
   
     while($row = mysql_fetch_array($rs))
     {
        echo"<option value='{$row['ItemType']}'>{$row['ItemType']}</option>";
     }
     mysql_close($conn);
    
     ?>
    </select>

    <br>
    <br>
    Price: 
    <br>
    <input type="int" size="1" MAXLENGTH="3" name="Price1"/>.<input type="int" MAXLENGTH="2" size="1" name="Price2"/>
    <br>
    <br>
    NUS Price: 
    <br>
    <input type="int" size="1" MAXLENGTH="3" name="NUSPrice1"/>.<input type="int" MAXLENGTH="2" size="1" name="NUSPrice2"/>
    <br>
    <br>
    Product Image: (Must be .jpg image type, 300x250 pixels in dimensions and less than 50Kb in size) 
    <br>
    <input type="file" name="ProductImage">
    <br>
    <br>
    Available Sizes:
    <br>
     <table id="Sizes">
        <tr>

        <?php

        $X=0;
        
        while($X!=7)
        {
           $Size = $CBSizes[$X];
           ?>
   <td><?php echo $Size?>:</td>
   <td><input type="checkbox" name="Sizes[]" value="<?php echo $Size?>"></td>
           </tr>
           <tr>
           <?php
           $X++;
        }
        ?>
     	</tr>
     </table>
    <br>
    Available Colours:

     <table><tr>

      <?php
      //Establish a connection to the Database

      $conn = mysql_connect($Host,$Username,$Password);

      $db = mysql_select_db($Dbname, $conn);

      $sql = "SELECT AvailableColours FROM Colours ORDER BY AvailableColours";
   
      $rs = mysql_query($sql, $conn) or die(mysql_error());

      $NumberOfColours = mysql_num_rows($rs);

      $counter = 0;

      if ($result || (mysql_num_rows($rs) > 0))
      {
         while ($row = mysql_fetch_assoc($rs)) 
         {    
            if($counter%9==0&&$counter!=0) echo "</tr><tr>";
            echo "<td><img src='/images/colours/{$row['AvailableColours']}.gif' title='{$row['AvailableColours']}'/><br />";
            echo "<td><input type='checkbox' name='Colours[]' value='{$row['AvailableColours']}'></td>";
            echo "</td>";
            ++$counter;
         }
      }
      echo "</tr></table>";
     
     mysql_close($conn); 
     ?>

   <br> 
   <br>

<?php
}
elseif($_POST['SubmitB'] == "Submit")
{
    ?>
    <br>
    Clothing Code: 
    <br>
    <input type="text" MAXLENGTH="6" name="ClothingCode" value ="<?php echo $_POST['ClothingCode']; ?>"/>
    <br>
    <br>
    Description: 
    <br>
    <input type="text" MAXLENGTH="30" name="Description" value ="<?php echo $_POST['Description']?>"/>
    <br>
    <br>

    Item Type: 
    <br>
    <select name="ItemType" selected value="<?php echo $_POST['ItemType']?>"/>
    <option value="">Please Select an Item Type</option>    
    <?php

     //Establish a connection to the Database
     $conn = mysql_connect($Host,$Username,$Password) or die(mysql_error());

     //Select the MySQL database

     $db = mysql_select_db($Dbname, $conn);

     $sql = "SELECT ItemType FROM ItemType ORDER BY ItemType";
  
     $rs = mysql_query($sql, $conn) or die(mysql_error());
   
     while($row = mysql_fetch_array($rs))
     {
        echo"<option value='{$row['ItemType']}'>{$row['ItemType']}</option>";
     }
     mysql_close($conn);
     ?>

    </select>

    <br>
    <br>
    Price: 
    <br>
    <input type="int" size="1" MAXLENGTH="3" name="Price1" value="<?php echo $_POST['Price1']?>"/>.<input type="int" MAXLENGTH="2" size="1" name="Price2" value="<?php echo $_POST['Price2']?>"/>
    <br>
    <br>
    NUS Price: 
    <br>
    <input type="int" size="1" MAXLENGTH="3" name="NUSPrice1" value="<?php echo $_POST['NUSPrice1']?>"/>.<input type="int" MAXLENGTH="2" size="1" name="NUSPrice2" value="<?php echo $_POST['NUSPrice2']?>"/>
    <br>
    <br>
    Product Image: (Must be .jpg image type, 300x250 pixels in dimensions and less than 50Kb in size) 
    <br>
    
    <input type="file" name="ProductImage" value="<?php echo $_FILES['ProductImage']['name']?>"/>
    <br>
    <br>
    Available Sizes:
    <br>
     <table id="Sizes">
        <tr>
        <?php
        $X=0;
        
        while($X!=7)
        {
           $Size = $CBSizes[$X];
           ?>
   <td><?php echo $Size?>:</td>
   <td><input type="checkbox" name="Sizes[]" value="<?php echo $Size?>"></td>
           </tr>
           <tr>
           <?php
           $X++;
        }
        ?>
     	</tr>
     </table>
    <br>
    Available Colours:

     <table><tr>

      <?php
      //Establish a connection to the Database

      $conn = mysql_connect($Host,$Username,$Password);

      $db = mysql_select_db($Dbname, $conn);

      $sql = "SELECT AvailableColours FROM Colours ORDER BY AvailableColours";
   
      $rs = mysql_query($sql, $conn) or die(mysql_error());

      $NumberOfColours = mysql_num_rows($rs);

      $counter = 0;

      if ($result || (mysql_num_rows($rs) > 0))
      {
         while ($row = mysql_fetch_assoc($rs)) 
         {    
            if($counter%9==0&&$counter!=0) echo "</tr><tr>";
            echo "<td><img src='/images/colours/{$row['AvailableColours']}.gif' title='{$row['AvailableColours']}'/><br />";
            echo "<td><input type='checkbox' name='Colours[]' value='{$row['AvailableColours']}'></td>";
            echo "</td>";
            ++$counter;
         }
      }
      echo "</tr></table>";
     
     mysql_close($conn); 
     ?>

   <br> 
   <br>
   <?php
}
?>
<input type="submit" name="SubmitB" value="Submit"/>
</form>
</div>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/86390-form-return-values-problem/
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.