Page 1 of 1

newbie question about php and html forms

Posted: Sat Nov 08, 2003 2:40 pm
by fitzov
Here's the php code for the form info:

Code: Select all

<?php
// open the connection
$conn = mysql_connect("localhost", "mysql", "blahblah");
// pick the database
mysql_select_db("sellers",$conn);
// create the SQL statement
$sql = "INSERT INTO clients values ('', '$_POST[coname]', '$_POST[costreet]', 
'$_POST[cocity]', '$_POST[costate]', '$_POST[cozip]')";
// execute the SQL statement
if (mysql_query($sql, $conn)) {
	header("Location: http://localhost/insert_form2.html");
} else {
	echo "something went wrong!";
}
?>
[mod_edit:

Code: Select all

tag added][/size]

My question is this: is there a more elegant/better way to redirect the user to a clean form other than using

'header("Location: http://localhost/insert_form2.html");'

(of course, 'localhost' will be something else in the future)??

Thanks in advance!

Posted: Sat Nov 08, 2003 3:17 pm
by twigletmac
A server-side redirect is fairly elegant, user knows little about it and they then can't refresh the screen 16 times and insert the same thing over and over.

Mac

ok

Posted: Sat Nov 08, 2003 4:11 pm
by fitzov
thanks. I'm new to this, so could you could give me a little hint on how to do that? maybe something like a '$_SERVER['PHP_SELF']' somewhere?

Posted: Sat Nov 08, 2003 4:48 pm
by d3ad1ysp0rk
header("Location: http://localhost/insert_form2.html");

thats a server redirect ;)

Posted: Mon Nov 10, 2003 5:20 am
by JAM
How about directly in the <form> tag?

Code: Select all

<form method="post" action="<?php echo $_SERVER&#1111;'PHP_SELF']; ?>">
 <input type="submit" value="Click me" />
</form>
or

Code: Select all

<form method="post" action="<?php echo basename(__FILE__); ?>">
<input type="submit" value="Click me" />
</form>

reload form

Posted: Mon Nov 10, 2003 4:51 pm
by fitzov
Ok. It's working as it is, but I will try Jam's way and let you know. Is there a difference with the number of hits? I don't know if 'hits' is the right word, but whatever connotes 'new connections to the server'? I would like to do it some way that the same user doesn't get counted again.

Thanks again.

Posted: Mon Nov 10, 2003 6:08 pm
by JAM
"I would like to do it some way that the same user doesn't get counted again. "

I'm having a hard time understanding that with the original post, so I'll mention two solutions...

a) Actually counting users, as in pagevisitor counter; You can use $_COOKIES. If cookie is not set = set it, and add visitor count to the database. If set, user is counted.

b) Actually blocking the insert in the original post; Add an additional query to the database that contains "select coname from clients where coname = '$_POST[coname]'". Use mysql_num_rows() (or similiar) to get the number of results. If it is > 0, there is allready an entry with that data. If not, well... Insert it.

Hope it helped.