the page reloading on refresh is because, the post still resides on the page.
i fixed the code to include the checkbox for saving, and also to block the current user in the username field which remains disabled through out his login.
The save functionality is partially implemented, but whatver else u had asked is present in this code.
my assumed file name for the following code is "main.php"
PHP Code:
<?php session_start(); ?>
<form id="form1" name="form1" method="post" action="">
<label>
<strong>Username</strong>:
<input type="text" name="user" id="user" <?php if(isset($_SESSION['user'])){?> disabled="disabled" value="<?php echo $_SESSION['user'];?>" <?php }?>/>
</label>
<p><strong>Comments: </strong></p>
<p>
<label>
<textarea name="comments" id="comments" cols="45" rows="5"></textarea>
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</p>
</form>
<?php session_start();
include('dbconn.php');
$date = date("Y-m-d");
$time = date("g:i a");
if(isset($_POST['user'])){
$username = $_POST['user'];
}
if(strlen($username) == 0){
$sessionUser = "hello";
}
if(isset($_POST['comments'])){
$comments = $_POST['comments'];
}
if(strlen($username)>0 || strlen($comments)>0){
//insert this value
if(strlen($username == 0)){
$username = "Kris"; //point this value to use the session variable of your current user.
}
$insert = "insert into comments(username,time,date,comment)values('$username','$time','$date','$comments');";
$suc = mysql_query($insert);
$newID = mysql_insert_id();
if($suc){
$url = "redirect.php?mode=redirect";
header('Location: '.$url);
}
}
//fetch the latest x amount of comments;
$fetch = "select *,id from comments order by date limit 20;";
$success = mysql_query($fetch);
?>
<table width="100%" border="0">
<?php
while($info = mysql_fetch_array($success)){
?>
<tr>
<?php if($_SESSION['user'] == "Kris"){?>
<form id="form2" name="form2" method="post" action="save.php">
<td align="left">
<label>
<input type="checkbox" name="<?php $info['id'];?>" id="save" />
</label></td><?php }?>
<td width="21%" align="left" style="color:#006600;"><?php echo ucfirst($info['username']);?><?php echo " » ".$info['date']." @".$info['time'];?></td>
<td width="79%" align="left" style="color:#000000;"><?php echo ucfirst(nl2br($info['comment']));?></td>
</tr>
<?php }?>
</table>
<?php if($_SESSION['user'] == "Kris"){?>
<label>
<input type="submit" name="button2" id="button2" value="Save" />
</label>
</form>
<?php }?>
second reference code file: redirect.php
PHP Code:
<?php session_start();
$mode = $_GET['mode'];
if($mode == "redirect"){
header('Location: main.php');
exit;
}
?>
Play with it, you might be able to enhance it even further
Bookmarks