Archive for the ‘Learn PHP’ Category

So now we can take information from the various lesson posts and tie it all together to do something wortwhile! I've explained how to Retrieve Form Post variables and Send Emails, so how about a comment form that gathers a person's details and emails them to you?

Okay first you need the form:

<form method="post" action="form-mail.php">
<label for="name">Name:</label> <input type="text" name="name" id="name" size="20" />
<label for="email">Email:</label> <input type="text" name="email" id="email" size="20" />
<label for="comments">Comments:</label> <textarea name="comments" id="comments" cols="30" rows="5"></textarea>
<input type="submit" name="submit" id="submit" value="Submit Form" />
</form>

So above we have a simple form (which you would use CSS to style how it is displayed). When the form is submitted it will post the values to the form-mail.php page. This page would contain PHP script at the top to retrieve the form post data, email it to you and then just display a simple thank you page. The PHP script required is:

<?php
$name = $_POST['name'];
$from_email = $_POST['email'];
$comments = $_POST['comments'];
$mailbody = "From: ".$name."\n";
$mailbody .= "Email: ".$from_email."\n";
$mailbody .= "Comments: ".$comments;
mail("toemail@domain.com", "Form Results from Web site", $mailbody, "From: website@domain.com");
?>

Here the code retrieves the information from the _POST array. It then creates a string variable called $mailbody and stored the information into it. Similar to using the print method, but instead of outputting the information to the screen we just save it to a variable instead. The "\n" at the end of each line just means a newline in the plain text email body to make it easier to read. Then the mail() function is run.

This is a very simple example. To expand on this you could (or should) check to ensure that the form fields have had information entered however I'll give an example of that after I've explained control structures ;)

Okay I may be jumping ahead a little here but it's hard to tell you about something and then not use the new found knowledge with a practical example for a few more weeks until I hit the 'meaty' stuff. So a quick explaination of how to email with PHP and then you can tie this together with posting form variables.

The simplest function to email is the mail() function. A quick and simple method would be

<?php mail("to_address@domain.com", "Subject Line Goes Here", "Mail Text goes here", "Headers go here but simple ones are From: me@test.com"); ?>

The headers are just the mailing headers and can be expanded on to state the type of text (plain or html), character set used etc. But for now we'll stick to the simple "From: me@test.com".

So that was an easy lesson!

A friend of mine has set up his own blog primarily for discussing PHP and MySQL along with various other bits of info he finds and stuff he's working on and with. This is the guy I often find on MSN to ask my PHP questions. He knows his stuff 100 times over and I hope to see good things here.

Anyhow, drop him a visit at, and a fantastic domain name it is, Jelly & Custard.

Some of these will be useful now. Some will be used later when I start explaining control structures such as if statements, however here are the operators available to you in PHP

$a += $b same as $a = $a + $b // plus
$a -= $b same as $a = $a - $b // minus
$a *= $b same as $a = $a * $b // multiply
$a /= $b same as $a = $a / $b // divide
$a .= $b same as $a = $a . $b // concatenate (for use in strings)
$a++ same as $a = $a + 1
$a-- same as $a = $a - 1
$a == $b // comparison operator, used in if statements etc.
$a = $b // assignment operator, ie. $a becomes the value of $b - don't get this and the comparison operator mixed up!
$a != $b // not equal to
$a < $b // $a is less than $b
$a <= $b // $a is less than or equal to $b
$a > $b // $a is greater than $b
$a >= $b // $a is great than or equal to $b
!$a // NOT operator, returns true if $a is false
$a && $b // AND operator, returns true if both are true
$a || $b // OR operator, returns true if either are true
$a AND $b // same as AND operator
$a OR $b // same as OR operator

PHP's built-in date() function can take in certain predefined letters and display a variety of formats of the date and time. All of these can be seen in the PHP Manual at http://www.php.net however a few of the most used formats are below:

<?php
print date("H:i T, jS F"); // prints 09:40 GMT, 1st September
print date("d-m-Y"); // prints 01-09-2003
print date("l, dS F Y"); // prints Monday, 1st September 2003
?>

There are more fancier things you can do with a simple date, such as keep an up-to-date copyright notice on the footer of your web site (so when February comes and you still haven't changed it from last year you can use PHP to do this automatically!), and there are more advanced functions available to you, however I'll go into these a little more indepth later on.

Page 5 of 7« First...«34567»