Jump to content


Contact form help


4 replies to this topic

#1 Matt L

    Young Padawan

  • Members
  • Pip
  • 272 posts
  • Gender:Male
  • Location:Newcastle

Posted 18 August 2006 - 12:26 PM

Okay, basically the thank you text is showing up before the email is sent. Any idea how I can fix this?
contact.php
<form action="<?php $_SERVER['PHP_SELF']; ?>">
<table width="100%" class="noborder" cellpadding="0" cellspacing="1">
  <tr>
	<td>Name</td>
	<td><input type="text" name="name" /></td>
  </tr>
  <tr>
	<td>Email</td>
	<td><input type="text" name="email" /></td>
  </tr>
  <tr>
	<td>Suject</td>
	<td><select name="subject">
	  <option>Quote on a template</option>
	  <option>Quote on a script</option>
	  <option>Other</option>
	</select></td>
  </tr>
  <tr>
	<td>Message:</td>
	<td><textarea name="message" cols="40" rows="5"></textarea></td>
  </tr>
  <tr>
	<td colspan="2" style="text-align:center; "><input type="submit" name="submit" value="Send" /></td>
  </tr>
<?php
$youremail = "matt@matt-license.com";
$subject = $_POST['subject'];
$thankyou = "Thank you. Your Email has been sent; I'll try to respond ASAP.";
$submit = $_POST['submit'];

if ( $_POST['submit'] ) {
	if( $email == "" ) {
		echo("<tr><td colspan='2' style='text-align:text-align:center; '>No email address added. Please go back.<br/></td></tr>");
		}
	elseif( $name == "" ) {
		echo("<tr><td colspan='2' style='text-align:text-align:center; '>No name added. Please go back.<br/></td></tr>");
		}
	elseif( $message == "" ) {
		echo("<tr><td colspan='2' style='text-align:text-align:center; '>No message added. Please go back.<br/></td></tr>");
		}
	}
else {
		$msg = ereg_replace("\\\'", "'", $message);
		$msg = ereg_replace('\\\"', "\"", $msg);
		$message1 = "from: $name<br />email: $email<br />message:<br />$msg1";

		mail($youremail, $subject, $msg, "From: $email\r\nReply-to: $email\r\n");
		echo("<tr><td colspan='2' style='text-align:text-align:center; '>$thankyou</td></tr>");
		}
?>
</table>
</form>


#2 NGPixel

    Senior Programmer

  • P2L Staff
  • PipPipPipPip
  • 1,410 posts
  • Gender:Male
  • Location:Montreal, Canada
  • Interests:Web Design : Coding : Animation

Posted 18 August 2006 - 12:53 PM

I don't see anything wrong with your script (where's the problem?).
But normally you process the contact form before displaying it.

#3 Matt L

    Young Padawan

  • Members
  • Pip
  • 272 posts
  • Gender:Male
  • Location:Newcastle

Posted 18 August 2006 - 12:56 PM

This is what it's doing

#4 Matt L

    Young Padawan

  • Members
  • Pip
  • 272 posts
  • Gender:Male
  • Location:Newcastle

Posted 18 August 2006 - 01:53 PM

Okay, I've changed the script.
<form action="<?php $_SERVER['PHP_SELF']; ?>">
<table width="100%" class="noborder" cellpadding="0" cellspacing="1">
  <tr>
	<td>Name</td>
	<td><input type="text" name="name" /></td>
  </tr>
  <tr>
	<td>Email</td>
	<td><input type="text" name="email" /></td>
  </tr>
  <tr>
	<td>Suject</td>
	<td><select name="subject">
	  <option>Quote on a template</option>
	  <option>Quote on a script</option>
	  <option>Other</option>
	</select></td>
  </tr>
  <tr>
	<td>Message:</td>
	<td><textarea name="message" cols="40" rows="5"></textarea></td>
  </tr>
  <tr>
	<td colspan="2" style="text-align:center; "><input type="submit" name="submit" value="Send" /></td>
  </tr>
<?php
$youremail = "matt@matt-license.com";
$subject = $_POST['subject'];
$thankyou = "Thank you. Your Email has been sent; I'll try to respond ASAP.";

while($_POST['submit']) {
	if( $email == "" ) {
		echo("<tr><td colspan='2' style='text-align:text-align:center; '>No email address added. Please go back.<br/></td></tr>");
		}
	elseif($name == "") {
		echo("<tr><td colspan='2' style='text-align:text-align:center; '>No name added. Please go back.<br/></td></tr>");
		}
	elseif($message == "") {
		echo("<tr><td colspan='2' style='text-align:text-align:center; '>No message added. Please go back.<br/></td></tr>");
		}
	else {
		$msg = ereg_replace("\\\'", "'", $message);
		$msg = ereg_replace('\\\"', "\"", $msg);
		$message1 = "from: $name<br />email: $email<br />message:<br />$msg1";

		mail($youremail, $subject, $msg, "From: $email\r\nReply-to: $email\r\n");
		echo("<tr><td colspan='2' style='text-align:text-align:center; '>$thankyou</td></tr>");
		}
	}
?>
</table>
</form>
It's got rid of the text showing when it shouldn't but it's not sending the email.

#5 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 18 August 2006 - 04:28 PM

Your code is completely wierd, lol. Your form and processing parts should be seperate. I also do not get why you would put your processing bit into a while loop. ^_^
And your action isn't there because you have to echo the PHP_SELF, not just put the variable there. And you need to tell the form how to process is, be it GET or POST data.
This should work.
<?php
if(!$_POST){
?>
<table width="100%" class="noborder" cellpadding="0" cellspacing="1">
<form action="<?php  echo $_SERVER['PHP_SELF']; ?>" method="post">
  <tr>
	<td>Name</td>
	<td><input type="text" name="name" /></td>
  </tr>
  <tr>
	<td>Email</td>
	<td><input type="text" name="email" /></td>
  </tr>
  <tr>
	<td>Subject</td>
	<td><select name="subject">
	  <option>Quote on a template</option>
	  <option>Quote on a script</option>
	  <option>Other</option>
	</select></td>
  </tr>
  <tr>
	<td>Message:</td>
	<td><textarea name="message" cols="40" rows="5"></textarea></td>
  </tr>
  <tr>
	<td colspan="2" style="text-align:center; "><input type="submit" name="submit" value="Send" /></td>
  </tr>
</form>
<?php
}
else{
$youremail = 'matt@matt-license.com';
$subject = $_POST['subject'];
$thankyou = 'Thank you. Your Email has been sent; I'll try to respond ASAP.';
$email = trim($_POST['email']);
$name = trim($_POST['name']);
$message = $_POST['message'];

	if(empty($email)) {
		echo '<tr><td colspan="2" style="text-align:center;">No email address submitted. Please go back.<br/></td></tr>';
		}
	elseif(empty($name)) {
		echo '<tr><td colspan="2" style="text-align:center;">No name submitted. Please go back.<br/></td></tr>';
		}
	elseif(empty($message)) {
		echo '<tr><td colspan="2" style="text-align:center;">No message submitted. Please go back.<br/></td></tr>';
		}
	else {
		$msg = ereg_replace("\\\'", "'", $message);
		$msg = ereg_replace('\\\"', "\"", $msg);
		$message1 = "from: $name<br />email: $email<br />message:<br />$msg";

		mail($youremail, $subject, $msg, "From: $email\r\nReply-to: $email\r\n");
		echo '<tr><td colspan="2" style="text-align:center;">'.$thankyou.'</td></tr>';
		}
	}
?>
</table>

I also cleaned some of your code up, including XHTML validating your table rows and such from the code.
As a note, you can't always trust the globals is turned on, so you always should retrieve your variables from the $_POST or $_GET arrays rather than assuming they are automatically thrown into the corresponding variables. :beat:





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users