$POST

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ferronrsmith
Forum Newbie
Posts: 3
Joined: Sat Apr 04, 2009 3:50 pm

$POST

Post by ferronrsmith »

I am a noob, just need you guys help. i am creating a one page guestbook.

I want after the user as entered there comments and its saved to the database it unsets the $_POST's so that data is not present in them. I tried putting the unset at the top but it deletes all the info before the record is posted. How can i fix this so that after user leave a comment it unsets. URgent help please

Code: Select all

 
 <?php
 
  $name = trim($_POST['name']);
  $email = trim($_POST['email']);
  $comments =  trim($_POST['comments']);
  $webpage = 'http://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
 
 ?>
 
 
  
  
<html>
<head>
<title>Sample Comment</title>
<script src="validation.js" type="text/javascript" language="javascript"> </script>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>
<body>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td align="center"><strong>Sample Comment Page </strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<!-- <form id="form" name="form" method="post" action="addguestbook.php"> -->
<form id="form" name="form" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<td>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><input name="name" type="text" id="name" size="40" class="reqd"/></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" id="email" size="40" class="reqd"/></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><textarea name="comments" cols="40" rows="3" id="comments" class="reqd"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="button" name="Enter" value="Submit" onclick= "validateForm(); return false;" /><input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
 
<?php
 
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="enter"; // Mysql password 
$db_name="ferronh"; // Database name 
$tbl_name="comments"; // Table name
 
 
   
// Remember to create a table named comments, make the necessary corrections to connect to your database
 
// Connect to server and select database.
$conn = mysql_connect("$host", "$username", "$password")or die("cannot connect server "); 
$select = mysql_select_db($db_name, $conn)or die("cannot select DB");
 
if (!$select) {
    die ('Can\'t select db : ' . mysql_error());
}
 
$datetime=date("y-m-d h:i:s"); //date time
if (isset ($_POST['name']) && ($_POST['email']) && ($_POST['comments']))
{
 
 // $name = $_POST['name'];
 // $email = $_POST['email'];
 // $comments =  $_POST['comments'];
 // $webpage = 'http://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
 // $status = 0;
 
 unset ($_POST['name']);
 unset ($_POST['email']);
 unset ($_POST['comments']);  
 
$sql="INSERT INTO $tbl_name(name, email, comments, datetimecreated, webpage)VALUES('$name', '$email', '$comments', '$datetime', '$webpage')";
   
   
 
    
$result=mysql_query($sql);
if (!$result) {
    die ('Can\'t run query : ' . mysql_error());
}
 
//check if query successful 
if($result){
//echo "Successful";
 
//echo "<BR>";
//echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page 
}
 
else {
//echo "ERROR";
}
}
 
 
 
$sql2="SELECT * FROM $tbl_name";
$results=mysql_query($sql2);
 
while($rows=mysql_fetch_array($results)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><?php echo $rows['name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><?php echo $rows['email']; ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><?php echo $rows['comments']; ?></td>
</tr>
<tr>
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td><?php echo $rows['datetimecreated']; ?></td>
</tr>
<tr>
<td valign="top">WebPage </td>
<td valign="top">:</td>
<td><?php echo $rows['webpage']; ?></td>
</tr>
</table></td>
</tr>
</table>
 
<?php } mysql_close(); //close database ?>
 
 
 
 
</body>
</html>
 
 
Randwulf
Forum Commoner
Posts: 63
Joined: Wed Jan 07, 2009 7:07 am

Re: $POST

Post by Randwulf »

I don't see why that's necessary. It's automatically wiped once the script finishes.

However, to answer your question, you can put unset($_POST) at the end of the script rather than at the start.

Plus you need to remove the comment slashes before the variable assignments (if you forgot to do that)
ferronrsmith
Forum Newbie
Posts: 3
Joined: Sat Apr 04, 2009 3:50 pm

Re: $POST

Post by ferronrsmith »

Yeah i know, was just testing some stuff before actual implementation. Thanks for the advice
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: $POST

Post by s.dot »

Send the form data to a different script and then redirect back to this script.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply