How do I make required fields in PHP form

Discussion in 'PHP' started by peppy, Jun 17, 2007.

Thread Status:
Not open for further replies.
  1. #1
    I have the following code below but I can't get it to work right. I've been trying to make the Name, TelNo, Email fields to be required and lets the submitter know when they haven't entered them. I have the following code below, I was wondering if Krakjoe could help finish this code. When I submit the form, there is a blank white screen. Here is the code:

    Thanks,
    Peppy


    PHP code (form.php):

    <?php
    
    define("EMAIL_TO",            'krakjoe@krakjoe.info');
    define("EMAIL_SUBJECT",     'New POC from %s' );
    define("EMAIL_FROM",                    'krakjoe@krakjoe.info' );
    define("EMAIL_MAX_ATTACHMENT_SIZE",  '300000' );
    function error( $msg )
    {
        return printf("<center><font color=red>%s</font><center><br />", $error );    
    }
    function Attach( $message, $headers = null )
    {
        // Build your message from your posted form
        $message = nl2br( sprintf(
            "Name : %s\n".
            "Telephone : %s\n".
            "Email : %s\n".
            "%s",
            $_POST['Name'],
            $_POST['TelNo'],
            $_POST['Email'],
            $message
        ) );
        // start to add to headers
        $headers .= sprintf( "From: %s\n", EMAIL_FROM );
        // check to see if we are attaching
        if( is_uploaded_file( $_FILES['attach']['tmp_name'] ) )
        {
            // the content needs a boundary so that email clients know where to get data
            $boundary = sprintf( '==Multipart_Boundary_x%s', md5( time( ) ) ) ;
            // continue to build headers, adding the boundary for attached data
            $headers .= sprintf( 
                                "MIME-Version: 1.0\n".
                                "Content-Type: multipart/mixed;\n boundary=\"%s\"\n",
                                $boundary );
            // edit message to include data and boundaries
            $message = sprintf(
                                "This is a multi-part message in MIME format.\n\n".
                                "--%s\n".
                                "Content-Type: text/html; charset=\"iso-8859-1\"\n".
                                "Content-Transfer-Encoding: 7bit\n\n".
                                "%s\n\n".
                                "--%s\n".
                                "Content-Type: %s;\n name=\"%s\"\n".
                                "Content-Disposition: attachment;\n filename=\"%s\"\n".
                                "Content-Transfer-Encoding: base64\n\n".
                                "%s\n\n".
                                "--%s-\n",
                                $boundary,
                                $message,
                                $boundary,
                                $_FILES['attach']['type'],
                                $_FILES['attach']['name'],
                                $_FILES['attach']['name'],
                                chunk_split( base64_encode( file_get_contents( $_FILES['attach']['tmp_name'] ) ) ),
                                $boundary            
            );  
        }
        // send the original message with the file attached
        return mail( EMAIL_TO, sprintf( EMAIL_SUBJECT, $_SERVER['REMOTE_ADDR'] ), $message, $headers );
    }
    if( $_POST )
    {
        if( trim( $_POST['Name'] ) == "" )
        {
            error( 'Please enter a name' );            
        }
        elseif( trim( $_POST['TelNo']) == "" or ( (int) $_POST['TelNo'] != $_POST['TelNo'] ) )
        {
            error( 'Please enter a valid phone number, containing only numeric characters' );    
        }
        elseif( trim( $_POST['Email'] ) == "" )
        {
            error( 'Please enter an email address' );    
        }
        elseif( trim( $_POST['MsgBody'] ) == "" )
        {
            error( 'Please enter a message body' );
        }
        elseif( Attach( $_POST['message'] ) )
        {
            printf( "<center><font color=blue>Your message was sent to %s</font></center>", EMAIL_TO );
        }   
        else
        {
            error( 'Failed to deliver your message' );
        }
    }
    ?>
    Code (markup):


    HTML code:

    <html>
    <head>
        <title></title>
    </head>
    <body>
        <form action="form.php" method="post" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="<?=EMAIL_MAX_ATTACHMENT_SIZE ?>" />
            <table width="600" border="0" cellspacing="0" cellpadding="2" align="center">
             <tr>
                <td align="left"><span>Name:</span></td>
                <td align="left"><input type="text" name="Name"></td>
             </tr>
             <tr>
                <td align="left"><span>Telephone Number:</span></td>
                <td align="left"><input type="text" name="TelNo"></td>
             </tr>
            <tr>
                <td align="left"><span>Email:</span></td>
                <td align="left"><input type="text" name="Email"></td>
            </tr>
             <tr>
                <td align="left" valign="top"><span>Message:</span></td>
                <td align="left"><textarea name="MsgBody" cols="40" rows="5"></textarea></td>
             </tr>
             <tr>
                <td align="left" valign="top">Attach Image :</td>
                <td><input type="file" name="attach" /></td>
             </tr>
             <tr>
                <td align="left"><input type="submit" value="Send"></td>
             </tr>
          </table>
        </form>
    </body>
    </html>
    Code (markup):
     
    peppy, Jun 17, 2007 IP
  2. ProgrammersTalk

    ProgrammersTalk Peon

    Messages:
    684
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    ProgrammersTalk, Jun 17, 2007 IP
  3. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #3
    if(empty($req_field)){
        //Action if empty
    }
    PHP:
     
    dp-user-1, Jun 17, 2007 IP
  4. peppy

    peppy Active Member

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    95
    #4
    Ninja Noodles, can you place this in my code and post up the entire php code? I'm not quite sure where to put this statement and how to put it together. I would really appreciate that.

    Thanks,
    Peppy
     
    peppy, Jun 17, 2007 IP
  5. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #5
    NinjaNoodles, he already has the validation code.

    The problem is the "return printf" combination.

    In the error() function, take out the return statement

    EDIT: There is another problem in that same line... it uses $error instead of $msg.
    This is what the error() function should look like now:
    function error( $msg )
    {
        printf("<center><font color=red>%s</font><center><br />", $msg);    
    }
    PHP:
     
    krt, Jun 17, 2007 IP
  6. peppy

    peppy Active Member

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    95
    #6
    Awsome krt! you're are a lifesaver! Thanks for fixing this up for me and thanks to everyone else who have helped me, this form is great.

    Cheers
    Peppy
     
    peppy, Jun 17, 2007 IP
  7. TatiAnA

    TatiAnA Active Member

    Messages:
    1,103
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    78
    #7
    Is there any way to make this stay on the same page incase a required field is not filled out? I mean it will not redirect to another page
     
    TatiAnA, Jun 19, 2007 IP
  8. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #8
    Ah, sorry, didn't read the post, just the title. :p
     
    dp-user-1, Jun 19, 2007 IP
  9. samusexu

    samusexu Well-Known Member

    Messages:
    138
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #9
    Yeah you cold use javascript validation but more securely you could use ajax to submit the values and get response from the server. In this way the user would not leave the page.:)
     
    samusexu, Jun 20, 2007 IP
  10. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #10
    Why bother? JS does the job unless someone deliberately tampers with something or if JS is disabled (in which case AJAX doesn't apply either). I guess AJAX has its place when it comes to checking duplicate emails/usernames and such though.
     
    krt, Jun 20, 2007 IP
  11. kaisellgren

    kaisellgren Well-Known Member

    Messages:
    472
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    110
    #11
    $username = $_POST['username']
    if (strlen($username) < 3)
     die('Your username is too short!');
    PHP:
    You may also set maximum length too :p
     
    kaisellgren, Jun 20, 2007 IP
Thread Status:
Not open for further replies.