The list of all PHP and Mysql posts

how to reply a topic

We want to develope a form for the forum visitors to reply each others posts. It's totally look like the newtopic page , so I recommend you to read that post first.

http://webprogram4beginners.blogspot.com/2011/04/how-to-add-newtopic-ability-to-forum.html

First we need a form for reply enteries. Put the following codes in "reply.php".


code:

<?php
include "auth.inc"; // we check that if user has loged in or not

session_start();

include ("funcs.php"); // the functions look like : connect (to connect to the database) and creatcode (for verification text) can be put in a file and then we include that file wherever it is required.

?>
<a href="logout.php">log out</a>
<br>

// the following html codes show a menu-bar on the page . they have been created by Frontpage . You can omit them.

<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>reply this post </title>
</head>
<body bgcolor="#99FF66" >
<table border="0" cellpadding="0" cellspacing="0" width="1055" height="625">
 <!-- MSTableType="layout" -->
 <tr>
  <td valign="top" colspan="3" height="148">
  <!-- MSCellType="ContentHead" -->
  <p align="center">
&nbsp;
<br>
<!--webbot bot="Navigation" S-Type="sequence" S-Orientation="horizontal" S-Rendering="graphics" S-Theme="poetic 0000" B-Include-Home="FALSE" B-Include-Up="FALSE" U-Page="sid:1001" --><a href="index.php"><img src="_derived/index.php_cmp_poetic000_hbtn.gif" width="154" height="37" border="0" alt="index.php" align="middle"></a> <a href="about.php"><img src="_derived/about.php_cmp_poetic000_hbtn.gif" width="154" height="37" border="0" alt="about.php" align="middle"></a> <a href="search.php"><img src="_derived/search.php_cmp_poetic000_hbtn.gif" width="154" height="37" border="0" alt="search.php" align="middle"></a><!--webbot bot="Navigation" i-checksum="38819" endspan --><br>
&nbsp;</p>
  <p><font size="6"><b>REply this post </b></font></td>
 </tr>
 <tr>
  <td valign="top" width="161">
  <!-- MSCellType="NavBody" -->
  &nbsp;</td>
  <td valign="top" width="747">
  <!-- MSCellType="ContentBody" -->
  <p align="justify"><b><font size="4">

<?php

// when the user clicks on the submit button (on the page) , this form information is sent to an other php page to process , for example "reply_preview.php" , there , base on the entered data two sessions ($_SESSION["sec_code_err_3"] , $_SESSION["fill_all_of_fields_2"] ) are used to inform incorrect entered code or format.

// if the security code is entered incorrect $_SESSION["sec_code_err_3"] is set to 1 and the browser is redirected to "reply.php" again.

// if one of the text fields is empty, $_SESSION["fill_all_of_fields_2"] is set to 1 and the browser is redirected to "reply.php" again.

if($_SESSION["sec_code_err_3"] == 1){
 echo "please enter the code correctly";
 $_SESSION["sec_code_err_3"] = 0;

}

if($_SESSION["fill_all_of_fields_2"] == 1){
 echo "please fill all of the fields";
 $_SESSION["fill_all_of_fields_2"] = 0;

}

create_code(); // please see "the verification code  post" on the weblog

?>

// this form information is sent to an other php page to process , for example "reply_preview.php". the "Textlink" is the variable that is sent to "reply_preview.php" , There, "Textlink" is saved to the database with the others Reply enteries. In future , the "replies" of a post can be extracted by Textlink from the database.

<FORM ACTION="reply_preview.php?Textlink=<?php echo  $_GET["Textlink"]; ?> " METHOD=POST>

<BR>
<BR>
post body:
<BR>
<textarea name="body" ROWS=20 COLS=100 ></textarea>
<BR>
<BR>
<INPUT TYPE="TEXT" NAME="SecCode3" SIZE="30"  >
<BR>
<img src="security_image.php">
<BR>
<BR>
<BR>
<INPUT TYPE="SUBMIT" NAME="BUTTON2" VALUE="new_reply">
</FORM>
</td>
  <td valign="top" height="427" width="147">
  <!-- MSCellType="NavBody2" -->
  &nbsp;</td>
 </tr>
 <tr>
  <td valign="top" colspan="3" height="50">
  <!-- MSCellType="ContentFoot" -->
  &nbsp;</td>
 </tr>
</table>
</body>
</html>
<p>
<br>
</p>
<html dir="ltr">

when the user clicks on the submit button (on the page) , this form information is sent to an other php page to process , for example "reply_preview.php" , there the enteries are processed and if there is any problem such as , wrong verification code , empty text field and wrong format for enteries , the user is redirected to "reply.php" to correct his enteries and the appropriate message is shown related to the problem . If there isn't any problem then the enteries will be recorded in the database and the browser will be directed to the post that the user had intended to reply .

In the following you can see the codes of "reply_preview.php" page :


code:

<?php
include "auth.inc";
include "funcs.php";
session_start();

// some processes over security code .

$UserSecCode = strtolower($_POST["SecCode3"]);
$SysSecCopde = strtolower($_SESSION["SecImageStr"]);
if($UserSecCode != $SysSecCopde){

// check if the security image has been entered correctly

 $_SESSION["sec_code_err_3"] = 1;

// notice the redirection here, "Textlink" variable should be sent.

 $dd = "reply.php?Textlink=".$_GET["Textlink"];
 header("Location: $dd");
 exit;
}

if( (($_POST["body"])=="") )
{

// check if there is any of the text fields empty

 $_SESSION["fill_all_of_fields_2"] = 1;
 $dd = "reply.php?Textlink=".$_GET["Textlink"];
 header("Location: $dd");
 exit;
}else{

// deactivate the error sessions because now there is no problem

$_SESSION["sec_code_err_3"] = 0;
$_SESSION["fill_all_of_fields_2"] = 0;

// the author of the reply is one of the things that should be recorded to the database:

$user = $_SESSION["auth_usr"];
$user = htmlspecialchars($user);
$user = str_replace("'", "", $user);
$user = str_replace(".", "", $user);
$user = str_replace("/", "", $user);

// the date is an other thing that should be recorded in to the database.

$Today = date("Ymd");

// the post link (Textlink) , is an identity for the reply , it is recorded to the database.

$topiclink = $_GET["Textlink"];

// After each time of redirection (to correct the errors of the user who wants to submit a reply) , some unwanted "space" characters may appear in the address bar , we can get rid of them by a "trim" and "str_replace".

$topiclink = trim($topiclink);

$topiclink = str_replace("%20", " ", $topiclink); // notice that "%20" in the address bar is equal to "space" character.

// the reply body should be recorded in to the database.

$data = $_POST["body"];
connect_2();
$Today = date("Ymd");

// the reply information (author,link,body,date) are recorded to the database.

$sql = "INSERT INTO reply (author,link,text,date) VALUES ('$user','$topiclink','$data','$Today')";
mysql_query($sql);

//after the completion of the record , the browser is redirected to the post page , and the user can see his reply under the post.

$dd = "showtopic.php?Textlink=".$_GET["Textlink"];
header("Location: $dd");
exit;
}
?>

The comments in codes are enough to understand them. You can go to "How to show a certain post by user demand" to understand how the replies are shown under a post.

No comments:

Post a Comment