newbie question about php and html forms

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
fitzov
Forum Newbie
Posts: 3
Joined: Sat Nov 08, 2003 2:40 pm

newbie question about php and html forms

Post 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!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
fitzov
Forum Newbie
Posts: 3
Joined: Sat Nov 08, 2003 2:40 pm

ok

Post 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?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

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

thats a server redirect ;)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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>
fitzov
Forum Newbie
Posts: 3
Joined: Sat Nov 08, 2003 2:40 pm

reload form

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
Post Reply