Need Help - Simple Submit News Form Not Quite Right
Posted: Mon Jun 29, 2009 10:22 am
Well everyone has to start somewhere, and I am a noob to PhP but loving every minute of the learning process. Anyway I have come up with a post news page that puts news into my database. I have a two point problem (seperate issues) Can anyone look at my code and see what I am missing. Or need to edit or remove.
Point 1 - I would like to either show something that says "Your post has been submitted" or re-direct me to a "post submitted" page. For some reason when trying the former it gave me the text before i posted the item. Which is a bit unhelpful.lol
Point 2 - I keep getting double blank posts on submit or refresh. On submit it happens with two blank posts especially when i integrate my form into a website template. But i think if i put some sort of protection against blank posts going in, it will solve both problems. Or to kill two birds with one stone, how do i make a "cannot leave blank" field?
My code below
Thank you in advance for any help. I know some of the coding must be messed up, but it does what it is supposed to, however doesnt do the little bits i need it to .lol
Point 1 - I would like to either show something that says "Your post has been submitted" or re-direct me to a "post submitted" page. For some reason when trying the former it gave me the text before i posted the item. Which is a bit unhelpful.lol
Point 2 - I keep getting double blank posts on submit or refresh. On submit it happens with two blank posts especially when i integrate my form into a website template. But i think if i put some sort of protection against blank posts going in, it will solve both problems. Or to kill two birds with one stone, how do i make a "cannot leave blank" field?
My code below
Code: Select all
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="news" id="news">
<table width="744" border="1" align="center" cellpadding="1" cellspacing="1" class="boxtext">
<tr>
<th width="122" bgcolor="#333333" scope="col">Title</th>
<th width="609" bgcolor="#333333" scope="col"><input name="title" type="text" id="title" size="30" maxlength="30" /></th>
</tr>
<tr>
<td bgcolor="#333333">News Intro</td>
<td bgcolor="#333333"><textarea name="description" id="description" cols="60" rows="5"></textarea></td>
</tr>
<tr>
<td bgcolor="#333333">Full News</td>
<td bgcolor="#333333"><textarea name="fullstory" id="fullstory" cols="60" rows="10"></textarea></td>
</tr>
<tr>
<td bgcolor="#333333">Author</td>
<td bgcolor="#333333"><input name="author" type="text" id="author" size="25" maxlength="15" /></td>
</tr>
<tr>
<td bgcolor="#333333"> </td>
<td bgcolor="#333333"><input type="submit" name="submit" id="submit" value="Submit" onClick='document.location("index.php");'/></td>
</tr>
</table>
</form>
<p>
<?php
// connect to database
$db=mysql_connect("localhost","news","password") or die ("cant connect");
mysql_select_db("news",$db) or die ("cant change");
$news=mysql_query("SELECT * FROM News ORDER BY date DESC LIMIT 3") or die ("cant get em");
?>
<?
session_start();
$secret=md5(uniqid(rand(), true));
$_SESSION['FORM_SECRET']=$secret;
?>
<?php
// _POST Query for sending entered info to database
if (isset($_POST['title']))
$title = $_POST['title'];
$description = $_POST['description'];
$fullstory = $_POST['fullstory'];
$author = $_POST['author'];
$date = $_POST['TIMESTAMP'];
$sql = "INSERT INTO News SET
title='$title',
description='$description',
fullstory='$fullstory',
author='$author'";
if (@mysql_query($sql)) {
echo 'Your post has been saved';
} else {
echo '<p>Sorry your post has not worked' .
mysql_error() . '</p>';
}
?>