Results 1 to 15 of 15
  1. #1

    Required fields PHP

    I have a simple mail script written in php. I am not sure how to make it so there are "required" fields. As it is now, anyone can submit a "blank" request, and I want to ensure that there atleast a couple of fields that are required.

    If anyone can lend a quick hand on this I would greatly appreciate it.

    I can show you the script if you need it.

  2. #2
    Join Date
    May 2004
    Posts
    500
    Can you show the script please =)

  3. #3
    Originally posted by mouldy_punk
    Can you show the script please =)
    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

    <title>Redirect Page</title>

    <SCRIPT LANGUAGE="JavaScript">

    <!-- Begin

    redirTime = "3000";

    redirURL = "http://www.removed.rem";

    function redirTimer() { self.setTimeout("self.location.href = redirURL;",redirTime); }

    // End -->

    </script>

    </head>

    <body onLoad="redirTimer()" bgcolor="#ffffff">

    <?

    $f_name=$_POST['f_name'];

    $l_name=$_POST['l_name'];

    $address=$_POST['address'];

    $city=$_POST['city'];

    $province=$_POST['province'];

    $business=$_POST['business_name'];

    $phone=$_POST['phone'];

    $email=$_POST['email'];

    $comments=$_POST['comments'];



    $to="[email protected]_obvious_reasons.com";

    $message="Message goes here.\n\n $f_name $l_name\n $address\n $city $province\n $phone\n\n Business/Department:\n $business_name\n\n Comments:\n $comments\n\nTheir e-mail address is: $email";

    if(mail($to,"Seminar Request",$message,"From: $email\n")) {

    echo "Thank for your request. You will be contacted for confirmation.";

    } else {

    echo "There was a problem sending the mail. Please check that you filled in the form correctly.";

    }

    ?>

    </html>

  4. #4
    Join Date
    May 2005
    Posts
    41
    in the script that handles the form you'll need something like:
    Code:
    if ($requiredfield){
           $handleform //  mail it or whatever you want to do with it
       }else{ $errormessage
        // you can also send it back to the form page using header(Location: $location) with an error message
    }
    That's just a simple mock thing you can set it up differently as you like, but that's the basics
    Last edited by snokrash257; 06-01-2005 at 12:23 PM.
    < !-- Place sig here //-- >

  5. #5
    Join Date
    May 2004
    Posts
    500
    What fields do you want to be required?

  6. #6
    Originally posted by mouldy_punk
    What fields do you want to be required?
    First Name:

    Last Name:

    Phone Number:

    Business/Department:

    Email:

    Thanks for your VERY prompt replies!!

  7. #7
    Join Date
    May 2004
    Posts
    500
    PHP Code:
    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

    <title>Redirect Page</title>

    <SCRIPT LANGUAGE="JavaScript">

    <!-- Begin

    redirTime = "3000";

    redirURL = "http://www.removed.rem";

    function redirTimer() { self.setTimeout("self.location.href = redirURL;",redirTime); }

    // End -->

    </script>

    </head>

    <body onLoad="redirTimer()" bgcolor="#ffffff">

    <?

    $f_name
    =$_POST['f_name'];

    $l_name=$_POST['l_name'];

    $address=$_POST['address'];

    $city=$_POST['city'];

    $province=$_POST['province'];

    $business=$_POST['business_name'];

    $phone=$_POST['phone'];

    $email=$_POST['email'];

    $comments=$_POST['comments'];


    if(
    $f_name == ''){
        die(
    "Forename was not entered");
    }
    if(
    $l_name == ''){
        die(
    "Lastname was not entered");
    }
    if(
    $phone == ''){
        die(
    "Phone number was not entered");
    }
    if(
    $business == ''){
        die(
    "Business/Department was not entered");
    }
    if(
    $email == ''){
        die(
    "Email was not entered");
    }

    $to="[email protected]_obvious_reasons.com";

    $message="Message goes here.\n\n $f_name $l_name\n $address\n $city $province\n $phone\n\n Business/Department:\n $business_name\n\n Comments:\n $comments\n\nTheir e-mail address is: $email";

    if(
    mail($to,"Seminar Request",$message,"From: $email\n")) {

    echo 
    "Thank for your request. You will be contacted for confirmation.";

    } else {

    echo 
    "There was a problem sending the mail. Please check that you filled in the form correctly.";

    }

    ?>

    </html>
    It's very basic. Basically, if those fields are empty, it'll just end the script and throw a suitable error messege at the user :p

  8. #8
    Thanks! Didn't realise it was so simple.

    You have been a great help.

  9. #9
    Join Date
    May 2004
    Posts
    500
    No prob, glad I could help =)

  10. #10
    Join Date
    Feb 2005
    Location
    Seattle, Washington
    Posts
    144
    even better, use the isset() function instead of == ''
    Regards,
    Matthew Fonda
    PHP Developer

  11. #11
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,342
    It is always unprofessional and truly annoying to use the die function, better yet, give the user a nice error message and allow them to edit the fields (fields should keep the invalid values)..

    I have wrote a contact form, of which you might want to take a look at it and see how I did it:

    http://free-php-scripts.net/download_script.php?id=4

    Peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  12. #12
    Join Date
    May 2004
    Posts
    500
    Originally posted by mfonda
    even better, use the isset() function instead of == ''
    Not if the PHP version is less than 3 :p Which I doubt, because then the mail() function wouldn't work...

    It is always unprofessional and truly annoying to use the die function, better yet, give the user a nice error message and allow them to edit the fields (fields should keep the invalid values)..
    Yeah, if it were my own site I'd probably use javascript just to alert the user, rather than sending it off through PHP. Actually, thinking about it, I'd probably use PHP aswell, incase the user has javascript disabled.

  13. #13
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    A better approach to the above is :

    PHP Code:
    <?php

        $required 
    = array('f_name','l_name','phone','business','email');
        
    $errors = array();
        foreach(
    $_POST as $key => $value)
        {
             if (
    in_array($key,$required) && strlen(trim($value)) == 0)
             {
                   
    $errors[] = "You must enter ".ucfirst(str_replace('_',' ',$key));
             }
        }

        if (
    sizeof($errors) > 0)
        {
              echo 
    'The following errors were detected in your submission : <br />';
              echo 
    implode('<br />',$errors);
        } else {
           / * 
    Insert Email Code */
        }
    ?>
    In the above code -- if your requirements change for the mandatory fields, you can just adjust the array -- and you don't need to keep adding/removing if()/echo statements.

    In addition, the check is a bit more robust than == ''.

    Always glad to help
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  14. #14
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,342

    Thumbs up

    Originally posted by fyrestrtr
    A better approach to the above is :

    PHP Code:
    <?php

        $required 
    = array('f_name','l_name','phone','business','email');
        
    $errors = array();
        foreach(
    $_POST as $key => $value)
        {
             if (
    in_array($key,$required) && strlen(trim($value)) == 0)
             {
                   
    $errors[] = "You must enter ".ucfirst(str_replace('_',' ',$key));
             }
        }

        if (
    sizeof($errors) > 0)
        {
              echo 
    'The following errors were detected in your submission : <br />';
              echo 
    implode('<br />',$errors);
        } else {
           / * 
    Insert Email Code */
        }
    ?>
    In the above code -- if your requirements change for the mandatory fields, you can just adjust the array -- and you don't need to keep adding/removing if()/echo statements.

    In addition, the check is a bit more robust than == ''.

    Always glad to help
    Very slick... and nice...

    will try it out soon..

    Peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  15. #15
    Join Date
    Jun 2004
    Posts
    109
    It would also seem like a good idea to first strip_tags() and then trim() unless you want to retain any tags and line breaks, etc.



    Here's an example of what I might use:
    PHP Code:
    <?php
    function myPrefix_makesafe($data$htmlRemove true$htmlEncode true$formatting false)
    {
        if(
    true === (bool) $htmlRemove)
        {
            
    /* If user wants to remove html tags... */
            
    $data strip_tags($data);
        }
        else
        {
            
    /* User doesn't want html tags removed, so */
            
    if(true === (bool) $htmlEncode)
            {
                
    /* If user wants to convert special html characters to their entities... */
                
    $data htmlspecialchars($data);
            }
        }

        if(
    false === (bool) $formatting)
        {
            
    /* User doesn't want to preserve formatting, therefore... */
            
    $data trim($data);
        }
        else
        {
            
    /* User wants to preserve formatting. So, convert  *
             *  new line characters (\r or \n) to html line breaks */
            
    $data nl2br($data);
        }
        return 
    $data;
    }
    ?>

    Usage:
    PHP Code:
    <?php
        $userName 
    myPrefix_makesafe($_POST['name']);
        
    $comments myPrefix_makesafe($_POST['comment'], truefalsetrue);
    ?>
    This is your function:
    myPrefix_makesafe($data, $htmlRemove = true, $htmlEncode = true, $formatting = false)

    $data is the actual data.
    $htmlRemove, if set to true, will remove all html tags.
    $htmlEncode, if set to true, will convert special html chars to their entities...of course you must realize that you cant have $htmlRemove set to true and have this set to true as well.
    $formatting, if set to true will preserve formatting such as new line characters, etc.

Posting Permissions

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