Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
i can't get this form work....please help...
<?php
// This is an example of config.php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'admin';
$dbname = 'demo';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head><body>
<?php
// an example of closedb.php
// it does nothing but closing
// a mysql database connectionif (isset($_POST['submitted'])) {
<form action="$_SERVER[PHP_SELF]" method="post">
<table>
<tr>
<td width="190">First Name *
<input type="text" name="fname" size="21"></td>
<td width="*">Last Name *
<input type="text" name="lname" size="21"></td>
<td width="*">
<input name="submit" type="button" value="Submit Your Information "></td>
</tr>
</table>
</form>
} else {
// Display it.
}
// This is an example opendb.php
// prepare the query string
$query = "INSERT INTO name (fname, lname) " .
"VALUES ('', now(), now(), '$_POST[fname], '$_POST[Lname]')";
// execute the query to insert the input to database
// if query fail the script will terminate
mysql_query($query) or die('Error, query failed. ' . mysql_error());
// redirect to current page so if we click the refresh button
// the form won't be resubmitted ( as that would make duplicate entries )
header('Location: ' . $_SERVER['REQUEST_URI']);
// force to quite the script. if we don't call exit the script may
// continue before the page is redirected
exit;mysql_close($conn);
?>
</body>
</html>

I have this form but the Validation doesn't work....please help...
<?php
include ("config.php");
include ("connectdb.php");
?><?php
if(!isset($_POST['submit']))
{
$error_msg='';
if(trim($fname)=='' ){
$error_msg.="Please enter Your First Name
";
}
if(trim($lname)=='') {
$error_msg.="Please enter Your Last Name
";
}
if(trim($dphone)=='' ){
$error_msg.="Please enter Your Day Time Phone
";
}
if(trim($ephone)=='') {
$error_msg.="Please enter Your Evening Phone
";
}
if(trim($email)=='') {
$error_msg.="Please enter an Email
";
} else {
// check if email is a valid address in this format username@domain.com
if(!ereg("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]", $email)) $error_msg.="Please enter a valid email address
";
}
if(trim($address)=='' ){
$error_msg.="Please enter Your Address
";
}
if(trim($city)=='') {
$error_msg.="Please enter Your City
";
}
if(trim($county)=='' ){
$error_msg.="Please enter Your County
";
}
if(trim($state)=='') {
$error_msg.="Please enter Your State
";
}
if(trim($zip)=='') {
$error_msg.="Please enter Your Zip Code
";
}
// display error message if any, if not, proceed to other processing
if($error_msg==''){
// other process here
} else {
echo "$error_msg";
}
}
?><form action="" method="post">
<table width="420" align="center" border="0" cellpadding="0" cellspacing="0" class="HomeInfoForm">
<tr>
<td colspan="2" style="padding:8px 0px 3px 0px;font-size:14px;font-weight:bold">Your
Contact Information</td>
</tr>
<tr>
<td width="190">First Name *
<input type="text" name="fname" size="21" value="<? echo $fname; ?>"></td>
<td width="*">Last Name *
<input type="text" name="lname" size="21" value="<? echo $fname; ?>"></td>
</tr>
<tr>
<td>Day Time Phone *
<input type="text" name="dphone" size="21" value="<? echo $dphone; ?>"></td>
<td>Evening Phone
<input type="text" name="ephone" size="21" value="<? echo $ephone; ?>"></td>
</tr>
<tr>
<td colspan="2">Email*
<input type="text" name="email" size="45" value="<? echo $email; ?>"></td>
</tr>
</table>
<table width="419" align="center" border="0" cellpadding="0" cellspacing="0" class="HomeInfoForm">
<tr>
<td colspan="4">Address *
<input name="address" type="text" size="45" value="<? echo $address; ?>"></td>
</tr>
<tr>
<td>City *
<input type="text" name="city" size="17" value="<? echo $city; ?>"></td>
<td>County *
<input name="county" type="text" size="13" maxlength="50" value="<? echo $county; ?>"></td>
<td>State *
<input type=text name=state size="2" maxlength="2" value="<? echo $state; ?>"></td>
<td>ZIP *
<input name="zip" type="text" size="5" maxlength="5" value="<? echo $zip; ?>">
</td>
</tr>
</table>
<div style="text-align:center;padding:13px 0px 13px 0px">
<input name="submit" type="submit" value="Submit Your Information ">
</div>
</form>
<?php
// }else {
// get the input from $_POST variable
// trim all input to remove extra spaces
$fname = trim($_POST['fname']);
$lname = trim($_POST['lname']);
$dphone = trim($_POST['dphone']);
$ephone = trim($_POST['ephone']);
$email = trim($_POST['email']);
$address = trim($_POST['address']);
$city = trim($_POST['city']);
$county = trim($_POST['county']);
$state = trim($_POST['state']);
$zip = trim($_POST['zip']);
// escape the message ( if it's not already escaped )
if(!get_magic_quotes_gpc())
{
$name = addslashes($name);
$message = addslashes($message);
}
// prepare the query string
$query = "INSERT INTO foreclose (fname, lname, dphone, ephone, email, address, city, county, state, zip, entry_date) " .
"VALUES ('$fname', '$lname', '$dphone', '$ephone', '$email', '$address', '$city', '$county', '$state', '$zip', current_date)";// execute the query to insert the input to database
// if query fail the script will terminate
mysql_query($query) or die('Error, query failed. ' . mysql_error());
// redirect to current page so if we click the refresh button
// the form won't be resubmitted ( as that would make duplicate entries )
//header('Location: ' . $_SERVER['REQUEST_URI']);
// force to quite the script. if we don't call exit the script may
// continue before the page is redirected
exit;
//}
?>

And, the errors you get are? What, exactly, IS happening? What have you tried so far? It's really hard to help you when you don't explain the problem.
Michael J

I think to problem is validation code doesn't work because when i hit submit button the information add to the database doesn't matter when the form is blank or filled in. It even add blank record into database table when i tried to refresh.
The form purpose is to notify user not to leave any fields blank, and have to fill in the right format.

The problem is that you have your logic to determine wether any info is missing or incorrect THEN you go ahead and insert into the database anyway!
Your logic should go something like this:
if(!isset($_POST['submit']))
{
Logic to determine if fields are correct
if($error_msg==''){
Insert record into database
} else {
echo "$error_msg";
}} else {
display form
}

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |