Forum Moderators: coopster

Message Too Old, No Replies

Could you help me with my form mailer?

         

naitsirhc26

9:30 pm on Aug 25, 2006 (gmt 0)

10+ Year Member



Hello, I have this really simple script that I would like to be spiced up. I want it to redirect the user after they use the form, back to my homepage. I would like it to have sessions to protect from spammers, and I was wondering if there was any way to make it so the form that is sent to me, is more than just plain text. With this script, it sends the form to be in plain text, and its hard to read. If anyone could add some better code to this script, it would be greatly appreciated. This needs to be done tonight.

Thanks.

Here is the code:

<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
// In testing, if you get an Bad referer error
// comment out or remove the next three lines
if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ¦¦
!strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
die("Bad referer");
$msg="Values submitted by the user:\n";
foreach($_POST as $key => $val){
if (is_array($val)){
$msg.="Item: $key\n";
foreach($val as $v){
$v = stripslashes($v);
$msg.=" $v\n";
}
} else {
$val = stripslashes($val);
$msg.="$key: $val\n";
}
}
$recipient="me@yahoo.com, me@example.com";
$subject="Contact Form";
error_reporting(0);
if (mail($recipient, $subject, $msg)){
echo "<center><h1>Thank you</h1><p>Message successfully sent!</p></center><br>\n";
echo nl2br($input);
} else
echo "An error occurred and the message could not be sent.";
} else
echo "Bad request method";

?>

[edited by: jatar_k at 10:13 pm (utc) on Aug. 25, 2006]
[edit reason]
[1][edit reason] generalized urls [/edit]
[/edit][/1]

jatar_k

5:24 pm on Aug 26, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you plan on keeping the thank you message you could just add a meta refresh to the output that would refresh after a second or two and send them to the main page.

something like

<meta http-equiv="refresh" content="2;url=http://www.example.com/index.html">

where 2 would be the number of seconds to wait before the refresh

as far as changing the email format, it is just a matter of adding html to the foreach loops and probably changing the mail headers.

adding sessions would involve adding sessions to the rest of your site too, I imagine. This isn't hard but could be a fair amount of work. I would roll it out as is and then you can add sessions later if needed.

naitsirhc26

2:57 pm on Aug 30, 2006 (gmt 0)

10+ Year Member



Thank you very much, that helped with the refresh. Does anyone know how to do anything else? Could you please show me how to use html to make the form nicer looking on my end?

jatar_k

4:56 pm on Aug 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well, this is the loop that builds the message

foreach($_POST as $key => $val){ 
if (is_array($val)){
$msg.="Item: $key\n";
foreach($val as $v){
$v = stripslashes($v);
$msg.=" $v\n";
}
} else {
$val = stripslashes($val);
$msg.="$key: $val\n";
}
}

it builds it line by line, value by value, you just need to add html to the loop. Play around and see what looks good