jpmad4it
03-12-2008, 07:35 PM
Hi there,
I've been using this form for a while now, without problems. However, i'm using it on a new site and its not working. The idea is that the form posts to itself, and then the data is checked. If the form fields are not filled out or the email address is not valid when the form is submitted, then an error message is displayed and the field is highlighted with three red stars ***. The form fields retain their entries if there is an error. The problem is that the even though I don't fill any form fields in, there are no error messages displayed......the page just refreshes over and over.
I have narrowed down my code just to two form fields, for testing purposes, but still it doesn't work.
I think it might be the functions that are the problem, as they are quite old. Can someone please advise?
Here is the included file data_valid.php which checks if the form fields are filled, and if the email address is valid:
<?php
function filled_out($form_vars)
{
// test that each variable has a value
foreach ($form_vars as $key => $value)
{
if (!isset($key) || ($value == ""))
return false;
}
return true;
}
function valid_email($address)
{
// check an email address is possibly valid
if (ereg("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $address))
return true;
else
return false;
}
?>
And here is the php page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
</head>
<body>
<div id="middle">
<?php
include("inc/data_valid.php")
//allows form fields to be remebered when user hits sumbit
if(isset($_POST['submit'])){
$field1 = $_POST['name'];
$field2 = $_POST['email'];
//if form is submitted set fieldHidden variable to the value of the form field submit_check
//if form has been submitted then submit_check will have value 'set'
$fieldHidden = $_POST['submit_check'];
} else {
$field1 = '';
$fieldHidden = '';
}
//checks to see if image verification was valid, checks if email was valid and if all form
// fields were filled in. If so, form was valid and we enter data to database and send confirmation email.
if (valid_email($email) && filled_out($HTTP_POST_VARS) && isset($_POST["savereview"])){
echo "Thank you, your online booking was taken and we will contact you shortly";
//SQL to insert database entries will follow here.............
}
//if form has been submitted and there were one or more errors, print message to inform user
if($fieldHidden == 'set'){
if (!valid_email($email) || !filled_out($HTTP_POST_VARS)){
echo "<font color='red'>There are errors in your form please correct them and submit the form again</font>";
}
}
?>
<form method="post" action="">
<table class="border" cellpadding="3" bgcolor="#000000" width="100%" >
<tbody>
<tr>
<td valign="top" colspan="3">
<!--Set hidden field which allows to check if form has already been submitted-->
<input type="hidden" name="submit_check" value="set" />
</td>
</tr>
<tr>
<td width="242" height="25px">Your name:</td>
<td width="194"><input name="name" id="name" size="30" value="<? echo $field1; ?>" type="text" style="width:95%;" /></td>
<td width="11%">
<?php
if($fieldHidden == 'set'){
if ($_POST['name'] == ''){
echo "<font color='red'> *** </font>";
}
}
?>
</td>
</tr>
<tr>
<td width="242" height="25px">Your email:</td>
<td width="194"><input name="email" id="email" size="30" value="<? echo $field2; ?>" type="text" style="width:95%;" /></td>
<td width="11%">
<?php
if($fieldHidden == 'set'){
if ($_POST['email'] == ''){
echo "<font color='red'> *** </font>";
}
}
?>
</td>
</tr>
<tr>
<td colspan="3" style="text-align: center;"><input name="savereview" type="submit" class="button" value="Submit" /></td>
</tr>
</tbody>
</table>
</form>
</div>
</body>
</html>
Any help would be greatly appreciated.
Jp
I've been using this form for a while now, without problems. However, i'm using it on a new site and its not working. The idea is that the form posts to itself, and then the data is checked. If the form fields are not filled out or the email address is not valid when the form is submitted, then an error message is displayed and the field is highlighted with three red stars ***. The form fields retain their entries if there is an error. The problem is that the even though I don't fill any form fields in, there are no error messages displayed......the page just refreshes over and over.
I have narrowed down my code just to two form fields, for testing purposes, but still it doesn't work.
I think it might be the functions that are the problem, as they are quite old. Can someone please advise?
Here is the included file data_valid.php which checks if the form fields are filled, and if the email address is valid:
<?php
function filled_out($form_vars)
{
// test that each variable has a value
foreach ($form_vars as $key => $value)
{
if (!isset($key) || ($value == ""))
return false;
}
return true;
}
function valid_email($address)
{
// check an email address is possibly valid
if (ereg("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $address))
return true;
else
return false;
}
?>
And here is the php page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
</head>
<body>
<div id="middle">
<?php
include("inc/data_valid.php")
//allows form fields to be remebered when user hits sumbit
if(isset($_POST['submit'])){
$field1 = $_POST['name'];
$field2 = $_POST['email'];
//if form is submitted set fieldHidden variable to the value of the form field submit_check
//if form has been submitted then submit_check will have value 'set'
$fieldHidden = $_POST['submit_check'];
} else {
$field1 = '';
$fieldHidden = '';
}
//checks to see if image verification was valid, checks if email was valid and if all form
// fields were filled in. If so, form was valid and we enter data to database and send confirmation email.
if (valid_email($email) && filled_out($HTTP_POST_VARS) && isset($_POST["savereview"])){
echo "Thank you, your online booking was taken and we will contact you shortly";
//SQL to insert database entries will follow here.............
}
//if form has been submitted and there were one or more errors, print message to inform user
if($fieldHidden == 'set'){
if (!valid_email($email) || !filled_out($HTTP_POST_VARS)){
echo "<font color='red'>There are errors in your form please correct them and submit the form again</font>";
}
}
?>
<form method="post" action="">
<table class="border" cellpadding="3" bgcolor="#000000" width="100%" >
<tbody>
<tr>
<td valign="top" colspan="3">
<!--Set hidden field which allows to check if form has already been submitted-->
<input type="hidden" name="submit_check" value="set" />
</td>
</tr>
<tr>
<td width="242" height="25px">Your name:</td>
<td width="194"><input name="name" id="name" size="30" value="<? echo $field1; ?>" type="text" style="width:95%;" /></td>
<td width="11%">
<?php
if($fieldHidden == 'set'){
if ($_POST['name'] == ''){
echo "<font color='red'> *** </font>";
}
}
?>
</td>
</tr>
<tr>
<td width="242" height="25px">Your email:</td>
<td width="194"><input name="email" id="email" size="30" value="<? echo $field2; ?>" type="text" style="width:95%;" /></td>
<td width="11%">
<?php
if($fieldHidden == 'set'){
if ($_POST['email'] == ''){
echo "<font color='red'> *** </font>";
}
}
?>
</td>
</tr>
<tr>
<td colspan="3" style="text-align: center;"><input name="savereview" type="submit" class="button" value="Submit" /></td>
</tr>
</tbody>
</table>
</form>
</div>
</body>
</html>
Any help would be greatly appreciated.
Jp