SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with PHP contact form

    Hi guys,

    I've made a PHP contact form for my site and need help with a couple of things...

    The form action links an external PHP script (scripts/contact-form-script.php) but is there a way I can have it so the PHP script for the form is contained within the same PHP file as my contact form (contact.php)?

    I tried just putting the form code at the top of contact.php but the browser automatically reads the anti-spam re-direct, so maybe that needs revising too?

    The second thing is, how can I make the Name, Email and Message fields mandatory? So if a user tries to submit the form and hasn't filled in one of the required fields and clicks submit, contact.php reloads with a message at the top of the form saying something like 'Complete the required fields' and highlights the relevant field with a red border?

    Here's the code for contact.php:

    Code HTML4Strict:
    <form action="http://www.mydomain.com/scripts/contact-form-script.php" method="post" name="contact" id="contact">
    <p><strong>Name:*</strong><br />
    <input name="name" type="text" class="ctextField" /></p>
    <p><strong>E-mail:*</strong><br />
    <input name="email" type="text" class="ctextField" /></p>
    <p><strong>Telephone:</strong><br />
    <input name="telephone" type="text" class="ctextField" /></p>
    <p><strong>Company:</strong><br />
    <input name="company" type="text" class="ctextField" /></p>
    <p><strong>Address:</strong><br />
    <input name="address1" type="text" class="ctextField" /></p>
    <p><input name="address2" type="text" class="ctextField" /></p>
    <p><strong>Town:</strong><br />
    <input name="town" type="text" class="ctextField" /></p>
    <p><strong>County:</strong><br />
    <input name="county" type="text" class="ctextField" /></p>
    p><strong>Postcode:</strong><br />
    <input name="postcode" type="text" class="ctextField" /></p>
    <p><strong>Message:*</strong><br />
    <textarea name="message" cols="55" rows="8" class="ctextField"></textarea></p>
    <p><input name="submit" value="SEND MESSAGE" class="submitButton" type="submit" /><div style="visibility:hidden; width:1px; height:1px"><input name="url" type="text" size="45" id="url" /></div></p>
    </form>



    And this is the PHP I'm using to submit the form data for contact-form-script.php:

    Code PHP:
    <?php
     
     
     
    $headers .= "Reply-To: " . $_POST["email"] . "\r\n";
     
    $to = "me@mydomain.com";
    $subject = "Contact from website";
     
    $message = $headers;
    $message .= "Name: " . $_POST["name"] . "\r\n";
    $message .= "E-mail: " . $_POST["email"] . "\r\n";
     
     
     
     
            $message= '
     
     
                <table cellspacing="0" cellpadding="8" border="0" width="500">
                <tr>
                    <td colspan="2"></td>
                </tr>
                <tr bgcolor="#eeeeee">
                  <td width="154" style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Name</strong></td>
                  <td width="314" style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$name.'</td>
                </tr>
                <tr bgcolor="#eeeeee">
                  <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>E-mail address:</strong></td>
                  <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$email.'</td>
                </tr>
                <tr bgcolor="#eeeeee">
                  <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Telephone number:</strong></td>
                  <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$telephone.'</td>
                </tr>
                <tr bgcolor="#eeeeee">
                  <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Company:</strong></td>
                  <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$company.'</td>
                </tr>
                <tr bgcolor="#eeeeee">
                  <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Address</strong></td>
                  <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$address1.'</td>
                </tr>
                <tr bgcolor="#eeeeee">
                  <td></td>
                  <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$address2.'</td>
                </tr>
                <tr bgcolor="#eeeeee">
                  <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Town</strong></td>
                  <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$town.'</td>
                </tr>
                <tr bgcolor="#eeeeee">
                  <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>County</strong></td>
                  <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$county.'</td>
                </tr>
                <tr bgcolor="#eeeeee">
                  <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Postcode</strong></td>
                  <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$postcode.'</td>
                </tr>
     
                <tr bgcolor="#eeeeee">
                    <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Message</strong></td>
                </tr>               
                <tr bgcolor="#eeeeee">
                    <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$message.'</td>
                </tr>               
     
                <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
             </table>
     
     
     
     
    ';
     
     
     
     
     
     
     
    $url = stripslashes($_POST["url"]);
    if (!empty($url)) {
    header( 'Location: [url]http://www.go-away-spam-robots.com[/url]' );
    exit();
    } 
     
    mail($to, $subject, $message, $headers);
     
    header( 'Location: [url]http://www.mydomain.com/sent.php[/url]' ) ;
    ?>


    Any help on this would be greatly appreciated.

    Thank you and I hope to hear from you!

    SM

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Essentially:

    PHP Code:
    <?php

    if( isset($_POST['formsent']) ){

    var_dump$_POST );

    if( empty(
    $_POST[''name]) ) $error['name'] = true ;
    // do your other stuff here

    }
    ?>

    <form action=""  method="POST" name="contact" id="contact">
    <p><strong>Name:*</strong><br />
    <input name="name" type="text" class="ctextField" /> <?php if ($error['name']) echo " *** Please add your name"?></p>
    <input type = hidden name="formsent" value=1>
    <input type="submit">
    </form>
    When you do this for your entire form though, you might think of better ways of doing it and making it less error prone (eg using arrays).

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Cups,

    That's awesome, thank you for that!

    I added the PHP at the top of my form and Dreamweaver seemed to display a syntax error for:

    Code PHP:
    if( empty($_POST[''name]) ) $error['name'] = true ;

    I tried a couple of things, and changed it to...

    Code PHP:
    if( empty($_POST['name']) ) $error['name'] = true ;

    ...which seemed to be ok.

    But when you hit Submit, the form still goes through and just leaves the field blank on the email I get sent.

    This is what my PHP looks like now:

    Code PHP:
     
    <?php
     
    if( isset($_POST['formsent']) ){
     
    var_dump( $_POST );
     
    if( empty($_POST['name']) ) $error['name'] = true ;
    if( empty($_POST['email']) ) $error['email'] = true ;
    if( empty($_POST['message']) ) $error['message'] = true ;
    // do your other stuff here
     
    }
     
     
     
    if (array_key_exists('submit', $_POST)) {
     
        $name = $_POST['name'];
     
        $email = $_POST['email'];
     
        $telephone = $_POST['telephone'];
     
        $company = $_POST['company'];
     
        $address1 = $_POST['address1'];
     
        $address2 = $_POST['address2'];
     
        $town = $_POST['town'];
     
        $county = $_POST['county'];
     
        $postcode = $_POST['postcode'];
     
        $formMessage = $_POST['message'];
     
     
     
     
     
    $headers .= "Reply-To: " . $_POST["email"] . "\r\n";
     
     
     
    $to = "me@mydomain.com";
     
    $subject = "Contact from website";
     
     
     
    $message = $headers;
     
    $message .= "Name: " . $_POST["name"] . "\r\n";
     
    $message .= "E-mail: " . $_POST["email"] . "\r\n";
     
     
     
    $headers  = "MIME-Version: 1.0\r\n";
     
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
     
     
     
     
     
     
     
     
     
            $message= "
     
    <table cellspacing='0' cellpadding='8' border='0' width='500'>
     
                <tr>
     
                    <td colspan='2'></td>
     
                </tr>
     
                <tr bgcolor='#eeeeee'>
     
                  <td width='154' style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Name</strong></td>
     
                  <td width='314' style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$name."</td>
     
                </tr>
     
                <tr bgcolor='#eeeeee'>
     
                  <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>E-mail address:</strong></td>
     
                  <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$email."</td>
     
                </tr>
     
                <tr bgcolor='#eeeeee'>
     
                  <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Telephone number:</strong></td>
     
                  <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$telephone."</td>
     
                </tr>
     
                <tr bgcolor='#eeeeee'>
     
                  <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Company:</strong></td>
     
                  <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$company."</td>
     
                </tr>
     
                <tr bgcolor='#eeeeee'>
     
                  <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Address</strong></td>
     
                  <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$address1."</td>
     
                </tr>
     
                <tr bgcolor='#eeeeee'>
     
                  <td></td>
     
                  <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$address2."</td>
     
                </tr>
     
                <tr bgcolor='#eeeeee'>
     
                  <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Town</strong></td>
     
                  <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$town."</td>
     
                </tr>
     
                <tr bgcolor='#eeeeee'>
     
                  <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>County</strong></td>
     
                  <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$county."</td>
     
                </tr>
     
                <tr bgcolor='#eeeeee'>
     
                  <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Postcode</strong></td>
     
                  <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$postcode."</td>
     
                </tr>
     
     
     
                <tr bgcolor='#eeeeee'>
     
                    <td colspan='2' style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Message</strong></td>
     
                </tr>              
     
                <tr bgcolor='#eeeeee'>
     
                    <td colspan='2' style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$formMessage."</td>
     
                </tr>              
     
     
     
                <tr><td colspan='2' style='padding: 0px;'><img src='images/whitespace.gif' alt='' width='100%' height='1' /></td></tr>
     
             </table>
     
     
     
    ";
     
     
     
    $url = stripslashes($_POST["url"]);
     
    if (!empty($url)) {
     
    header( 'Location: [url]http://www.go-away-spam-robots.com[/url]' );
     
    exit();
     
    }
     
     
     
    mail($to, $subject, $message, $headers);
     
     
     
    header( 'Location: [url]http://www.mydomain.com/sent.php[/url]' ) ;
     
    }
     
     
     
    ?>

    Do you have any ideas what may be causing the problem?

    Thanks again for your help

    SM

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    As I wasn't able to previously test the code I posted and it contained errors, here is a better version (tested this time).

    This is just to show how to push a message back to your in-completed form, making better use of PHP arrays - I am not dealing with any problems your email script has thrown up.

    PHP Code:
    <?php

    $error 
    = array();

    $basic_form_elements = array(
    'name'
    'address_line_1'
    'address_line_2'
    'tel'
    'mob'
    'fax'
    'inside_leg'
    );

    if( isset(
    $_POST['formsent']) ){

        if( empty(
    $_POST['name']) ) {

        
    $error['name'] = " *** Please add your name" ;

        }else{

        
    // do your other stuff here, send mail etc
        
    var_dump$_POST );
        exit();

        }
    }
    ?>

    <form action=""  method="POST" name="contact" id="contact">


    <?php foreach($basic_form_elements as $el) : ?>

        <p><strong><?php echo ucwords(str_replace("_"" "$el)) ?></strong><br />
        <input name="<?php echo $el ?>" type="text" 
            class="ctextField" value="<?php if(isset($_POST[$el])) echo htmlentities($_POST[$el])?>"/> 
        <?php echo (array_key_exists($el ,$error)) ?  $error[$el] : "" ;?></p>

    <?php endforeach;?>


    <input type = "hidden" name="formsent" value="1">
    <input type="submit">
    </form>
    You can then go on and have the basic_form_elements array contain the error message then you could simplify the error checking so that if count($errors) is > 0 then re-display the form. I hope I am not making things too convoluted for you, but arrays are a cornerstone of programming - and you'd do well to spend a good time investigating how best to use them.


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
  •