Welcome to PHPMule.com
A blog site which I am posting to during my learning of the popular programming language PHP. Feel free to leave comments!
-
Making A Comments Form
I have been experimenting with PHP over the last couple hours trying to design a comments form. By the way I am the newbee of newbies at this stuff so it takes a while to know the concepts!
Well here goes:
<html>
<head>
<title>Comment Form - Submit your comments!</title>
</head>
<body>
<form action=”processcomment.php” method=”post”>
<table border=”0″>
<tr>
<td>First Name: </td>
<td align=”left”><input type=”text” name=”firstname” size =”20″ maxlength=”20″ /></td>
</tr>
<tr>
<td>Email Address: </td>
<td align=”left”><input type=”text” name=”emailaddress” size =”30″ maxlength=”30″ /></td>
</tr>
<tr>
<td>Comments: </td>
<td align=”left”><input type=”text” name =”comments” size =”100″ maxlength=”100″ /></td>
</tr>
<tr>
<td colspan=”2″ align =”center”><input type=”submit” value=”Submit Comment” /></td>
</tr>
</table>
</form></body>
</html>This is the comments form that I created. (Using HTML, PHP Script to follow) This will be a basic comments form to take the first name, email address and obviously their comments. You will notice that the form’s action contains the file name “processcomments.php”. This is the file that get’s processed once the submit button on the form is pressed.
The following file contains three global variables. (e.g. $_POST['firstname'] ) You might notice that the value your visitor sent when they entered their ‘firstname’ will now be the variable and therefore can be controlled in the processcomments.php script. Same goes with their emailaddress and comments.
Here is the processcomments.php file:
<?php
$firstname = $_POST['firstname'];
$emailaddress = $_POST['emailaddress'];
$comments = $_POST['comments'];
?><html>
<head>
<title>Process Comment</title>
</head><body>
<?php
echo “<p>Following comment sent: </p><br />”;
echo “First Name: ” . $firstname . “<br />”;
echo “Email Address: ” . $emailaddress . “<br />”;
echo “Comments: ” . $comments . “<br />”;
?></body>
</html>
When you press the ‘Submit Comment’ button this file is opened and will output this to the browser:
Following comment sent:
First Name: Nathan
Email Address: admin@phpmule.com
Comments: My very first comments box!Although it say’s it’s sent, it’s actually not until we add a bit more code… Will add how to do this in the next blog post!
(Post created on Monday, April 6th 09 at 21:55)
-
PHP - Conditional Statements
In PHP, you can use Conditional Statements to make decisions. Let’s say you wanted to give your customer a discount if he/she has an order over £5 (I know I’m generous!). You can easily do this by using the ‘If’ statement.
Let’s create the IF statement:
<?php
define(’CHOCOLATECAKE’, 3.50);
define(’SWISSROLL’, 1.50);$subtotal = CHOCOLATECAKE + SWISSROLL;
echo “Sub total of your order: £” . $subtotal . “. Thanks for visiting! “;
if( $subtotal >= 5.00 )
{
$totalcost = ($subtotal * 0.90);
$grandtotal = number_format($totalcost,2);
echo “This order qualified for a discount! “;
echo “Your grand total is: £” . $grandtotal;
}
else {
$subtotal = number_format($subtotal,2);
echo “Your grand total for your order is £” . $subtotal;
}
?>
As you may notice I am charging customer’s 3.50 for the cake and 1.50 for the swiss roll. If the customer bought both of these (equalling £5) it would be eligable for discount! The basic princpiple is that if the $subtotal is over the value of 5 then it creates a new variable called $totalcost. The $totalcost is worked out by the $subtotal being multiplied by 0.90 to subtract 10% off the order.
You will also notice there is an ‘Else’ function in there as well. If the ‘If’ function turns false (as in the order is below £5) the ‘Else’ function is performed. The Else funtion in this script will output the $subtotal variable if the order is below £5
The layout of writing an IF statement is as follows: if (your argument here) {commands here}
(Post created on Monday, April 6th 09 at 17:30)
-
PHP - Constants
A constant in PHP stores a value like a variable but if a Constant is given a value it can’t be changed in the script.
To declare a constant you have to set it out in the following way. Let’s say we wanted to give a chocolate cake a fixed price:
define(’CHOCOLATECAKE’, 1.50);
or even a swiss roll! ummm
define(’SWISSROLL’, 1.25);
If you wanted to use a constant all you have to do is use the name of the Constant such as:
echo CHOCOLATECAKE;
or even:
echo SWISSROLL;
Using these Constants will obviously output the price to the browser. You can use these constants to work out the total cost of both by using Variables and Constants together:
$totalcost = CHOCOLATECAKE + SWISSROLL;
The $totalcost now has a value of 2.75 and subsequently this variable is now of the ‘float’ data type as it’s a decimal number. You can see why PHP is a very good language to build customer order forms etc. You now have a variable that has a value and we can now output this to the browser:
echo “Total cost of your order: £” . $totalcost . “. Thanks for visiting!”;
As you can see you can effectively use constants and variables together!
(Post created on Monday, April 6th 09 at 14:15)
-
PHP - Variable Types
In PHP, there are 6 data types:
Some of them everybody may know of already:
String - This data type has quite simply a string of data such as flower231tulip
Integer - This data type holds numbers such as 834
Float - This data type is used for real numbers such as 12.45
Boolean - This data type is used for true or false values
Array - Stores multiple data items
and the Object data type.
If a variable hasn’t got a value the data type is known as ‘NULL‘ to indicate nothing basically!
(Post created on Monday, April 6th 09 at 13:25)
-
Archives
Recent Articles
- PHP - Rounding Off Numbers
- PHP - Single vs Double Quotation Marks
- PHP - Loading Files Into Your PHP Script By Using require() and include()
- PHP - Replacing Strings With Other Strings
- PHP - Finding Strings Within Strings
- PHP - Using strlen() To Test String Length
- PHP - Using The explode() Function
- PHP - Changing The String Case
- PHP - The nl2br() Function
- PHP - Trimming Strings
Links
Categories
- PHP (22)
- Uncategorized (1)


