SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Form Error Messages Aren't Working?

    Hi Everyone,

    If a user presses "submit" and does not fill in any information into the text fields they are suppose to get a error message stating "All fields Required" but if you press "submit" it says

    "Please enter a valid zip.
    Please enter a valid zip code
    Please enter a valid email."
    I'm not sure what I'm doing wrong here. Any ideas?

    http://whatsmyowncarworth.com/class-...hip-submit.php

    PHP Code:
    <?php
    include "connect_to_mysql3.php";

    //////////////// Below is the function
      
    function capitalize($element)
    {
      
    $element strtolower($element);
      return 
    ucwords($element);

    //////////////// Above is the function

    if (isset ( $_POST['dealership'] ) ){
            
       
    $dealership $_POST['dealership'];
       
    $address $_POST['address'];
       
    $state $_POST['state'];
       
    $city $_POST['city'];
       
    $zip $_POST['zip'];
       
    $phone $_POST['phone'];
       
    $website $_POST['website'];
       
    $email $_POST['email'];
       
    $name $_POST['name'];
       
       
    $dealership mysql_real_escape_string($dealership);
       
    $address mysql_real_escape_string($address);
       
    $state mysql_real_escape_string($state);
       
    $city mysql_real_escape_string($city);
       
    $zip mysql_real_escape_string($zip);  
       
    $phone mysql_real_escape_string($phone);
       
    $website mysql_real_escape_string($website);
       
    $email mysql_real_escape_string($email);
       
    $name mysql_real_escape_string($name);
       
       
    $dealership capitalize($dealership);
       
    $state capitalize($state);
       
    $city capitalize($city);
       
    $name capitalize($name); 
       
        if ( empty(
    $dealership) || empty($address) || empty($state) || empty($city) || empty($zip) || empty($phone) || empty($website) || empty($email) || empty($name) ){
            
             
    $errors[] = "All Fields Are Required! <br>";
            
            } else {
            
              if (
    strlen($dealership) > 65){
                
    $errors[] = 'Dealership name is too long. Shorten it'.'<br>'
              }
              
              if (
    strlen($state) > 65){
                
    $errors[] = 'State name too long. Shorten it'.'<br>';
              }
                
              if (!
    is_numeric($zip)){
                
    $errors[] = 'Please enter a valid zip.'.'<br>';
              } 
              
              if(
    strlen($zip) > || strlen($zip) == 6){
               
    // If string/pass length is greater then 6 or equal to 6 it will display password okay, 
               // You can do further code here, else, It will show short pass error.
               // echo"Its good mate! have atleast 5 digit pass ";
               // You can continue your login code here...
              
    }
              else{
                
    $errors[] = 'Please enter a valid zip code'.'<br>';
              }
              
              if (
    filter_var($emailFILTER_VALIDATE_EMAIL) === FALSE){
                
    $errors[] = 'Please enter a valid email.'.'<br>';
              }
              
            
            } 

       
            if(!empty(
    $errors)){
              foreach (
    $errors as $error){
                echo 
    "$error";
              }
            } 
            
            else {
              
              
                
    $insert = ("INSERT INTO dealers (name, state, city, age, email, password, time) 
                VALUES ('
    $name','$state','$city','$age','$email','$password', now() )");
              
                 
    mysql_query($insert);
              
               
    /* $to = $email;
                $from = "admin@whatsmyowncarworth.com";
                $subject = "Thanks for signing up!";
                
                $message = '<html>
                 <body bgcolor="#FFFFFF">
                        Hi ' . $name . ',
                        <br /><br />
                        Thanks for signing up!
                        <br /><br />
                         <a href="http://www.yahoo.com?city='. $city .'" target="_blank">Browse through cars in your city!</a> 
                        </a>
                        <br /><br /> 
                        Thanks! 
                 </body>
                 </html>';
                 
                $headers = "From: $from\r\n";
                $headers .= "Content-type: text/html\r\n";
                $to = "$to";
                mail($to, $subject, $message, $headers);
                
                echo 'Thanks for submitting your information.'; */
              
    }
       



    }



    ?>
    HTML Code:
    <form action="add-dealership-submit.php" method="POST">
            <table>
    		  <tr> 
                <td width="99">Dealership Name:</td>
                <td width="112"> 
                  <input type="text" name="dealership" value="<?php $dealership ;?> " />
                </td>
              </tr>
    		  <tr> 
                <td width="99">Address:</td>
                <td width="112"> 
                  <input type="text" name="address" value="<?php $address ;?> " />
                </td>
              </tr>
    		  <tr> 
                <td width="99">State:</td>
                <td width="112"> 
                  <input type="text" name="state" value="<?php $state ;?> " />
                </td>
              </tr>
    		  <tr> 
                <td width="99">City:</td>
                <td width="112"> 
                  <input type="text" name="city" value="<?php $city ;?> " />
                </td>
              </tr>
              <tr> 
                <td width="99">Zip:</td>
                <td width="112"> 
    			  <input type="text" name="zip" value="<?php $zip ;?> " />
                </td>
              </tr>
              <tr> 
                <td width="99">Phone:</td>
                <td width="112"> 
                  <input type="text" name="phone" value="<?php $phone ;?> " />
                </td>
              </tr>
              <tr> 
                <td width="99">Website:</td>
                <td width="112"> 
                  <input type="text" name="website" value="<?php $website ;?> " />
                </td>
              </tr>
    		   <tr> 
                <td width="99">Email:</td>
                <td width="112"> 
                  <input type="text" name="email" value="<?php $email ;?> " />
                </td>
              </tr> 
    		   <tr> 
                <td width="99">Your Name:</td>
                <td width="112"> 
                 <input type="text" name="name" value="<?php $name ;?> " />
                </td>
              </tr>
              <tr> 
                <td width="99"> 
                  <input type="submit" name="submit" value="Submit">
                </td>
              </tr>
            </table>
          </form>

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    I think you should use AND instead of OR to check the fact that all fields are empty.

  3. #3
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Atlanta, GA
    Posts
    244
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Try something like this:
    PHP Code:
     <?php
    include "connect_to_mysql3.php";

    //////////////// Below is the function
    function capitalize($element)
    {
        return 
    ucwords(strtolower($element));

    //////////////// Above is the function

    if (isset ( $_POST['dealership'] ) ){
        
    $errors = array();
        
    $formValues = array();
        
    $reqFields = array('dealership',
                           
    'address',
                           
    'state',
                           
    'city',
                           
    'zip',
                           
    'phone',
                           
    'website',
                           
    'email',
                           
    'name');
        
        foreach (
    $reqFields as $field => $val)
        {
            if ((!isset(
    $_POST[$field])) || ($_POST[$field] == '')) {
                
    $errors[] = "{$field} is required.<br>";
            } else {
                if ((
    $field == 'dealership') && (strlen($val 65))) {
                    
    $errors[] = 'Dealership name is too long. Shorten it.<br>';
                } else if ((
    $field == 'state') && (strlen($val 65))) {
                    
    $errors[] = 'State name is too long. Shorten it.<br>';
                } else if ((
    $field == 'zip') && ((!is_numeric($val)) || (strlen($val) > 5))) {
                    
    $errors[] = 'Please enter a valid zip.<br>';
                } else if ((
    $field == 'email') && (filter_var($valFILTER_VALIDATE_EMAIL) === FALSE)) {
                    
    $errors[] = 'Please enter a valid email.<br>';
                } else if ((
    $field == 'age') && (!is_numeric($val))) {
                    
    $errors[] = 'Please enter a numeric age.<br>';
                }
                
    $formValues[$field] = (ctype_alpha($val)) ? capitalize($val) : $val;
            }
            
        }
        
        
    $formValues['time'] = date('Y-m-d H:i:s');
        
        if(
    count($errors)){
            foreach (
    $errors as $error){
                echo 
    $error;
            }
        } else {
            
    $fields '(';
            
    $values '(';
            foreach (
    $formValues as $field => $val)
            {
                
    $fields .= "{$field}, ";
                
    $values .= mysql_real_escape_string($val) . ', ';
            }
            
    $fields rtrim($fields', ') . ')';
            
    $values rtrim($values', ') . ')';
            
            
    $sql "INSERT INTO dealers {$fields} VALUES {$values}";
            
    mysql_query($sql)
        }
    }
    ?>
    On a side note, I would suggest using MySQLi or PDO for your database functions. They both support prepared statements which make it much easier to protect against SQL injections.

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys for both of your replies. I got the code working but I encountered a new problem.

    Regarding, my zip code. I want the user to enter a zip code that is exactly 5 digits long. If they enter anything under or over 5 digits I want a error message to show.

    I'm about half way there because as of right now if a user types in a zip that is less than 5 digits, they get an error but if they type in a zip code that's over 5 digits then they aren't presented with an error.

    How would I account for a number that's greater than 5?

    Thanks again everyone for your responses and tips!

    PHP Code:
    if(strlen($zip) > || strlen($zip) == ){

    }
    else {
      
    $errors[] = 'Please enter a valid zip code'.'<br>';


  5. #5
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Atlanta, GA
    Posts
    244
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    if (strlen($zip) != 5)

  6. #6
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Atlanta, GA
    Posts
    244
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Keith, I appreciate that!

  8. #8
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Atlanta, GA
    Posts
    244
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Lol I totally messed up in the first post to this thread. In the first foreach I should have replaced most $val with $_POST[$field] ... it was early in the morning for me :/

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,222
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kduv View Post
    if (strlen($zip) != 5)
    Also, set the max length on your input field to 5. Why let the user enter more than you want (granted you still need the validation to ensure only 5 is entered, but it helps)?

  10. #10
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good tip cpradio. Thanks again Everyone!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •