Slipszenko Web Development
PHP With Forms
Category: PHP. Date: 2007-12-09. Comments: 3.

This tutorial should give you a good knowledge of using PHP to process forms for sending email, uploading files or processing user information, it is aimed at beginners looking to get better or an intermediate looking to remind him/herself.

Just going to do a simple introduction for the ABSOLOUTE beginners if you want to skip it, click here. One of the most useful things with php is how easy it is to handle variables and process them. Here is a really simple example of a variable being passed on:

Php Code Box
<?php
if(!isset($_POST['name']))
    {
    
?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="nameform">
        Name: <input type="text" name="name" id="name" /><br />
        <input type="submit" name="submit" id="submit" />
    </form>
    <?php
    
}
else
    {
    
$name $_POST['name'];
    echo 
"Hello: ".$name;
    }
?>

This simple bit of code first uses the if statment to check if the variable has been filled in (and if we therefore know there name). The code will allow us to display "Hello" followed by there name, for a bit of personalisation.

Let's face it though, this is quite useless and theres not really going to a site when your going to want to do this. So lets try something a bit more useful: Sending Email's via forms.

Here's the code, I'll explain it after:

Php Code Box
<?php
if(!isset($_POST['submit']))
    {
    
?>
    <form action="contact.php" class="niceform" method="post">
        <div class="label"><label for="yourname">Your Name:</label></div> <input type="text" name="yourname" id="yourname" /><br class="clear" />
        <div class="label"><label for="youremail">Your Email:</label></div> <input type="text" name="youremail" id="youremail" /><br class="clear" />
        <div class="label"><label for="subject">Subject:</label></div> <input type="text" name="subject" id="subject" /><br class="clear" />
        <div class="label"><label for="message">Message:</label></div> <textarea name="message" id="message"></textarea><br class="clear" />
        <div class="submit"><input class="sub" type="submit" name="submit" id="submit" value="Submit" /></div><br class="clear" />
    </form>
    *All fields are required.
    <?php
    
}
else
    {
    
$name $_POST['yourname'];
    
$subject $_POST['subject'];
    
$message $_POST['message'];
    
$email $_POST['youremail'];
    
$from "From: Contact Form";
    
    if(!empty(
$name) || !empty($subject) || !empty($message) || !empty($email))
        {
        
mail("youremail@yoursite.com"$subject"From: ".$email."/n".$message$from); //Change email address on this line to your own email address!
        
?>
        Thank you for the email, I'll try to reply as soon as I can. Return to the <a href="/">home page</a>?
        <?php
        
}
    else
        {
        
?>
        You appear to have left a field blank, please go <a href="contact.php">back</a> and try again.
        <?php
        
}
    }
?>

This is the code for a simple contact form. Copy this code to a file called contact.php, and upload it or save it to your testing server.

First we check to see if the form has been submited or not by checking the variable automatically set when you submit the contact form, if it hasn't been set then we display the contact form (if you want the styles, steal them from my stylesheet, :P). If they have sumbited the form then we process the information they've passed to the server.

First we put all of the $_POST varibales into shorthand variables. This makes it easier to refer to them later in the page. Then we use another if statement to check if any of the variables are empty, if they are then we tell the user that they've missed something out and give them a link to re-try using the contact form. If they filled everything in then it send the email to the seleceted email address and gives them a message informing them of this.

I'm going to leave it there for now, check back tomorrow and I'll have the rest online! Including how to upload files, just gotta get back to A-Level studys now.

Comments

Comments are temporarily unavailable as I'm working on various different updates, will be back very soon!


2007 - 2008 © Slipszenko Web Development