Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Php contact form help

  1. #1
    Join Date
    Sep 2013
    Location
    Antalya - Turkey
    Posts
    37
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Php contact form help

    Hallo everybody;

    I have this php contach form.I want to change warning item.
    For example: If somebody want to send empty email,they see warnings (for examplelease enter your name,please enter your email adress etc.)

    In this form,i think something wrong.If i want to change warning text for $name,automaticly change $email at the same time too.

    Please help me to resolve this problem.

    Thank you so much in advance...


    Code:
    <?php
    if(!session_id()) {
    	session_start();
    }
    error_reporting(0);
    
    if (isset($_REQUEST['action'])) {
        if ($_REQUEST['action'] == "contact_form_request") {
    
            $ourMail = "v.koretskiy@gmail.com";
    
            $required_fields = array("name", "email", "message");
            $pre_messagebody_info = "";
            $errors = array();
            $data = array();
            parse_str($_REQUEST['values'], $data);
    		
            //check for required and assemble message
    
            if (!empty($data)) {
                foreach ($data as $key => $value) {
                    $name = strtolower(trim($key));
                    if (in_array($name, $required_fields)) {
                        if (empty($value)) {
                            $errors[$name] = "Please enter your " . $name . "!";
                        }
                    }
    
                    if ($name == "email") {
                        if (!check_email_address($value)) {
                            $errors[$name] = "You've entered invalid email address!";
                        }
                    }
                }
            }
    
    //***	
         
            $verify = $_SESSION['verify'];
            if ($verify != md5($data['verify'])) {
                $errors["verify"] = "The verification code you've entered is wrong!";
            }
    
    //***
            $result = array(
                "is_errors" => 0,
                "info" => ""
            );
            if (!empty($errors)) {
                $result['is_errors'] = 1;
                $result['info'] = $errors;
                echo json_encode($result);
                exit;
            }
    		
    		$pre_messagebody_info.="<strong>Name</strong>" . ": " . $data['name'] . "<br />";
            $pre_messagebody_info.="<strong>E-mail</strong>" . ": " . $data['email'] . "<br />";
    
            $headers = 'MIME-Version: 1.0' . "\r\n";
            $headers.= 'Content-type: text/html; charset=UTF-8' . "\r\n";
            $headers.= "From: ".$data['email']."\r\n";
    
            $after_message = "\r\n<br />--------------------------------------------------------------------------------------------------\r\n<br /> This mail was sent via contact form";
    
            if (mail($ourMail, "Email from contact form", $pre_messagebody_info .="<strong>Message</strong>" . ": " . $data['message'] .$after_message, $headers)) {
                $result["info"] = "success";
            } else {
                $result["info"] = "server_fail";
            }
    
            echo json_encode($result);
            exit;
        }
    }
    
    function check_email_address($email) {
    // First, we check that there's one @ symbol,
    // and that the lengths are right.
        if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    // Email invalid because wrong number of characters
    // in one section or wrong number of @ symbols.
            return false;
        }
    // Split it into sections to make life easier
        $email_array = explode("@", $email);
        $local_array = explode(".", $email_array[0]);
        for ($i = 0; $i < sizeof($local_array); $i++) {
            if
            (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
    вЄ'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
                return false;
            }
        }
    // Check if domain is IP. If not,
    // it should be valid domain name
        if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
            $domain_array = explode(".", $email_array[1]);
            if (sizeof($domain_array) < 2) {
                return false; // Not enough parts to domain
            }
            for ($i = 0; $i < sizeof($domain_array); $i++) {
                if
                (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
    вЄ([A-Za-z0-9]+))$", $domain_array[$i])) {
                    return false;
                }
            }
        }
        return true;
    }
    ?>

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by volkantr19 View Post
    In this form,i think something wrong.If i want to change warning text for $name,automaticly change $email at the same time too...
    PHP Code:
    // snip

            
    $required_fields = array("name""email""message");
            
    $pre_messagebody_info "";
            
    $errors = array();
            
    $data = array();
            
    parse_str($_REQUEST['values'], $data);
            
            
    //check for required and assemble message

            
    if (!empty($data)) {
                foreach (
    $data as $key => $value) {
                    
    $name strtolower(trim($key));
                    if (
    in_array($name$required_fields)) {
                        if (empty(
    $value)) {
                            
    $errors[$name] = "Please enter your " $name "!";
                        }
                    }

                    if (
    $name == "email") {
                        if (!
    check_email_address($value)) {
                            
    $errors[$name] = "You've entered invalid email address!";
                        }
                    }
                }
            } 
    I'm not quite sure what you mean by this. Can you explain further? Are you trying to give each field its own error message?

    Note that $name is not the "name" field, it's the name of each field that you're looping through. The way you have it set up right now, all fields will get the empty($value) check, and the "email" field will also get the check_email_address check.

    ~~~~~~~~~~
    As a side note, that's one of the more involved email checks I've seen. I am not sure why the characters в†Є are in there…? There are two other things you might want to consider:

    1) The ereg functions have been deprecated for quite some time; relying on them is highly discouraged.
    2) There is a much simpler and more complete way to validate email addresses:
    PHP Code:
    <?php
    filter_var
    $value,FILTER_VALIDATE_EMAIL );
    The only thing you might want to add is to check that the email address has a TLD (i.e., that it's an address routable over http and not a local email address):
    PHP Code:
    preg_match"#\.[^@]*$#",$value 

  3. #3
    Join Date
    Sep 2013
    Location
    Antalya - Turkey
    Posts
    37
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    Yes,you understood my problem.
    I am trying to give each field its own error message.

    Ok,i send all files to SERVER and you can see what i mean.

  4. #4
    Join Date
    Sep 2013
    Location
    Antalya - Turkey
    Posts
    37
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    Here you can see what i mean.

    http://ornek5.antalyabalikcilik.com.tr/iletisim.html

    If you click SEND button you see this errors:
    Please enter your name!
    You've entered invalid email adress!
    Please enter your message!
    The verification code you've entered is wrong!



    Code:
    //check for required and assemble message
    
            if (!empty($data)) {
                foreach ($data as $key => $value) {
                    $name = strtolower(trim($key));
                    if (in_array($name, $required_fields)) {
                        if (empty($value)) {
                            $errors[$name] = "Please enter your " . $name . "!";
                        }
                    }
    
                    if ($name == "email") {
                        if (!check_email_address($value)) {
                            $errors[$name] = "You've entered invalid email address!";
                        }
                    }
                }
            }

    ~~~~~~~~~AFTER I CHANGE ERROR MESSAGES~~~~~~~~~~~


    And i change the error message for name filed.
    You can see the result here.
    If you click SEND button you see this errors:
    How are you name!
    You've entered invalid email adress!
    How are you message!
    The verification code you've entered is wrong!


    http://ornek7.antalyabalikcilik.com.tr/iletisim.html


    Code:
    //check for required and assemble message
    
            if (!empty($data)) {
                foreach ($data as $key => $value) {
                    $name = strtolower(trim($key));
                    if (in_array($name, $required_fields)) {
                        if (empty($value)) {
                            $errors[$name] = "How are you " . $name . "!";
                        }
                    }
    
                    if ($name == "email") {
                        if (!check_email_address($value)) {
                            $errors[$name] = "You've entered invalid email address!";
                        }
                    }
                }
            }
    ~~~~~~~~~~~~~~

    The problem is:
    If i want to change error message for ''name field'' ; message field become ''same error message'' automaticly.
    Last edited by volkantr19; 01-08-2014 at 06:39 PM.

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by volkantr19 View Post
    Here you can see what i mean …
    ~~~~~~~~~AFTER I CHANGE ERROR MESSAGES~~~~~~~~~~~

    And i change the error message for name filed.
    You can see the result here.
    If you click SEND button you see this errors:
    How are you name!
    You've entered invalid email adress!
    How are you message!
    The verification code you've entered is wrong!
    Quote Originally Posted by traq View Post
    Note that $name is not the "name" field, it's the name of each field that you're looping through.
    You did not "change the error message for [the] name field"; you changed the error message for all (empty) fields.

    To work with the "name" field specifically, you need to check what $name is. For example:
    PHP Code:
    if( $name === "name" && empty( $value )){
        
    $errors[$name] = "How are you $name!";

    Also, "How are you name!" seems like an unusual error message…?

  6. #6
    Join Date
    Sep 2013
    Location
    Antalya - Turkey
    Posts
    37
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by traq View Post
    Also, "How are you name!" seems like an unusual error message…?
    Of course it's unusual error message.It was just an example to show you how is going.

  7. #7
    Join Date
    Sep 2013
    Location
    Antalya - Turkey
    Posts
    37
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    I have a hard question to you.
    In this php form there is no error message field for ''messege''.


    error message for name:
    Code:
            if (!empty($data)) {
                foreach ($data as $key => $value) {
                    $name = strtolower(trim($key));
                    if (in_array($name, $required_fields)) {
                        if (empty($value)) {
                            $errors[$name] = "Please enter your " . $name . "!";
                        }
                    }


    error message for email:

    Code:
    if ($name == "email") {
                        if (!check_email_address($value)) {
                            $errors[$name] = "You've entered invalid email address!";
                        }
                    }
                }
            }


    error message for verification code:

    Code:
      $verify = $_SESSION['verify'];
            if ($verify != md5($data['verify'])) {
                $errors["verify"] = "The verification code you've entered is wrong!";
            }

    where is error message for ''message field'' ?
    Last edited by volkantr19; 01-08-2014 at 07:52 PM.

  8. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by volkantr19 View Post
    Of course it's unusual error message.It was just an example to show you how is going.
    Okay; I figured it might be something like that.

    Quote Originally Posted by volkantr19 View Post
    I have a hard question to you.
    In this php form there is no error message field for ''messege''.

    error message for name:
    Code:
                foreach ($data as $key => $value) {
                    $name = strtolower(trim($key));
                    if (in_array($name, $required_fields)) {
                        if (empty($value)) {
                            $errors[$name] = "Please enter your " . $name . "!";
                        }
                    }
                }
    No. Please re-read me responses above. This code applies to all form fields, in turn. (read about foreach loops.)

    You have shown no code that applies only, specifically to the "name" field. To do so, see the code example I gave in my post ( #5 ) above.

  9. #9
    Join Date
    Sep 2013
    Location
    Antalya - Turkey
    Posts
    37
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    I read all of them but i couldn't fix up.

    My last question.

    Is it possible to give each field its own error message seperatly?

    For example:
    Name: ''Please write your name!''
    Email: ''Please write your email!''
    Message: ''Please enter your message!''
    Verify: ''Please write true verify number!''

  10. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by volkantr19 View Post
    Is it possible to give each field its own error message seperatly?
    Of course. That's the example code I pointed out in my prior post.

    PHP Code:
    // inside your foreach loop

        // to check a specific field, check for the field name inside your loop.
        // this, for example, checks the "name" field specifically to see if its value is empty.
        
    if( $name === "name" && empty( $value )){
            
    $errors[$name] = "Please write your name!";
        }

        
    // do the same for "email", "verify", etc. 

Similar Threads

  1. Contact form
    By jan24 in forum PHP
    Replies: 7
    Last Post: 10-11-2010, 06:48 PM
  2. Contact form
    By andyg41 in forum PHP
    Replies: 3
    Last Post: 08-02-2009, 08:47 PM
  3. Help with contact form please
    By cbleep in forum HTML
    Replies: 6
    Last Post: 09-14-2007, 07:12 PM
  4. Need a contact form....
    By k3v1n in forum Looking for such a script or service
    Replies: 10
    Last Post: 05-13-2007, 01:58 AM
  5. Contact Form
    By Johnnymushio in forum HTML
    Replies: 2
    Last Post: 08-25-2006, 05:20 PM

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
  •