SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with php contact form

    Hello,

    I am trying to work out how to make the following changs to the form as l don't have much knowledge of PHP.

    1. Validate each field

    2. Display thank you page.

    3. Display error page if all fields are not complete.


    coding:

    <?php
    $to="jjr@club-amigos.co.uk ";
    if (!isset($_POST["send"])){
    // no post data -> display form
    ?>
    <form method="POST" action="<?=$_SERVER['PHP_SELF'];?>">
    To: jjr@club-amigos.co.uk

    From: <input type="text" name="sender">

    Subject : <input type="text" name="subject">

    Message :

    <textarea name="message" rows="10" cols="60" lines="20"></textarea>

    <input type="submit" name="send" value="Send">

    </form>
    <?
    }else{
    // found post data .. deal with it
    $from=$_POST['sender'];
    // send mail :
    if (mail($to,$_POST['subject'],$_POST['message'],"From: $from\n")){
    // display confirmation message if mail sent successfully
    echo "Your mail was indeed sent to $to.
    ";
    }else{
    // sending failed, display error message
    echo "Doh! Your mail could not be sent.
    ";
    }
    }
    ?>

  2. #2
    SitePoint Enthusiast xsreality's Avatar
    Join Date
    Feb 2006
    Location
    India
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    club,

    There is a way to do all that in a single PHP file which will display messages according to the situations.

    To show html part you can use the PHP HereDoc syntax.
    e.g.
    PHP Code:
    $html = <<< TEXT
    --------
    all html form structure goes here.
    u can use variables directly.
    TEXT; 
    The TEXT is not a keyword and any word can be used.

    For validation, u can check the length of each field using strlen() function.
    If an integer value is expected then
    PHP Code:
    if( $var strval(intval($var)) )
        
    // everything ok
    else
        
    $mess "$var is not valid."
    such validations help. Actually many things can be done. Its up to you.

    The thank you page is actually the $mess in the above code.
    Show the form only if something is wrong else dont show it.

    Same is the case with error pages too. Show the form with the $mess variable containing the exact error. You can use javascript for validation too but PHP is must.

    Check out this code:
    PHP Code:
    <?php

    $thisfile 
    $_SERVER['PHP_SELF'];
    $mess '';
    $name '';

    if( isset(
    $_POST['submit']) && $_POST['submit'] == 'Submit' )
    {
        if( !isset(
    $_POST['name']) || strlen($_POST['name']) > 30 )
            
    $mess "Invalid name entered.";
        
    // Validate other fields the same way.
        
    else
        {
            
    // everything ok.
            
    $mess "Thank You for sending your name.";    
            
    $noform 1;    // dont show form
        
    }
        
    $name $_POST['name'];
    }

    if( !isset(
    $noform) || !$noform )
    {
    $mess .= <<< TEXT
    <p>Enter your name</p>
    <form method="post" action="
    $thisfile">
    <input type="text" size=25 name="name" value="
    $name">
    <br><br>
    <input type="submit" name="submit" value="Submit">
    </form>
    TEXT;
    }
    ?>

    <html>
    <body>
    <?php echo $mess?>
    </body>
    </html>
    Hope it helps.

  3. #3
    Wadge! F4nat1c's Avatar
    Join Date
    Oct 2005
    Location
    South Wales, UK
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    reality, your code looks good. I've found an error:

    PHP Code:
    if( !isset($noform) || !$noform 
    needs to be:

    PHP Code:
    if( !isset($noform) || $noform == 
    An alternative way to do this would be to make a "forma.html" file with your contact form in. If there was error with all the validation, then use:

    PHP Code:
    require("/forma.html"); 
    OMFG SitePoint ROXORZ TEH BIG ONE111!
    Wish you were invisible?

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The validation appears to work but when l complete all fields the error still shows up.
    Any ideas as to why this is?
    What changes to the code do l have to make in order to rectify this issue?
    Could you make the highlighted changes for me.
    I am sorry but l am not particularly knowledgable when it comes to php.

  5. #5
    SitePoint Enthusiast xsreality's Avatar
    Join Date
    Feb 2006
    Location
    India
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    F4,

    AFAIK, the following code
    PHP Code:
    if( !$noform 
    is same as
    PHP Code:
    if( $noform == 
    and
    PHP Code:
    if( $noform 
    is same as
    PHP Code:
    if( $noform != 
    It is the way C/C++ goes, i believe.

  6. #6
    SitePoint Enthusiast xsreality's Avatar
    Join Date
    Feb 2006
    Location
    India
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    club,

    I dont exactly get your problem. Is the error message coming even if the data is valid?
    Be sure to use multiple if's if in case of multiple fields in the code that i gave above separated by else.
    You can show the exact code you are using. Only then will i be able to help you.

    xs

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    I have decied to find another script which validates better.
    Having found one l need to make the following amendments to it:

    1. To remove the text:

    $success_sent_msg='<p align="center"><strong>&nbsp;</strong></p>
    <p align="center"><strong>Your message has been successfully sent to us<br>
    </strong> and we will reply as soon as possible.</p>
    <p align="center">A copy of your query has been sent to you.</p>
    <p align="center">Thank you for contacting us.</p>';

    and instead call up the file: 'thankyou.htm'.


    2. Remove from the coding so that an email is not sent to the sender.
    This is what is sent to the sender:

    =============================================
    Thank you for your email.

    We will endeavour to reply to you shortly.

    Please DO NOT reply to this email.

    Below is a copy of the message you submitted:
    --------------------------------------------------
    Subject: $thesubject
    Query:
    $themessage
    --------------------------------------------------

    ==================================

    If you could make these alterations l would appreciate if very very much, as l don't know much about php.




    This is the coding:


    <?php
    /* PHP Form Mailer - phpFormMailer v2.1, last updated 30th Nov 2005 - check back often for updates!
    (easy to use and more secure than many cgi form mailers) FREE from:
    www.TheDemoSite.co.uk
    Should work fine on most Unix/Linux platforms */

    // ------- three variables you MUST change below -------------------------------------------------------
    $valid_ref1="http://www.club-amigos.co.uk/contact_test.html";// chamge "Your--domain" to your domain
    $valid_ref2="http://www.club-amigos.co.uk/contact_test.html";// chamge "Your--domain" to your domain
    $replyemail="jjr@club-amigos.com";//change to your email address
    // ------------------------------------------------------------

    //clean input in case of header injection attempts!
    function clean_input_4email($value, $check_all_patterns = true)
    {
    $patterns[0] = '/content-type:/';
    $patterns[1] = '/to:/';
    $patterns[2] = '/cc:/';
    $patterns[3] = '/bcc:/';
    if ($check_all_patterns)
    {
    $patterns[4] = '/\r/';
    $patterns[5] = '/\n/';
    $patterns[6] = '/%0a/';
    $patterns[7] = '/%0d/';
    }
    //NOTE: can use str_ireplace as this is case insensitive but only available on PHP version 5.0.
    return preg_replace($patterns, "", strtolower($value));
    }

    $name = clean_input_4email($_POST["name"]);
    $email = clean_input_4email($_POST["email"]);
    $thesubject = clean_input_4email($_POST["thesubject"]);
    $themessage = clean_input_4email($_POST["themessage"], false);

    $error_msg='ERROR - not sent. Try again.';

    $success_sent_msg='<p align="center"><strong>&nbsp;</strong></p>
    <p align="center"><strong>Your message has been successfully sent to us<br>
    </strong> and we will reply as soon as possible.</p>
    <p align="center">A copy of your query has been sent to you.</p>
    <p align="center">Thank you for contacting us.</p>';

    $replymessage = "Hi $name

    Thank you for your email.

    We will endeavour to reply to you shortly.

    Please DO NOT reply to this email.

    Below is a copy of the message you submitted:
    --------------------------------------------------
    Subject: $thesubject
    Query:
    $themessage
    --------------------------------------------------

    Thank you";

    // email variable not set - load $valid_ref1 page
    if (!isset($_POST['email']))
    {
    echo "<script language=\"JavaScript\"><!--\n ";
    echo "top.location.href = \"$valid_ref1\"; \n// --></script>";
    exit;
    }

    $ref_page=$_SERVER["HTTP_REFERER"];
    $valid_referrer=0;
    if($ref_page==$valid_ref1) $valid_referrer=1;
    elseif($ref_page==$valid_ref2) $valid_referrer=1;
    if(!$valid_referrer)
    {
    echo "<script language=\"JavaScript\"><!--\n alert(\"$error_msg\");\n";
    echo "top.location.href = \"$valid_ref1\"; \n// --></script>";
    exit;
    }
    $themessage = "name: $name \nQuery: $themessage";
    mail("$replyemail",
    "$thesubject",
    "$themessage",
    "From: $email\nReply-To: $email");
    mail("$email",
    "Receipt: $thesubject",
    "$replymessage",
    "From: $replyemail\nReply-To: $replyemail");
    echo $success_sent_msg;
    /*
    PHP Form Mailer - phpFormMailer (easy to use and more secure than many cgi form mailers)
    FREE from:

    www.TheDemoSite.co.uk */
    ?>


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
  •