how to get my contact form to do what i want it to!

bentzak

Senior Member
As you may have noticed from the amount of postsi have put up recently, im building my first website, and am learning! well, i have another question if anyone fancies helping me some more,

this is the code for my email form, it all looks ok, syle wise on my page, but when i try and use it on my test server it doesnt really work. after i click submit it goes to a new page that says "no subject" and "click here" which takes you back, two things, why does it say no subject? is it not sending? and also how do i get it so that it displays any messages on the same page??

here is the code contact page php,

PHP:
<?php
 if($content)
 {
  $from = $_POST["from"]; 
  $email = $_POST["email"]; 
  $content = "This message is from $from whose email address is $email.\r\n-----------\r\n ";  
  $content = $content . $_POST["content"]; 
  $content = wordwrap($content, 70); 
  mail('[email protected]', 'website inquiry', $content); 
  echo "Your message has been sent, and I will reply within 48hrs. Thank you."; 
 }
 ?>
 <form method="post" action="mail.php">
  <p>Name:</p>
  <p> 
    <input type="text" name="from" title="Your name">
  </p>
  <p>Email:</p>
  <p> 
    <input type="text" name="email">
  </p>
  <p>Message:</p>
  <p> 
    <textarea name="content" cols="50" rows="20"></textarea>
  </p>
<p><?php  require_once('recaptchalib.php');
$publickey = "..."; 
echo recaptcha_get_html($publickey);
?></p>
  <p><input type="submit" value="Submit"></p>
 </form>
think thats all the code?

Cheers, in advance!

Zak
 
more code, in the mail.php, figured out the subject bit, cos i had

$subject instea of $from

PHP:
<?php

$email = $HTTP_POST_VARS['email'];
$from = $HTTP_POST_VARS['from'];
$message = $HTTP_POST_VARS['message'];

if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
  echo "<h4>Invalid email address</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($from == "") {
  echo "<h4>No subject</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
}


elseif (mail($email,$from,$message)) {
  echo "<h4>Thank you for sending email</h4>";
} else {
  echo "<h4>Can't send email to $email</h4>";
}
require_once('recaptchalib.php');
$privatekey = "... ";
$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
  die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
       "(reCAPTCHA said: " . $resp->error . ")");
}

?>

still if anyone could help me so that thie thnkyou message apears on the same page?

Z
 
I take it the second php section is mail.php?
Try changing this ~
PHP:
$email = $HTTP_POST_VARS['email'];
$from = $HTTP_POST_VARS['from'];
$message = $HTTP_POST_VARS['message'];
To ~
PHP:
$email = $_POST['email'];
$from = $_POST['from'];
$message = $_POST['message'];
 
no luck, i keep trying different codes, but i can get one to work! trying to do it with recaptcha to make i spam free, gotta keep on keeping on i guess,

Cheers tho,

Z
 
Well the reason you are getting no subject and the back button is because the $from variable is empty.
PHP:
elseif ($from == "") {
  echo "<h4>No subject</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
}
Now I'm pretty sure that wont send an email because to send an email $content must be set, now when they fill in the form, I take it the 2 code snippets you gave are 2 different files, they go 2 the second file and bypass the $content test entirely.

Where $from then needs to be set from the $_POST['from'] variable, it will then equal not empty and the code will carry on executing to the success if. Does that make sense?
 
not gonna lie, that makes no sense to me! it is two files tho. im pretty new to coding, but i am determined to get a ruddy contact form together, a secure one! so if its not too much hassle, i dont wanna take up to much of anyones time, i have tried another form, this ones nearly working, but there is two lines i think either i need to change, or understand what they mean, which could be a long shot, here goes,

PHP:
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

basically i want to change it so that it doesnt go to the pages above, it goes to my own versions of those pages. the contactthanks.php is my own one, but it doesnt go to it, it goes to the php engine of my form, which just shows up blank. and the error.htm, i cant find that anywhere. if you need to see any more code let me know.

Cheers in advance again! i dunno what i would do with this forum!

Z
 
Right I've just written this off the cuff so if you get error let me know. But this is a lot more user friendly than your first version, and I've coded the form for disabled users.

If you get an error let me know and I'll run it locally and put up any bug fixes. Looks fine to me.
Bare in mind if you are running it locally and you haven't sent up your local server to send emails you will get a mail error.

If you get one post me the exact error.
Jaz

<?php
if(isset($_POST['name']))
{
$count=0;
$err="Please fix the errors below to be able to contact me: <br />";
if(!empty($_POST["name"])){
$from = $_POST["name"];
}else{
$count=$count+1;
$err=$err."Please enter your name<br />";
} if(!empty($_POST["email"])){
$email = $_POST["email"];
}else{
$count=$count+1;
$err=$err."You need to enter an email address so I can contact you<br />";
} if(!empty($_POST['mess'])){

$content = "This message is from $from whose email address is $email.\r\n-----------\r\n ";
$content = $content . $_POST["mess"];
$content = wordwrap($content, 70);
}else{
$count=$count+1;
$err=$err."Your message was blank";
}
if($count==0){

mail('[email protected]', 'website inquiry', $content);
echo "Your message has been sent, and I will reply within 48hrs. Thank you.";
}else{?>
<h2><?php if($count>1){echo$count?> errors occured<?php }else{?>1 error occured<?php }?></h2>
<p>
<?php echo $err;?>
</p>

<form method="post" action="Put this files name in here as we want to loop back to the top of the file when the user clicks the button">
<p><label for="n">Name:</label></p>
<p>
<input type="text" name="name" id="n"<?php if(!empty($_POST["name"])){?> value="<?php echo $_POST['name']?>"<?php }?> />
</p>
<p><label for="e">Email:</label></p>
<p>
<input type="text" name="email" id="e"<?php if(!empty($_POST["email"])){?> value="<?php echo $_POST['email']?>"<?php }?> />
</p>
<p><label for="m">Message:</label></p>
<p>
<textarea name="mess" cols="50" rows="20" id="m"><?php if(!empty($_POST["message"])){?><?php echo $_POST['email'];}?></textarea>
</p>
<p><?php require_once('recaptchalib.php');
$publickey = "...";
echo recaptcha_get_html($publickey);
?></p>
<p><button type="submit">Contact me</button></p>
</form>
<?php }
}else{
?>
<form method="post" action="Put this files name in here as we want to loop back to the top of the file when the user clicks the button">
<p><label for="n">Name:</label></p>
<p>
<input type="text" name="name" id="n" />
</p>
<p><label for="e">Email:</label></p>
<p>
<input type="text" name="email" id="e" />
</p>
<p><label for="m">Message:</label></p>
<p>
<textarea name="mess" cols="50" rows="20" id="m"></textarea>
</p>
<p><?php require_once('recaptchalib.php');
$publickey = "...";
echo recaptcha_get_html($publickey);
?></p>
<p><button type="submit">Contact me</button></p>
</form>
<?php }?>


Edit: Working script top of page 3
 
ok, so...

... it has come up with two tables in design view? and when i preview it the page apears blank.

Z
 
btw, i copied and pasted all of the above into my contact page and changed all the relevant bits i could see.

Z
 
Jazajay said:
Yeah but if I do that all my bolding goes and that took me some time there fella. : )

Ah, I assumed your bolding was your version of syntax highlighting :p
 
Do you mean forms in design mode as I haven't used any tables?
Aww yeah that would be because I have used server code I imagine. View the source if you can in a browser, if your server can interpret PHP code upload it to a temporary directory and try it there.
 
ok tried to preview again, got this error


Parse error: syntax error, unexpected '}' in /Applications/XAMPP/xamppfiles/htdocs/rootfolder web/home/contact/contact.php on line 150

line 150 is this,

PHP:
} if(!empty($_POST['mess'])){
 
Right change the line above it and add a semi-colon ( ; ) to the end of it, as I missed it off.
So ~
$err=$err."You need to enter an email address so I can contact you<br />"
} if(!empty($_POST['mess'])){


Should be ~

$err=$err."You need to enter an email address so I can contact you<br />";
} if(!empty($_POST['mess'])){


Amended the first script.

Key:
Red ~ PHP
 
@Harry
LOL, no those where the bits I added to his script, lol. How random would that of been, back to monkey coding again. :D
 
yep thats that fixed, now only one problem,

it says its sent without verifying through the recaptcha

also how do i set up my local host to send emails?

Cheers,

Z
 
Err....TBH I don't know every time I get the mail error I know the message is sending so i load it to the server and just double check and it does, so never needed to set it up. At a guess maybe just activate it but I'm not 100% sure.

Now 1 problem?
What would that be?

O yeah hold on.....
 
@Jazajay: There's a couple of things that could improve that file.

First, if the action attribute on the form tag is left blank then it will automatically submit to itself. Secondly, you can increment a variable like this:

PHP:
$count++;

Rather than:

PHP:
$count = $count + 1;

Other than that, seems pretty swift and does its job. Saves me writing one off the cuff ;)

Also, check out http://www.toolheap.com/test-mail-server-tool/ for testing emails in PHP scripts. I use it and it's brilliant.
 
Back
Top