Forum Moderators: coopster

Message Too Old, No Replies

Editing a contact form script

         

slippyjim

1:47 pm on May 30, 2007 (gmt 0)

10+ Year Member



Hiya,

First of all I know nothing about php, I did a bit of programming with ADA and C++ over 4 years ago so my knowledge is very limited.

What am I trying to do is to have a contact form on my website. I got a free script from somewhere on the web, but am now trying to get it to do a few extra things.

What I want to happen is if all the fields that are entered in are validated OK then they are redirected to one webpage saying "I will reply asap", and if they mess something up then a message is displayed saying go back and correct the problem.

Before I started messing with the script it displayed the message about going back and correcting the problem OK but it didn't automatically redirect to the webpage saying "I will reply asap".

Here is the script befoe I broke it :)
<?php
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$notes = $_POST['notes'];
$attn = $_POST['attn'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sendemail Script</title>
</head>
<body>

<!-- Reminder: Add the link for the 'next page' (at the bottom) -->
<!-- Reminder: Change 'YourEmail' to Your real email -->

<?php
if(!$visitormail == "" && (!strstr($visitormail,"@") ¦¦!strstr($visitormail,".")))
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
}
if(empty($visitor) ¦¦ empty($visitormail) ¦¦ empty($notes )) {
echo "<h2>Use Back - fill in all fields</h2>\n";
}

$todayis = date("l, F j, Y, g:i a") ;

$attn = $attn ;
$subject = $attn;

$notes = stripcslashes($notes);

$message = " $todayis [EST] \n
Attention: $attn \n
Message: $notes \n
From: $visitor ($visitormail)\n
";

$from = "From: $visitormail\r\n";

mail("info@example.com", $subject, $message, $from);

?>

</p>

</body>
</html>

and here is what I was attempting to do (stop laughing!)
<?php
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$notes = $_POST['notes'];
$attn = $_POST['attn'];
$sentok = $_POST['boolean'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sendemail Script</title>
</head>
<body>

<!-- Reminder: Add the link for the 'next page' (at the bottom) -->
<!-- Reminder: Change 'YourEmail' to Your real email -->

<?php
if(empty($visitor) ¦¦ empty($visitormail) ¦¦ empty($notes) ¦¦ empty($subject) ¦¦ empty($phone_number))
{
echo "<h2>Use Back - fill in all fields</h2>\n";
}

if(!$visitormail == "" && (!strstr($visitormail,"@") ¦¦!strstr($visitormail,".")))
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
}

$sentok = true;

$todayis = date("l, F j, Y, g:i a") ;

$attn = $attn ;
$subject = $subject;

$notes = stripcslashes($notes);

$message = " $todayis [GMT] \n
Message: $notes \n
From: $visitor ($visitormail)\n
Phone Number: $phone_number \n
";

$from = "From: $visitormail\r\n";

mail("info@example.com", $subject, $message, $from);

?>

<?php

<p>
if ($sentok == true)
<meta http-equiv="Refresh" content= "0;URL=http://www.example.com/thanks.htm">;
else
<meta http-equiv="Refresh" content= "0;URL=http://www.example.com/failed.htm">;

</p>

?>

</body>
</html>

Any ideas what I'm doing wrong or even what I'm on about?

Many thanks in advance,
Steve

Duskrider

2:21 pm on May 30, 2007 (gmt 0)

10+ Year Member



Hey Steve,

<p>
if ($sentok == true)
<meta http-equiv="Refresh" content= "0;URL=http://www.example.com/thanks.htm">;
else
<meta http-equiv="Refresh" content= "0;URL=http://www.example.com/failed.htm">;

</p>

You've got that in a php block, which is ok for the if statement, but the html in there won't be output to the page unless you ECHO it out like this:

echo '<p>';
if ($sentok == true)
echo '<meta http-equiv="Refresh" content="0;URL=http://www.example.com/thanks.htm">';

All of your html content will need to be echoed in this way while you're inside of a <?php?> block. Don't forget to put it inside the single quotes.

Alternately you can do what I usually do, which is change the location header with php like this:

<?php
header('Location: http://www.example.com/thanks.htm');
?>

Hopefully one of those will work for you. Good luck. :)

-DR-

Duskrider

2:30 pm on May 30, 2007 (gmt 0)

10+ Year Member



Looking at it again, it looks like the php header function won't work for this case though. The function that I mentioned will only work if nothing has yet been output to the browser. In this case the script has already put stuff out there, so that probably won't work.

Echoing the Meta Refresh tags in the way I suggested is probably your best bet.

echo '<p>';
if ($sentok == true)
echo '<meta http-equiv="Refresh" content= "0;URL=http://www.example.com/thanks.htm">';
else
echo '<meta http-equiv="Refresh" content= "0;URL=http://www.example.com/failed.htm">';
echo '</p>';

[edited by: Duskrider at 2:32 pm (utc) on May 30, 2007]

slippyjim

2:52 pm on May 30, 2007 (gmt 0)

10+ Year Member



Ok, thanks for your quick reply I will give it a go and let you know what happens.

Thanks,
Steve

slippyjim

2:59 pm on May 30, 2007 (gmt 0)

10+ Year Member



just tried that, and it works exactly how I want it to if teh form is completed correctly, but if something is not validated correctly, it briefly flashes up saying "Use Back - fill in all fields" then goes to the thanks.htm page.

certainly an improvement on what it was doing before though!

slippyjim

3:12 pm on May 30, 2007 (gmt 0)

10+ Year Member



It seems to be ignoring the sentok value, I´m probably doing something wrong with that

slippyjim

3:48 pm on May 30, 2007 (gmt 0)

10+ Year Member



I've got it sorted now, it's a lot simpler now but it does exactly what I want.

Thanks for your help, I never would have got there without it.

If anyone is interested here is the script

<?php
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$notes = $_POST['notes'];
$attn = $_POST['attn'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sendemail Script</title>
</head>
<body>

<!-- Reminder: Add the link for the 'next page' (at the bottom) -->
<!-- Reminder: Change 'YourEmail' to Your real email -->

<?php
if(empty($visitor) ¦¦ empty($visitormail) ¦¦ empty($notes) ¦¦ empty($subject) ¦¦ empty($phone_number) ¦¦ (!$visitormail == "" && (!strstr($visitormail,"@") ¦¦!strstr($visitormail,"."))))
{
echo '<meta http-equiv="Refresh" content= "0;URL=http://www.example.com/failed.htm">';
}
else
echo '<meta http-equiv="Refresh" content= "0;URL=http://www.example.com/thanks.htm">';

$todayis = date("l, F j, Y, g:i a") ;

$attn = $attn ;
$subject = $subject;

$notes = stripcslashes($notes);

$message = " $todayis [GMT] \n
Message: $notes \n
From: $visitor ($visitormail)\n
Phone Number: $phone_number \n
";

$from = "From: $visitormail\r\n";

mail("info@example.com", $subject, $message, $from);

?>
</body>
</html>

eelixduppy

3:59 pm on May 30, 2007 (gmt 0)



Code looks fine except for one thing, you are leaving yourself vulnerable to email header injection [securephpwiki.com]. Read up on this; you will be glad you did :)

And Welcome to WebmasterWorld!

Good luck!