Please help with some simple HTML Form code

Miscellaneous Forums/General Discussion/Please help with some simple HTML Form code

Hi, I'm no web expert and would love a simple bit of help please. Basically my newsletter code here:

http://www.greyaliengames.com/news.php

no longer works because the provider of the newsletter has changed.

So all I want is for people to enter their email in the box and click submit and for it to send an email to info"AT"greyaliengames.com with their email address in it. Can this be done easily? Do I need mailto.cgi and point it at my SMTP server or something as I don't want the user to have to use their own email program.

Any help with this would be great thanks.

Change:

<form method='post' action='http://www.indiegamebusiness.com/newsletter.subscribe.php'>

to

<form method='post' action='mailto:[your email address]'>

You will be better creating a php script to re-direct it though - because if you send it directly to your e-mail address, users will have the information sent via Outlook as an e-mail (or they wont be able to send it if they don't have an e-mail client).

Use something like this as your php script to send to:

<?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 = "Newsletter request";

$notes = stripcslashes($notes);

$message = " $todayis [EST] \n
Attention: $attn \n
Message: $notes \n
From: $visitor ($visitormail)\n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n
Email: ".$_POST['email']
";

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


mail("YourEmail", $subject, $message, $from);

?>



Save the code as subscribe.php and change the line of HTML mentioned above to:

<form method='post' action='subscribe.php'>

Not tested it, but hopefully that will work.

Gameboy. Thanks very much I'll try that out! So the script won't need them to use Outlook Express or whatever, cool. Doesn't it need to know my smpt server or something?

Hmm, well it had a syntax error on this line:

Email: ".$_POST['email']

Somthing about an unexpected "

So I removed it and have changed the code to this:

<?php
if(!$visitormail == "" && (!strstr($visitormail,"@") || !

strstr($visitormail,".")))
{
echo "<h2>Please use the Back button and enter a valid e-mail 

address.</h2>\n";
}
if(empty($visitormail)) {
echo "<h2>Please use the Back button and enter an e-mail 

address.</h2>\n";
}

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

$attn = $attn ;
$subject = "Newsletter request";

$notes = stripcslashes($notes);

$message = " $todayis [EST] \n
Attention: $attn \n
Message: $notes \n
From: $visitor ($visitormail)\n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n
";

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


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

?>


Now when I run it it always reports "Please use the Back button and enter an e-mail address" so basically it thinks visitormail is empty. I've changed the news.php page so that the form has this <input type='text' name='visitormail' size='40'> but it doesn't seem to work. Any ideas where I'm going wrong? Thanks.

Change

Email: ".$_POST['email']

to

Email: ".$_POST['email'].";

and delete the "; from the line below. This will allow for the e-mail addresses that are sent from your form to be sent to you. I shall take a proper look at the code later and have a finalised solution for you.

As for it having to know your SMTP server, PHP should be able to send it automatically, but if not, you should check with your host for any settings you have to change with PHP commands (I had to do this with mine).

Game Boy: Thanks, I'm beginning to figure out this php lark. It still thought the email address was blank so I put this line at the top:

$visitormail = $_POST['email'];

And made sure my form input was called email.

Also further down I couldn't make the change exactly as you suggested because that email line was part of a bigger message, but I jut added a . and a " after ['email'] and it worked fine. Many thanks for your help.

Now, one final question. I have a nice "thanks for subscribing" page, how can I redirect the flow there after the letter has been sent please?

Many thanks :-)

header("Location: http : // www.myurl.com"); // Remove the unwanted spaces - Hyperlinks in Codeboxes! whatever next.:)
exit;


You also could have put the php on that thank you page and used an if statement so if anything failed, it would show the error, and if it passed it would say thankyou.

That is how I do all of mine. So the error reports are still in the same layout and style and the rest of my site.

Thanks Tim.

DampeS8N: Yes I considered that but I don't know how the ifs work properly in php. For example on that top example I'd need two else ifs if they work in PHP.

they do. If you know any C or C++ it is exactly the same. Only instead of cout you use echo "words and stuff";.

actually.. you can retrofit the above junk like so... I'm adding lots of extra returns around the stuff I add... its only a few lines.

html for your page up to where you want text.

<?php
if(!$visitormail == "" && (!strstr($visitormail,"@") || !

strstr($visitormail,".")))
{



Did_we_hit_an_error = "yes";



}
if(empty($visitormail)) {



Did_we_hit_an_error = "yes";




}

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

$attn = $attn ;
$subject = "Newsletter request";

$notes = stripcslashes($notes);

$message = " $todayis [EST] \n
Attention: $attn \n
Message: $notes \n
From: $visitor ($visitormail)\n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n
";

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


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



if(Did_we_hit_an_error == "yes"){

echo "<h2>Please use the Back button and enter a valid e-mail address.</h2>\n";

}else{

echo "<h2>Thank you for filling this in or whatever you wanna say</h2>\n";

}

?>

rest of the HTML for the page



groovy dude. Retrofit in progess.

well that didn't get very far. It fails with

"Parse error: syntax error, unexpected '=' in /home/wwwgrey/public_html/subscribe.php on line 9"

That's this line:

Did_we_hit_an_error = "yes";

So I changed it to a string throught out the code but it doesn't work either, it runs but never dispay the success message. Seems $Did_we_hit_an_error == "yes" just doesn't ever evaluate as false, weird...

[edit] OK I see what happened, you used the code posted highter which doesn't work anyway as it doesn't take the form output.

OK it works now. Thanks to all for their help.

http://www.greyaliengames.com/news.php

It doesn't show the errors as part of the main page as I want them to stand out, but the success page is now shown.

And I know a little more php! cool.

well... you could still make the fail page pretty. Make it look like a big warning sign or something. not just a line of text. hehehe

yeah l8r when I'm not flat out with my current game...thanks.