Learning PHP and MySQL

Monday, September 25, 2006

Example 15-1 Building a form that validates its fields before submission

<SCRIPT  LANGUAGE="JavaScript1.2"  SRC="source.js">
</SCRIPT>

<HTML>
<HEAD>
       <TITLE>Sample  Form</TITLE>
</HEAD>

<SCRIPT  LANGUAGE="JavaScript1.2"> 
         function  check_valid(form)  { 
         var  error  =  "";
         error  +=  verify_username(form.username.value);
         error  +=  verify_password(form.password.value); 
         error  +=  verify_phone(form.phone.value);
         error  +=  verify_email(form.email.value);
         if  (error  !=  "")  { 
              alert(error); 
              return  false;
         }
return  true;
}
</SCRIPT>

<BODY  BGCOLOR="#FFFFFF">
         <FORM  action="process.php"  METHOD="post"
onSubmit="return  check_valid(this)"  id="test1"  name="test1">
         <TABLE  BORDER="0"  WIDTH="100%"  CELLSPACING="0" CELLPADDING="0">
              <TR>
                     <TD  WIDTH="30%"  ALIGN="right">Username</TD>
                     <TD  WIDTH="70%">:  <INPUT  TYPE="text"  NAME="username"></TD>
              </TR>
              <TR>
                      <TD  ALIGN="right">Password</TD>
                      <TD>:  <INPUT  TYPE="password"  NAME="password"></TD>
               </TR>
               <TR>
                      <TD  ALIGN="right">Phone</TD>
                      <TD>:  <INPUT  TYPE="phone"  NAME="phone"></TD>
                </TR>
                <TR>
                      <TD  ALIGN="right">Email</TD>
                      <TD>:  <INPUT  TYPE="email"  NAME="email"></TD>
                 </TR>
                 <TR>
                      <TD>&nbsp;</TD>
                      <TD><INPUT  TYPE="SUBMIT"  VALUE="Submit"></TD>
                  </TR>
           </TABLE>
         </FORM>
</BODY>
</HTML>

Posted by krautgrrl on 09/25 at 04:05 PM
Chapter 15 Code • (0) Comments • (0) TrackbacksPermalink

Tuesday, September 26, 2006

Example 15-2 The file source.js contains functions to check the various fields

//  verify username - 6-10 chars, uc, lc, and underscore only. 
function verify_username  (strng)  {
var  error  =  "";
if  (
strng  ==  "")  {
    error  
=  "You didn't enter a username.\n";
}
     
var  illegalChars  =  /\W/;  // allow letters, numbers, and underscores 
      
if  ((strng.length  <  6)  ||  (strng.length  >  10))  {
           error  
=  "The username is the wrong length. It must be 6-10 characters.\n";
      
}
      
else  if  (illegalChars.test(strng))  {
      error  
=  "The username contains illegal characters.\n";
      
}
return error;
}

//  verify password - between 6-8 chars, uppercase, lowercase, and numeral 
function  verify_password  (strng)  {
var  error  =  "";
if  (
strng  ==  "")  {
    error  
=  "You didn't enter a password.\n";
}
    
var  illegalChars  =  /[\W_]/;  //  allow only letters and numbers
    
if  ((strng.length  <  6)  ||  (strng.length  >  8))  {
        error  
=  "The password is the wrong length. It must be 6-8 characters.\n";
     
}
     
else  if  (illegalChars.test(strng))  {
     error  
=  "The  password contains illegal characters.\n";
    
}
    
else  if  (!((strng.search(/(a-z)+/))  &&  (strng.search(/(A-Z)+/))  &&
(
strng.search(/(0-9)+/))))  {
          error  
=  "The password must contain at least one uppercase letter, one 
lowercase letter, and one numeral.\n"
;
        
}
return error;
}

//  verify email
function  verify_email  (strng)  {
var  error="";
if  (
strng  ==  "")  {
    error  
=  "You didn't enter an email address.\n";
}

    
var  emailFilter=/^.+@.+\..{2,3}$/;
     if  (!(
emailFilter.test(strng)))  {
         error  
=  "Please enter a valid email address.\n";
     
}
     
else  {
//test email for illegal characters
          
var  illegalChars=  /[\(\)\<\>\,\;\:\\\"\[\]]/
             if  (strng.match(illegalChars))  {
              error  =  "
The email address contains illegal characters.\n";
         }
      }
return  error;
}

//  verify phone number - strip out delimiters and verify for 10 digits
function verify_phone (strng)  {
var  error  =  "";
if  (strng  ==  "")  {
    error  =  "
You didn't enter a phone number.\n";
}
//strip out acceptable non-numeric characters
var  stripped  =  strng.replace(/[\(\)\.\-\  ]/g,  '');
       if  (isNaN(parseInt(stripped)))  {
           error  =  "The phone number contains illegal characters.";

      }
      if  (!(stripped.length  ==  10))  {
      error  =  "The phone number is the wrong length. Make sure you included an area 
code.\n";
       }
return error;
}

Posted by krautgrrl on 09/26 at 02:58 PM
Chapter 15 Code • (0) Comments • (91) TrackbacksPermalink

Wednesday, September 27, 2006

Example 15-3 Using preg_match to return an array of matches that start with ple

<?php
$subject  
=  "example";
$pattern  =  '/^ple/';
preg_match($pattern,  $subject,  $matches);
print_r($matches);
?>

This code displays:

Array  (  )

Posted by krautgrrl on 09/27 at 11:10 AM
Chapter 15 Code • (0) Comments • (0) TrackbacksPermalink

Example 15-4 Displaying an error from PHP and redisplaying the form with submitted values

<html>
<
head>
<
title>Sample  Form</title>
<
script  type="text/javascript"  src="source.js"></script>
<script  type="text/javascript">
function  check_valid(form)  {
var  error  =  "";
error  +=  verify_username(form.username.value); 
error  +=  verify_password(form.password.value); 
error  +=  verify_phone(form.phone.value);
error  +=  verify_email(form.email.value);
if  (error  !=  "")  { 
alert(error); 
return  false;
}
return  true;
}
</script>
</head>
<body>
<?php
//  Check for form post submit
 
if  ($_POST["submit"])
require_once('db_login.php'); 
require_once(
'DB.php');
$connection  =  DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if  (
DB::isError($connection)){
die  ("Could not connect to the database:  <br  />".  DB::errorMessage($connection));
}
//  Remember to use htmlentities to prevent cross-site scripting vulerablities
$username  =  htmlentities($_POST["username"]);
$password  =  htmlentities($_POST["password"]);
$email  =  htmlentities($_POST["email"]);
$phone  =  htmlentities($_POST["phone"]);
$error  =  "";
if  (
$username  ==  ""){
$error  
.=  "Username must not be null.<br  />";
}
if  ($password  ==  ""){
$error  
.=  "Password must not be null.<br  />";
}
if  ($email  ==  ""){
$error  
.=  "Email must not be null.<br  />";
}
if  ($phone  ==  ""){
$error  
.=  "Phone must not be null.<br  />";
}
//  Query the posts with catagories and user information
$query  =  "SELECT * FROM `users` WHERE `username`='$username'";
//  Execute the database query
$result  =  $connection->query($query);
if  (
DB::isError($result)){
die("Could not query the database: <br  />".$query."  ".DB::errorMessage($result));
}
$user_count  
=  $result->numRows();
if  (
$user_count  >  0)  {
$error  
.=  "Error: Username $username is taken already. Please select another.<br  />";
}
if  ($error){
echo  $error;
}
else  {
echo  "User created successfully.";
exit;
}
}
?>
<form  action="<?php  echo $_SERVER["PHP_SELF"];  ?>" method="POST"
onsubmit="return  check_valid(this);"  id="test1"  name="test1">
<table>
<tr>
<td  width="30%"  align="right">Username:</td>
<td><input  type="text"  name="username"  value="<?php  echo 
htmlspecialchars(stripslashes($username));  ?>"  /></td>
</tr>
<tr>
<td  align="right">Password:</td>
<td><input  type="password"  name="password"  value="<?php echo 
htmlspecialchars(stripslashes($password));  ?>"  /></td>
</tr>
<tr>
<td  align="right">Phone:</td>
<td><input  type="phone"  name="phone"  value="<?php echo 
htmlspecialchars(stripslashes($phone));  ?>"  /></td>
</tr>
<tr>
<td  align="right">Email:</td>
<td><input  type="email"  name="email"  value="<?php echo 
htmlspecialchars(stripslashes($email));  ?>"  /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input  type="submit"  name="submit"  value="Submit"  /></td>
</tr>
</table>
</form>
</body>
</html>

Posted by krautgrrl on 09/27 at 11:13 AM
Chapter 15 Code • (0) Comments • (0) TrackbacksPermalink

Example 16-1 The config.php script defines settings that are used throughout the site

<?php
//  put  full  path  to  Smarty.class.php 
require('/usr/share/php/Smarty/Smarty.class.php');
$smarty  =  new  Smarty();

$smarty->template_dir  =  '/home/www/htmlkb/smarty/templates';
$smarty->compile_dir  =  '/home/www/htmlkb/smarty/templates_c';
$smarty->cache_dir  =  '/home/www/htmlkb/smarty/cache';
$smarty->config_dir  =  '/home/www/htmlkb/smarty/configs';

$blog_title="Coffee  Talk  Blog";
?>

Posted by krautgrrl on 09/27 at 11:30 AM
Chapter 16 Code • (0) Comments • (3) TrackbacksPermalink

Example 16-10 The posts.php script displays a listing of posts and their subjects

<?php
session_start
();
require_once(
'config.php');
require_once(
'db_login.php');
require_once(
"DB.php");
//  Display  the  page  header
$smarty->assign('blog_title',$blog_title);
$smarty->display('header.tpl');
//  Check  for  valid  login
if  (!isset($_SESSION['username']))  {
echo  'Please  <a  href="login.php">login</a>.';
exit;
}
//  Connect  to  the  database
$connection  =  DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");

if  (
DB::isError($connection)){
die  ("Could  not  connect  to  the  database:  <br  />".  DB::errorMessage($connection));
}
//  Query  the  posts  with  catagories  and  user  information
$query  =  "SELECT  *  FROM  `users`  NATURAL  JOIN  `posts`  NATURAL  JOIN  `categories`
ORDER  BY  `posted`  DESC"
;
//  Execute  the  database  query
$result  =  $connection->query($query);
if  (
DB::isError($result)){
die("Could  not  query  the  database:  <br  />".$query."  ".DB::errorMessage($result));
}
//  Place  the  query  results  into  an  array
while  ($result_row  =  $result->fetchRow(DB_FETCHMODE_ASSOC))  {
$test[]  
=  $result_row;
}
//  Send  the  data  to  the  template
$smarty->assign('posts',  $test);
//  Display  the  template  with  the  data  plugged  in
$smarty->display('posts.tpl');
//  Close  the  database  connection
$connection->disconnect();
//  Display  the  page  footer
$smarty->display('footer.tpl');
?>

Posted by krautgrrl on 09/27 at 11:43 AM
Chapter 16 Code • (27) Comments • (94) TrackbacksPermalink

Example 16-11 The posts.tpl template file defines how the postings appear on the page

{section  name=mysec  loop=$posts}
<a  href="view_post.php?post_id={$posts[mysec].post_id}">{$posts[mysec].title}</a>
by  <b>{$posts[mysec].first_name}  {$posts[mysec].last_name}</b>
from  the  <b>{$posts[mysec].category}</b>  category  at  <b>{$posts[mysec].posted}</b>.
<
br  />
{/section}
<br  />
Click to <a  href="modify_post.php?action=add">add</aa posting.<br  />

Posted by krautgrrl on 09/27 at 11:45 AM
Chapter 16 Code • (0) Comments • (126) TrackbacksPermalink

Example 16-12 The view_post.php script displays and a summary of its comments

<?php 

session_start
();

require_once(
'config.php');
require_once(
'db_login.php');
require_once(
"DB.php");

//  Display  the  header
$smarty->assign('blog_title',$blog_title);
$smarty->display('header.tpl');

//  Check  for  valid  login
if  (!isset($_SESSION["username"]))  {
echo  'Please  <a  href="login.php">login</a>.';
exit;
}

//  Connect  to  the  database
$connection  =  DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");

if  (
DB::isError($connection)){
die  ("Could not connect to the database:  <br  />".  DB::errorMessage($connection));
}

$post_id  
=  $_GET["post_id"];

$query  =  "SELECT * FROM `users` NATURAL JOIN `posts` NATURAL JOIN `categories`
WHERE  `post_id`=
$post_id";
$result  =  $connection->query($query);

if  (
DB::isError($result)){
die("Could not query the database: <br  />".$query."  ".DB::errorMessage($result));
}

while  ($result_row  =  $result->fetchRow(DB_FETCHMODE_ASSOC))  {
$test[]
=$result_row;
}

$smarty
->assign('posts',$test);
$smarty->assign('owner_id',$_SESSION["user_id"]);
$query  =  "SELECT * FROM `users` NATURAL JOIN `comments` WHERE `post_id`=$post_id";
$result  =  $connection->query($query);

if  (
DB::isError($result)){
die("Could not query the database:  <br  />".$query."  ".DB::errorMessage($result));
}

$comment_count  
=  $result->numRows();

while  (
$result_row  =  $result->fetchRow(DB_FETCHMODE_ASSOC))  {
$comments[]  
=  $result_row;
}

$smarty
->assign('posts',$test);
$smarty->assign('comments',$comments);
$smarty->assign('comment_count',$comment_count);

$smarty->display('view_post.tpl');

$connection->disconnect();

//  Display the footer
$smarty->display('footer.tpl');

?>

Posted by krautgrrl on 09/27 at 11:46 AM
Chapter 16 Code • (13) Comments • (65) TrackbacksPermalink

Example 16-13 view_post.tpl

{section  name=mysec  loop=$posts}
<h2>{$posts[mysec].title}</h2>
{$posts[mysec].body}
<br  />
Posted  by  <b>{$posts[mysec].first_name}  {$posts[mysec].last_name}</b>
from  the  <b>{$posts[mysec].category}</b>  category  at
<b>{$posts[mysec].posted}</b>.<br  />
{if  $posts[mysec].user_id  ==  $owner_id}
<a  href="modify_post.php?post_id={$posts[mysec].post_id}&action=edit">Edit</a>  ||
<
a  href="modify_post.php?post_id={$posts[mysec].post_id}&action=delete">Delete</a>  ||
<
a  href="modify_comment.php?post_id={$posts[mysec].post_id}&action=add"
>Add  a  comment</a>
<
br  />
{/if}
{
/section}
{if  $comment_count  
!=  "0"}
<h3>Comments</h3>
{section  name=mysec2  loop=$comments}
<hr  />
<
b>{$comments[mysec2].title}</b>
<
br  />
{$comments[mysec2].body}
<br  />
Posted  by  <b>{$comments[mysec2].first_name}  {$comments[mysec2].last_name}</b>
at  <b>{$comments[mysec2].posted}</b>.<br  />
{if  $comments[mysec2].user_id  ==  $owner_id}
<a  href="modify_comment.php?comment_id={$comments[mysec2].comment_id}&action=edit"
>Edit</a>  ||
<
a  href="modify_comment.php?comment_id={$comments[mysec2].comment_id}&action=delete"
>Delete</a>
<
br  />
{/if}
{
/section}
{
/if}

Posted by krautgrrl on 09/27 at 11:51 AM
Chapter 16 Code • (7) Comments • (226) TrackbacksPermalink

Example 16-14 modify_posts.php

<?php
    
include('db_login.php');
    require_once(  
'DB.php'  );
    require_once(  
'config.php'  );

    
//check  for  valid  login
    
session_start();

    
//display  the  header
    
$smarty->assign('blog_title',$blog_title);
    
$smarty->display('header.tpl');

    if    (!isset(
$_SESSION['username']))  {
    
echo  ("Please  <a  href='login.php'>login</a>.");
    exit();
    
}
    
//grab  submission  variables
    
$post_id=$_POST[post_id];
    
$title=htmlentities($_POST['title']);
    
$body=htmlentities($_POST['body']);
    
$action=htmlentities($_POST['action']);
    
$category_id=htmlentities($_POST['category_id']);
    
$user_id=$_SESSION["user_id"];

    
//conected to database
    
$connection  =  DB::connect(  "mysql://$db_username:$db_password@$db_
host/
$db_database"  );
    if  (!
$connection)
    
{
    
die  ("Could not connect to the database: <br>"DB::errorMessage());
    
};
    if  (
$_GET['action']=="delete"  and  !$stop)
    
{
    $post_id
=$_GET[post_id];
    
$query  =  "delete from posts where post_id='".$post_id."' and
user_id='"
.$user_id."'";
    
$result  =  $connection->query($query);
    if (
DB::isError($result))
    
{
    
die ("Could not  query the database: <br>".  $query.  "  ".
DB::errorMessage($result));
    
};
    echo  (
"Deleted  successfully.<br>");
    
$stop="TRUE";
    
}

    
//we're editing an entry, explicitly grab the id from the URL
    
if  ($_GET[post_id]  AND  !$stop)  {
    $query  
=  "SELECT * FROM users NATURAL JOIN posts NATURAL JOIN categories
where  post_id  =  
$_GET[post_id]";
    
$result  =  $connection->query($query);
    if  (
DB::isError($result))
    
{
    
die  ("Could not query the database: <br>"$query.  "  ".
DB::errorMessage($result));
    
};
    while  (
$result_row  =  $result->fetchRow(DB_FETCHMODE_ASSOC))  {
    $posts[]
=$result_row;
    
}
    $smarty
->assign('action','edit');
    
$smarty->assign('posts',$posts);
    
//get  those  categories
    
$query  =  "SELECT  category_id, category FROM categories";
    
$smarty->assign('categories',$connection->getAssoc($query));
    
$smarty->display('post_form.tpl');
    
$stop="TRUE";
    
}

    
//The form was submitted, was it an add or an edit?
    
if  ($_POST['submit']  AND  !$stop)
    
{
    
//validate fields
    
if  ($title  ==  ""){
    
echo  ("Title must not be null.<br>");
    
$found_error=TRUE;
    
$stop="TRUE";
    
}
    
if  ($body  ==  ""){
    
echo  ("Body must not be null.<br>");
    
$found_error=TRUE;
    
$stop="TRUE";
    
}
    
//validated OK lets hit the databae
    
if  (  $_POST['action']=="add" AND !$stop)
    
{
    $query 
"insert into posts values  (NULL,
"
."'".$category_id."','".$user_id."','".$title."','".$body."',  NULL)";
    
$result  =  $connection->query($query);
    if  (
DB::isError($result))
    
{
    
die  ("Could not query the database: <br>"$query.  "  ".
DB::errorMessage($result));
    
};
    echo  (
"Posted successfully.<br>");
    
$stop="TRUE";
    
}
    
if  ($_POST['action']=="edit" and !$stop)
    
{
    
//do nothing
    
$query  =  "update posts set category_id  ='".$category_id."',
title  ='"
.$title."',body='".$body."' where post_id='".$post_id."'
and  user_id='"
.$user_id."'";
    
//echo $query;
    
$result $connection->query($query);
    if  (
DB::isError($result))
    
{
    
die  ("Could not query the database: <br>"$query.  "  ".
DB::errorMessage($result));
    
};
    echo (
"Updated successfully.<br>");
    
$stop="TRUE";
    
}
    }
    
if  (!$stop)
    
{
    
//display blank form
    //create an empty entry
    
$result_row=array('title'=>NULL,'body'=>NULL);
    
$posts[]=$result_row;
    
//get the categories
    
$query "SELECT category_id, category FROM categories";
    
$smarty->assign('categories',$connection->getAssoc($query));
    
$smarty->assign('posts',$posts);
    
$smarty->assign('action','add');
    
$smarty->display('post_form.tpl');
    
}

    
if  ($found_error)  {
    
//assign old vals
    //redisplay form
    
$result_row=array('title'=>"$title",'body'=>"$body",'post_id'=>"$post_id");
    
$posts[]=$result_row;
    
$smarty->assign('action',$action);
    
$smarty->assign('posts',$posts);
    
$smarty->display('post_form.tpl');
    
}
    
//display the footer
    
$smarty->display('footer.tpl');

?>

Posted by krautgrrl on 09/27 at 11:55 AM
Chapter 16 Code • (6) Comments • (12) TrackbacksPermalink

Example 16-15 post_form.tpl

<form action="modify_post.php" method="POST">
<
label>
Title:  <input type="text" name="title" value="{$posts[mysec].title}">
</
label>
<
br  /><br  />
<
label>
Body:  <textarea name="body" cols="40" rows="4">{$posts[mysec].body}</textarea>
</
label>
<
input  type="hidden" name="action"  value="{$action}">
<
input  type="hidden" name="post_id"  value="{$posts[mysec].post_id}"><br>
<
labelCategory:
{html_options  name="category_id" options=$categories selected=$posts[mysec].category_id}
</label>
<
br  />
<
input  type="submit" name="submit" value="Post"  />
</
form>
{/section}

Posted by krautgrrl on 09/27 at 12:19 PM
Chapter 16 Code • (0) Comments • (1200) TrackbacksPermalink

Example 16-16 modify_comment.php

<?php 

session_start
();

require_once(
'config.php'); 
require_once(
'db_login.php'); 
require_once(
"DB.php");

//  Display  the  header
$smarty->assign('blog_title',$blog_title);
$smarty->display('header.tpl');

//  Check for valid login
if  (!isset($_SESSION["username"]))  {
echo 'Please <a  href="login.php">login</a>.';
exit;
}

//  Connect to the database
$connection DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");

if  (
DB::isError($connection)){
die  ("Could not connect to the database: <br  />"DB::errorMessage($connection));
}

$stop 
false;

$post_id  =  $_REQUEST["post_id"];

$title htmlentities($_POST['title']);
$body htmlentities($_POST['body']);
$action htmlentities($_POST['action']);
$category_id htmlentities($_POST['category_id']);
$user_id $_SESSION["user_id"];
$comment_id htmlentities($_POST['comment_id']);

if  (
$_GET['action'== "delete" and !$stop)  {
$comment_id 
$_GET["comment_id"];
$query "DELETE  FROM  `comments`  WHERE  `comment_id`='".$comment_id."' AND  `user_id`='".$user_id."'";
$result $connection->query($query);
if (
DB::isError($result)){
die("Could  not  query  the  database:  <br  />".$query."  ".DB::errorMessage($result));
}
echo "Deleted successfully.<br  />";
$stop true;
}

// We're editing an entry, explicitly grab the id from the URL
if ($_GET["comment_id"and !$stop)  {
$query  
=  "SELECT * FROM `comments` NATURAL JOIN `users`
WHERE `comment_id`="
.$_GET["comment_id"];

$result  =  $connection->query($query);
if  (
DB::isError($result)){
die("Could not query the database: <br  />".$query."  ".DB::errorMessage($result));
}
while  ($result_row  =  $result->fetchRow(DB_FETCHMODE_ASSOC))  {
$comments[]  
=  $result_row;
}
$post_id  
=  $_GET["post_id"];
$smarty->assign('action','edit');
$smarty->assign('comments',$comments);
$smarty->assign('post_id',$post_id);
$smarty->display('comment_form.tpl');
//  Display the footer
$smarty->display('footer.tpl');
exit;
}

//The form was submitted, was itan add or an update?
if ($_POST['submit']  and  !$stop)  {
// Validate fields if  ($title  ==  ""){
echo  'Title must not be null.<br  />';
$found_error  =  true;
$stop  =  true;
}
if  ($body  ==  ""){
echo  "Body must not be null.<br  />";
$found_error true;
$stop true;
}
// Validated OK lets hit the database
if  ($_POST['action'== "add" AND !$stop{
$query  
=  "INSERT INTO `comments` VALUES (NULL,
'"
.$user_id."','".$post_id."','".$title."','".$body."',  NULL)";
$result  =  $connection->query($query);
if  (
DB::isError($result)){
die("Could not query the database: <br  />".$query."  ".DB::errorMessage($result));
}
echo "Posted successfully.<br  />";
$stop true;
}
if ($_POST['action']=="edit" and !$stop){
$query 
"UPDATE `comments` SET
`title`='"
.$title."',
`body`='"
.$body."'
WHERE `comment_id`='"
.$comment_id."' AND `user_id`='".$user_id."'";
$result  =  $connection->query($query);
if (
DB::isError($result)){
die("Could not query the database: <br  />".$query." ".DB::errorMessage($result));
}
echo 'Updated successfully.<br  />';
$stop true;

}
}

if  (!$stop){
//  Display blank form
// Create an empty entry
$post_id $_GET["post_id"];
$result_row = array('title'=>NULL,'body'=>NULL,'comment_id'=>NULL);
$comments[] $result_row;
// Get the categories
$smarty->assign('post_id',$post_id);
$smarty->assign('comments',$comments);
$smarty->assign('action','add');
$smarty->display('comment_form.tpl');
}

if  ($found_error)  {
//  Assign old vals
// Redisplay form
$post_id $_POST["post_id"];
$result_row = array('title'=>"$title",'body'=>"$body",'comment_id'=>"$comment_id");
$comments[] $result_row;
$smarty->assign('action',$action);
$smarty->assign('post_id',$post_id);
$smarty->assign('comments',$comments);
$smarty->display('comment_form.tpl');
}

//  Display the footer
$smarty->display('footer.tpl');

?>

Posted by krautgrrl on 09/27 at 12:25 PM
Chapter 16 Code • (3) Comments • (12) TrackbacksPermalink

Example 16-17 comment_form.tpl

{section  name=mysec  loop=$comments}
<form  action="modify_comment.php"  method="post">
<
labelTitle:
<
input  type="text"  name="title"  value="{$comments[mysec].title}"  />
</
label>
<
br  />
<
br  />
<
labelBody:
<
textarea  name="body"  cols="40"  rows="4">{$comments[mysec].body}</textarea>
</
label>
<
input  type="hidden"  name="action"  value="{$action}"  />
<
input  type="hidden"  name="post_id"  value="{$post_id}"  />
<
input  type="hidden"  name="comment_id"  value="{$comments[mysec].comment_id}"  />
<
br  /><br  />
<
input  type="submit"  name="submit"  value="Post"  />
</
form>
{/section}

Posted by krautgrrl on 09/27 at 12:36 PM
Chapter 16 Code • (9) Comments • (212) TrackbacksPermalink

Example 16-2 The header.tpl file

<html>
<
head>
<
title>{$blog_title}</title>
</
head>
<
body>
<
h1>Welcome  to  the  {$blog_title}</h1>

Posted by krautgrrl on 09/27 at 11:31 AM
Chapter 16 Code • (0) Comments • (0) TrackbacksPermalink

Example 16-3 The footer.tpl file

<hr>
<
a  href='posts.php'>Home</a>  ||  <a  href='logout.php'>Logout</a>
</
head>
</
body>
</
html>

Posted by krautgrrl on 09/27 at 11:32 AM
Chapter 16 Code • (1) Comments • (8) TrackbacksPermalink
Page 4 of 5 pages « First  <  2 3 4 5 >

Statistics

This page has been viewed 375216 times
Page rendered in 0.3826 seconds
Total Entries: 224
Total Comments: 16
Total Trackbacks: 307338
Most Recent Entry: 09/27/2006 12:39 pm
Most Recent Comment on: 10/26/2007 10:00 am
Total Members: 2
Total Logged in members: 0
Total guests: 8
Total anonymous users: 0
Most Recent Visitor on: 02/10/2012 09:46 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm

Referrers

Powered by ExpressionEngine