PDA

View Full Version : Contact form issue



anothermethod
October 14th, 2007, 02:25 AM
Hello - I've been working on this contact form that writes the users info to db. Everything works great but I'm having trouble with displaying a confirmation message after the user submits their information. Please help if you can!!

Thanks,

AM,

Here is the code I'm using

---------------------------------------------------------------------------------------------------

<?php
include 'includes/config.php';
include 'includes/opendb.php';

mysql_select_db('newsletter2') or die('Cannot select database');

$query = 'CREATE TABLE guestbook ( '.
'id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, '.
'name VARCHAR(30) NOT NULL, '.
'email VARCHAR(50) NOT NULL, '.
'entry_date DATE NOT NULL, '.
'PRIMARY KEY(id))';

$result = mysql_query($query);

// check if the form is submitted
if(isset($_POST['btnSign']))
{
// get the input from $_POST variable
// trim all input to remove extra spaces
$name = trim($_POST['Name']);
$email = trim($_POST['Email']);

// escape the message ( if it's not already escaped )
if(!get_magic_quotes_gpc())
{
$name = addslashes($name);
}

// if the visitor do not enter the url
// set $url to an empty string
if ($url == 'http://')
{
$url = '';
}

// prepare the query string
$query = "INSERT INTO guestbook (name, email, entry_date) " .
"VALUES ('$name', '$email', 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;
}
?>
<html>
<head>
<title>Guestbook</title>
<script language="JavaScript" src="gen_validatorv2.js" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="styles/styles.css">
</head>
<body>
<form method="post" name="guestform">
<table width="550" border="0" cellpadding="2" cellspacing="1">
<tr>
<td width="100">Name *</td> <td>
<input name="Name" type="text" id="txtName" size="30" maxlength="30"></td>
</tr>
<tr>
<td width="100">Email</td>
<td>
<input name="Email" type="text" id="txtEmail" size="30" maxlength="50"></td>
</tr>
<tr>
<td width="100">&nbsp;</td>
<td>
<input name="btnSign" type="submit" id="btnSign" value="Sign Guestbook" onClick="return checkForm();"></td>
</tr>
</table>
</form>
<script language="JavaScript" type="text/javascript">
//You should create the validator only after the definition of the HTML form
var frmvalidator = new Validator("guestform");
frmvalidator.addValidation("Name","req","Please enter your Name");
frmvalidator.addValidation("Name","maxlen=20",
"Max length for Name is 20");
frmvalidator.addValidation("Name","alpha");

frmvalidator.addValidation("Email","maxlen=50");
frmvalidator.addValidation("Email","req");
frmvalidator.addValidation("Email","email");


</script>
<br>
<br>

</body>
</html>

-------------------------------------------------------------------------------------

prc
October 14th, 2007, 02:59 AM
you could replace this line:



header('Location: ' . $_SERVER['REQUEST_URI']);

with this:


header('Location: ' . $_SERVER['REQUEST_URI'].'?success');


and add in:


</head>
<body>
<?
if(isset($_get['success'])){
echo "<h3>Successfully Posted!</h3>";
}
?>
<form method="post" name="guestform">
<table width="550" border="0" cellpadding="2" cellspacing="1">
<tr>

anothermethod
October 14th, 2007, 03:13 PM
Thanks PRC - But the echo doesn't post the success message...:puzzle:

joran420
October 15th, 2007, 12:47 AM
just redirrect to a thank you page

header("location:confirmatiion.html")

then just make confirmation.html a thank you page?

prc
October 15th, 2007, 02:52 AM
you could replace this line:




</head>
<body>
<?
if(isset($_get['success'])){
echo "<h3>Successfully Posted!</h3>";
}
?>
<form method="post" name="guestform">
<table width="550" border="0" cellpadding="2" cellspacing="1">
<tr>


Try that! ($_GET has to be uppercase - Oops..)



</head>
<body>
<?
if(isset($_GET['success'])){
echo "<h3>Successfully Posted!</h3>";
}
?>
<form method="post" name="guestform">
<table width="550" border="0" cellpadding="2" cellspacing="1">
<tr>

anothermethod
October 15th, 2007, 10:09 PM
works perfectly now!!! Thanks PRC:beer: