Saturday, April 25, 2009

a simple content management system: finis

I've made a few finishing touches to my Contacts application by including some functionality to allow users to add new records to the database table.

In a new file I named insert.php, I've added two main sections. Firstly, I've written some code to take user form inputs and store them into my database table, as shown here:

<?php

// If "Insert Contact" form was submitted.
if ($_POST['action'] == 'saveform') {
// Load the user inputs into local variables.
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$email = $_POST['email'];
// Insert new record into database.
$sql = "INSERT INTO contacts ( first_name, last_name, phone, email ) VALUES ( '$firstname', '$lastname', '$phone', '$email' );";
mysql_query($sql) or die(mysql_error());
// Return to list of contacts page.
echo "
<script type=\"text/javascript\">
window.location=\"index.php\";
</script>
";
}
?>

I also built a form in the <body> section of the HTML for users to submit:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Maya's Contact CMS</title>
</head>
<body>

<h3>Insert Contact</h3>

<form name="InsertContact" method="post">
<input type="hidden" name="action" value="saveform" />
<table border="1" cellpadding="3" cellspacing="0">
<tr><th align="left">First Name:</th><td><input type="text" name="firstname" value="" /></td></tr>
<tr><th align="left">Last Name:</th><td><input type="text" name="lastname" value="" /></td></tr>
<tr><th align="left">Phone:</th><td><input type="text" name="phone" value="" /></td></tr>
<tr><th align="left">E-mail:</th><td><input type="text" name="email" value="" /></td></tr>
<tr><td align="center" colspan="2"><input type="submit" name="submit" value="Save" /></td></tr>
</table>
</form>

</body>
</html>

And that's it! I now have a rudimentary content management system built solely with HTML and PHP.

Please feel free to check it out and test the functionality here.

0 comments:

  © 2009. Template by Ourblogtemplates.com.