PHP pre-defined error displaying

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Matt Morgan
    New Member
    • Oct 2011
    • 41

    PHP pre-defined error displaying

    I have created a form on my website but for some reason 3 sections of the form arenot submitting correctly. After clicking submit I recieve this on screen:
    'We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.

    The RAID you entered do not appear to be valid.
    The Accessories you entered do not appear to be valid.
    The Guarantee you entered do not appear to be valid.


    Please go back and fix these errors.'

    This is the PHP for the form:
    Code:
    if(isset($_POST['email'])) {
         
        $email_to = "matt.email@gmail.com";
         
        $email_subject = "Custom Form";
         
         
        function died($error) {
            echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }
        if(!isset($_POST['first_name']) ||
           !isset($_POST['last_name']) ||
           !isset($_POST['email']) ||
    	   !isset($_POST['role']) ||
    	   !isset($_POST['ram']) ||
    	   !isset($_POST['raid']) ||
    	   !isset($_POST['hdd1']) ||
    	   !isset($_POST['hdd2']) ||
    	   !isset($_POST['os']) ||
    	   !isset($_POST['gpu']) ||
    	   !isset($_POST['extras']) ||
    	   !isset($_POST['accessories']) ||
    	   !isset($_POST['guarantee']) ||
           !isset($_POST['customer']) ||
           !isset($_POST['budget'])) {    
        }
         
        $first_name = $_POST['first_name']; 
        $last_name = $_POST['last_name']; 
        $email_from = $_POST['email']; 
    	$Role = $_POST['role']; 
    	$Memory_Size = $_POST['ram'];
    	$RAID = $_POST['raid']; 
    	$Hard_Drive_1 = $_POST['hdd1']; 
    	$Hard_Drive_2 = $_POST['hdd2']; 
    	$Operating_System = $_POST['os'];
    	$Graphics_Card = $_POST['gpu']; 
    	$Optional_Extras = $_POST['extras']; 
    	$Accessory_Packs = $_POST['accessories'];
    	$Guarantee = $_POST['guarantee']; 
    	$Customer_Specifics = $_POST['customer']; 
    	$Maximum_Budget = $_POST['budget']; 
         
        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
      if(!preg_match($email_exp,$email_from)) {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
      }
        $string_exp = "/^[A-Za-z .'-]+$/";
      if(!preg_match($string_exp,$first_name)) {
        $error_message .= 'The First Name you entered does not appear to be valid.<br />';
      }
      if(!preg_match($string_exp,$last_name)) {
        $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
      }
      if(strlen($Role) < 2) {
        $error_message .= 'The Role you entered do not appear to be valid.<br />';
      }
      if(strlen($Memory_Size) < 2) {
        $error_message .= 'The RAM you entered do not appear to be valid.<br />';
      }
      if(strlen($RAID) < 2) {
        $error_message .= 'The RAID you entered do not appear to be valid.<br />';   
      }
      if(strlen($Hard_Drive_1) < 2) {
        $error_message .= 'The HHD1 you entered do not appear to be valid.<br />';
      }
      if(strlen($Hard_Drive_2) < 2) {
        $error_message .= 'The HDD2 you entered do not appear to be valid.<br />';
      }
      if(strlen($Operating_System) < 2) {
        $error_message .= 'The OS you entered do not appear to be valid.<br />';
      }
      if(strlen($Graphics_Card) < 2) {
        $error_message .= 'The GPU you entered do not appear to be valid.<br />';
      }
      if(strlen($Optional_Extras) < 2) {
        $error_message .= 'The Extras you entered do not appear to be valid.<br />';
      }
      if(strlen($Accessory_Packs) < 2) {
        $error_message .= 'The Accessories you entered do not appear to be valid.<br />';   
      }
      if(strlen($Guarantee) < 2) {
        $error_message .= 'The Guarantee you entered do not appear to be valid.<br />';
      }
      if(strlen($Customer_Specifics) < 2) {
        $error_message .= 'The Customer Specifics you entered do not appear to be valid.<br />';   
      }
      if(strlen($Maximum_Budget) < 2) {
        $error_message .= 'The Maximum Budget you entered do not appear to be valid.<br />';   
      }
      if(strlen($error_message) > 0) {
        died($error_message);
      }
        $email_message = "Form details below.\n\n";
         
        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }
         
        $email_message .= "First Name: ".clean_string($first_name)."\n";
        $email_message .= "Last Name: ".clean_string($last_name)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Role: ".clean_string($Role)."\n";
    	$email_message .= "Memory Size: ".clean_string($Memory_Size)."\n";
    	$email_message .= "RAID: ".clean_string($RAID)."\n";
    	$email_message .= "Hard Drive 1: ".clean_string($Hard_Drive_1)."\n";
    	$email_message .= "Hard Drive 2: ".clean_string($Hard_Drive_2)."\n";
    	$email_message .= "Operating System: ".clean_string($Operating_System)."\n";
    	$email_message .= "Graphics Card: ".clean_string($Graphics_Card)."\n";
    	$email_message .= "Optional Extras: ".clean_string($Optional_Extras)."\n";
    	$email_message .= "Accessory Packs: ".clean_string($Accessory_Packs)."\n";
    	$email_message .= "Guarantee: ".clean_string($Guarantee)."\n";
        $email_message .= "Customer Specifics: ".clean_string($Customer_Specifics)."\n";
    	$email_message .= "Maximum Budget: ".clean_string($Maximum_Budget)."\n";
         
    // create email headers
    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers); 
    ?>
     
         
    Thank you for requesting a quote.
    
    <?php
    }
    die();
    ?>
    
    <body>
    </body>
    </html>
    I was getting the error for every field entered but seems to just be the three now.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    can you print out the invalid values?

    Comment

    • Matt Morgan
      New Member
      • Oct 2011
      • 41

      #3
      This is the PHP sections for the RAID field, as well as the relevant HTML.
      Code:
          !isset($_POST['raid']) ||
           
          $RAID = $_POST['raid'];
           
          if(strlen($RAID) < 2) {
              $error_message .= 'The RAID you entered do not appear to be valid.<br />';   
            }
           
          $email_message3 .= "RAID: ".clean_string($RAID)."\n";
           
          <tr>
                          <td align="left" valign="top">
                            <label for="raid">RAID </label>                 </td>
                           <td align="left" valign="top">
                            <select name="raid">
                              <option value="raid"> </option>
                              <option value="Y">Yes*</option>
                              <option value="N">No</option>
                            </select>
                            <span class="style8">(*Requires 2 identical Hard Drives)</span></td>
                          </tr>
      The Accessories Field:
      Code:
          !isset($_POST['accessories']) ||
           
          $Accessory_Packs = $_POST['accessories'];
           
          if(strlen($Accessory_Packs) < 2) {
              $error_message .= 'The Accessories you entered do not appear to be valid.<br />';   
            }
           
          $email_message9 .= "Accessory Packs: ".clean_string($Accessory_Packs)."\n";
           
          <tr>
                          <td align="left" valign="top">
                            <label for="accessories">Accessory Packs [2]</label>                 </td>
                          <td align="left" valign="top">
                           <input type="checkbox" name="accessories" value="0" checked="checked"/> 
                            None <br />
                            <input type="checkbox" name="accessories" value="1" /> 
                            Pack 1  <br />
                            <input type="checkbox" name="accessories" value="2" /> 
                            Pack 2 <br />
                            <input type="checkbox" name="accessories" value="3" /> 
                            Pack 3 <br />
                            <input type="checkbox" name="accessories" value="4" /> 
                            Pack 4 <br />
                            <input type="checkbox" name="accessories" value="5" /> 
                            Pack 5 <br />
                            <input type="checkbox" name="accessories" value="6" /> 
                            Pack 6 <br />
                            <input type="checkbox" name="accessories" value="Other" /> 
                            Other (Please Specify) <br />                  </select>                  </td>
                          </tr>
      The Guarantee Field:
      Code:
          !isset($_POST['guarantee']) ||
           
          $Guarantee = $_POST['guarantee'];
           
          if(strlen($Guarantee) < 2) {
              $error_message .= 'The Guarantee you entered do not appear to be valid.<br />';
            }
           
          $email_message10 .= "Guarantee: ".clean_string($Guarantee)."\n";
           
          <tr>
                          <td align="left" valign="top">
                            <label for="guarantee">Guarantee </label>                 </td>
                           <td align="left" valign="top">
                            <select name="guarantee">
                          <option value="guarantee"> </option>
                          <option value="0">No Extension</option>
                          <option value="1">1 Year Extension</option>
                          <option value="2">2 Year Extension</option>
                          </select></td>
                          </tr>

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        in pretty much every case, you mark valid answers as invalid.
        Raid:
        - values (strlen): raid (4), Y (1), N (1)
        Accessories:
        - values (strlen): 0 (1), 1 (1), … , 6 (1), Other (5)
        Guarantee:
        - values (strlen): guarantee (9), 0 (1), 1 (1), 2 (1)

        Comment

        • Matt Morgan
          New Member
          • Oct 2011
          • 41

          #5
          So how do I get around this. I only understand basic PHP so I'm not very good at it.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            change the condition for a failure.

            example: you expect only integers to be given ($_POST['myint'])
            Code:
            $value = filter_input(INPUT_POST, "myint", FILTER_VALIDATE_INT);
            if (false === $value)
            {
                // myint is not an integer
            }

            Comment

            • Matt Morgan
              New Member
              • Oct 2011
              • 41

              #7
              Hi, I've attempted some changes to the values on the PHP however it appears to have made things worse!

              I now recieve these error messages:
              We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.

              The RAID you entered do not appear to be valid.
              The Accessories you entered do not appear to be valid.
              The Guarantee you entered do not appear to be valid.
              The Maximum Budget you entered do not appear to be valid.


              Please go back and fix these errors.


              This is the code for the error messages:
              Code:
                  $error_message = "";
                  $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
                if(!preg_match($email_exp,$email_from)) {
                  $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
                }
                  $string_exp = "/^[A-Za-z .'-]+$/";
                if(!preg_match($string_exp,$first_name)) {
                  $error_message .= 'The First Name you entered does not appear to be valid.<br />';
                }
                if(!preg_match($string_exp,$last_name)) {
                  $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
                }
                if(strlen($Role) < 2) {
                  $error_message .= 'The Role you entered do not appear to be valid.<br />';
                }
                if(strlen($Memory_Size) < 2) {
                  $error_message .= 'The RAM you entered do not appear to be valid.<br />';
                }
                if(strlen($RAID) < 2) {
                  $error_message .= 'The RAID you entered do not appear to be valid.<br />';   
                }
                if(strlen($Hard_Drive_1) < 2) {
                  $error_message .= 'The HHD1 you entered do not appear to be valid.<br />';
                }
                if(strlen($Hard_Drive_2) < 2) {
                  $error_message .= 'The HDD2 you entered do not appear to be valid.<br />';
                }
                if(strlen($Operating_System) < 2) {
                  $error_message .= 'The OS you entered do not appear to be valid.<br />';
                }
                if(strlen($Graphics_Card) < 2) {
                  $error_message .= 'The GPU you entered do not appear to be valid.<br />';
                }
                if(strlen($Optional_Extras) < 2) {
                  $error_message .= 'The Extras you entered do not appear to be valid.<br />';
                }
                if(strlen($Accessory_Packs) < 2) {
                  $error_message .= 'The Accessories you entered do not appear to be valid.<br />';   
                }
                if(strlen($Guarantee) < 2) {
                  $error_message .= 'The Guarantee you entered do not appear to be valid.<br />';
                }
                  $string_exp = "/^[A-Za-z .'-]+$/";
                if(!preg_match($string_exp,$Customer_Specifics)) {
                  $error_message .= 'The Customer Specifics you entered do not appear to be valid.<br />';   
                }
                  $string_exp = "/^[A-Za-z .'-]+$/";
                if(!preg_match($string_exp,$Maximum_Budget)) {
                  $error_message .= 'The Maximum Budget you entered do not appear to be valid.<br />';   
                }
                if(strlen($error_message) > 0) {
                  died($error_message);
                }
              I assume it's this ' if(strlen($Guar antee) < 2) {' that needs to be changed for the ones flagging as Errors. What should they be changed to?
              Last edited by Matt Morgan; Nov 5 '11, 07:37 PM. Reason: To make Mod happy.

              Comment

              • Stewart Ross
                Recognized Expert Moderator Specialist
                • Feb 2008
                • 2545

                #8
                This thread follows on from a previous thread: http://bytes.com/topic/php/answers/9...ror-unexpected

                -Stewart
                Last edited by Stewart Ross; Nov 6 '11, 07:40 AM. Reason: Noted that question is a follow-on from previous thread

                Comment

                • Matt Morgan
                  New Member
                  • Oct 2011
                  • 41

                  #9
                  Im assuming that it's this section ' if(strlen($Guar antee) < 2) {' that needs to be changed for the ones flagging as Errors (RAID, Accessories, Guarantee). What would you suggest setting them as?

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    What would you suggest setting them as?
                    what values do you expect (do you have given in the HTML)?

                    if you set numbers, check for a number (e.g. post #6), if you set strings, check if the received string matches one of the valid strings.

                    Comment

                    • Matt Morgan
                      New Member
                      • Oct 2011
                      • 41

                      #11
                      RAID HTML is
                      Code:
                      <tr>
                      				<td align="left" valign="top">
                        				<label for="raid">RAID </label> 				</td>
                       				<td align="left" valign="top">
                      			      <select name="raid">
                      			        <option value="raid" selected="selected"> </option>
                      			        <option value="Y">Yes*</option>
                      			        <option value="N">No</option>
                      		          </select>
                        				<span class="style8">(*Requires 2 identical Hard Drives)</span></td>
                      				</tr>
                      Accessories HTML is
                      Code:
                      <tr>
                      				<td align="left" valign="top">
                        				<label for="accessories">Accessory Packs [2]</label> 				</td>
                      				<td align="left" valign="top">
                      			     <input type="checkbox" name="accessories" value="0" checked="checked"/> 
                      			      None <br />
                      			      <input type="checkbox" name="accessories" value="1" /> 
                      			      Pack 1  <br />
                      			      <input type="checkbox" name="accessories" value="2" /> 
                      			      Pack 2 <br />
                      			      <input type="checkbox" name="accessories" value="3" /> 
                      			      Pack 3 <br />
                      				  <input type="checkbox" name="accessories" value="4" /> 
                      			      Pack 4 <br />
                      				  <input type="checkbox" name="accessories" value="5" /> 
                      			      Pack 5 <br />
                      				  <input type="checkbox" name="accessories" value="6" /> 
                      			      Pack 6 <br />
                      			      <input type="checkbox" name="accessories" value="Other" /> 
                      			      Other (Please Specify) <br />			      </select>			      </td>
                      				</tr>
                      Guarantee HTML is:
                      Code:
                      <tr>
                      				<td align="left" valign="top">
                        				<label for="guarantee">Guarantee </label> 				</td>
                       				<td align="left" valign="top">
                        				<select name="guarantee">
                      				<option value="guarantee" selected="selected"> </option>
                      				<option value="0">No Extension</option>
                      				<option value="1">1 Year Extension</option>
                      				<option value="2">2 Year Extension</option>
                      				</select></td>
                      				</tr>

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        I think (hope) with that you can answer the question what values you expect for each field.

                        Comment

                        • Matt Morgan
                          New Member
                          • Oct 2011
                          • 41

                          #13
                          Honestly have no idea. I've only recently started using PHP.

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            this has nothing to do with PHP. this is pure HTML (at this point):

                            what are the possible values for the guarantee field?

                            Comment

                            • Matt Morgan
                              New Member
                              • Oct 2011
                              • 41

                              #15
                              guarantee, 0, 1, 2. So 4 values?

                              Comment

                              Working...