Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 14th, 2006, 05:58 AM
Registered User
 
Join Date: Apr 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to plexus
Default Form mail validation

Hi all,

I've coded the following form mail code that will simply send an email upon filling up information but I need some enhancements to it which I'm not sure how to go about coding it.

Code:
<?php

    if ($_SERVER['REQUEST_METHOD'] != 'POST'){
        $me = $_SERVER['PHP_SELF'];

?>

<form id="form" name="form" method="post" action="contact.php">
  <p>Name:
    <input name="fname" type="text" id="fname" value="" />
</p>
  <p>Subject:
    <input name="fsubject" type="text" id="fsubject" />
  </p>
  <p>Email Address: 
    <input name="femail" type="text" id="femail" />
  </p>
  <p>
    <input name="fcc" type="checkbox" id="fcc" />
    CC Yourself 
  </p>
  <p>URL: 
    <input name="furl" type="text" id="furl" value="http://" />
</p>
  <p>Comments:<br />
    <textarea name="fcomments" cols="50" rows="10" id="fcomments"></textarea>
  </p>
  <p>
    <input type="submit" name="submit" value="Submit" />
  </p>
</form>

<?php
    } 
    else {
        $uself = 1;
        $headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;

        // Retriving textfields values
        $senderName = stripslashes($_POST['fname']);
        $senderSubject = stripslashes($_POST['fsubject']);
        $senderEmail = stripslashes($_POST['femail']);
        $senderURL = stripslashes($_POST['furl']);
        $senderComment = stripslashes($_POST['fcomments']);

        // Domestics
        $to = "[email protected]";
        $subject = "[Website] " . $senderSubject;
        $senderADDR = stripslashes($_SERVER['REMOTE_ADDR']);
        $senderHOST = stripslashes($_SERVER['HTTP_USER_AGENT']);

        // headers
        $header = "MIME-Version: 1.0\r\n";
        $header .= "Content-type: text/plain; charset=US-ASCII\r\n";
        $header .= "Content-Transfer-Encoding: 7bit\r\n";
        $header .= "From: \"$senderName\" <$senderEmail>" . $headersep . "\r\n";

        // check if user wants a copy of the email
        if (isset($_POST['fcc']) == "true")
            $header .= "Cc: " . $senderEmail . "\r\n";

        // Specify default timezone
        date_default_timezone_set("Asia/Singapore");

        // Constructing email message
        $message = "From........: " . $senderName; "\n";
        $message.= "\nSite........: " . $senderURL; "\n";
        $message.= "\nAddr........: " . $senderADDR; "\n";
        $message.= "\nHost........: " . $senderHOST;
        $message.= "\n\n";
        $message.= "********************************************************\n";
        $message.= $subject;
        $message.= "\n";
        $message.= "********************************************************\n\n";
        $message.= $senderComment;
        $message.= "\n";

        if (mail($to, $subject, $message, $header))
            echo "Message successfully sent!";
        else
            echo "Error, message not sent!";
    }
?>
My questions:

1. How can I add validations to the input such that it will check and ensure that the required fields have been filled up upon submission of the form?

2. Assuming the required fields were not filled up, how can I display each individual error messages (example: "Name is required", "Email address is incorrect" etc.) on the same page and above the form itself

3. If all the fields have been filled up correctly and the form have been submitted, how can I display a note (example: "Thanks, you submission have been received") on the same page but this time, without showing the form itself.

Thanks!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Standalone validation + web form validation morbo Struts 0 August 19th, 2008 04:02 AM
Form Validation Help jonsey Javascript 0 April 25th, 2007 08:14 AM
Validation of Form qazi_nomi Javascript 5 August 2nd, 2004 03:49 PM
Form not refreshing after form validation Mimi Javascript How-To 0 August 25th, 2003 03:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.