Click to See Complete Forum and Search --> : How do I put required fields into this script?


SpikeWeb
04-19-2006, 02:17 PM
Hi all,
I'm making a guestbook for my site in PHP. I have the script working, but now I'd like to make some of the fields required. What might be the easiest way to do this. I tried putting the following line into the form, but that didn't seem to work. I got this from this link (http://www.clickfire.com/viewpoints/articles/webdevelopment/create_php_form.php) , however I'm not sure I implemented it correctly.
<input type=hidden name="required" value="name,address,city,state,zip">Below is my PHP code. When the form is submitted, the data goes into a MySQL DB. I did have one other question and that is, if I have these required fields in the DB instead of the page, this wouldn't really work since the form could still be submitted without being checked on the client-side, correct?
I think I need to make the required fields in the DB the same as the ones in the form, but I thought I'd ask anyway.
<?php
if (!isset($_POST['submit'])) {
?>

<!--HTML Form begins here-->

<?php
}
else {
$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$find = addslashes($_POST['find']);
$question = addslashes($_POST['question']);

mysql_query ("INSERT INTO `guestbook` (name, address, city, state, country, zip, phone, email, find, question)
VALUES ('$name', '$address', '$city', '$state', '$zip', '$country', '$phone', '$email', '$find', '$question')");
?>
<div align="center">
<span class="text12ptbold">

<?php
echo "Thank you! You have successfully been added to our guestbook!";
}
?>

Any insight is appreciated, thanks!

Sheldon
04-19-2006, 02:31 PM
just use php to check if there empty or not

<?php
if (!isset($_POST['submit'])) {

//echo an error if there is one
if(isset($_GET['msg']))
{
echo($_GET['msg']);
}
//end error message
?>

<!--HTML Form begins here-->

<?php
}
else {
//check if the required fields are empty
if(empty($_POST['name']) ||
empty($_POST['address']) ||
empty($_POST['city']) ||
empty($_POST['state']) ||
empty($_POST['zip']))
{
header("Location: ". $_SERVER['PHP_SELF'] . "?msg=you must enter all required fields");

}
else
{
//else send the mail

$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$find = addslashes($_POST['find']);
$question = addslashes($_POST['question']);

mysql_query ("INSERT INTO `guestbook` (name, address, city, state, country, zip, phone, email, find, question)
VALUES ('$name', '$address', '$city', '$state', '$zip', '$country', '$phone', '$email', '$find', '$question')");
?>
<div align="center">
<span class="text12ptbold">

<?php
echo "Thank you! You have successfully been added to our guestbook!";
}

//close the if / else i started
}
?>

SpikeWeb
04-19-2006, 03:45 PM
Thanks Sheldon, that did the trick! I submitted a test entry and it went into the DB correctly.
One thing though...I'm not quite sure I understand how this line works:
header("Location: ". $_SERVER['PHP_SELF'] . "?msg=you must enter all required fields");And when I left a field blank to test it, I did get this error:
PHP Warning: Cannot modify header information - headers already sent by (output started at e:\...page address... on line 266.
I don't know if it was trying to read my drive or what, but line 266 is the one above. Any ideas?
Thanks,
Spike

Sheldon
04-19-2006, 07:53 PM
Ah, well a header(); can not be set before any output. On the script you posted there was nothing before the header was output.
If you post the whole script i will rearrange it for you.

Else try yourself, examples of what i mean are here http://php.net/header