Forum Moderators: coopster

Message Too Old, No Replies

php help for uber beginner

php,beginner

         

kateharp

11:37 pm on Aug 8, 2010 (gmt 0)

10+ Year Member



I tried to attach a php form to my website but when I get the email of the results, it's empty. I know like zero about php and every time I try to read a book for it, it's all about big projects. I don't know programming. I also use godaddy which has php on their server.

Can you tell me what is wrong and also what my php code?
And also tell what the top of the <form> tag should say to link this all together? The name of my php file is webformmailer.php

<?php
$to='addressgoes@here.com';
$email='email';
$company='$company';
$products='$products';
$req='1';
$sent=mail($to,$email,$company,$products) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>

rocknbil

2:15 am on Aug 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard kateharp,

$products='$products';
$req='1';

$req does nothing and can be deleted. But the body of your message should have been "$products", and none of your other variables are set right, info below. When you single-quote, variables don't interpolate.

$products = 'blah';
echo $products; // prints "blah"
echo '$products'; // prints "$products"
echo "$products"; // prints "blah" - double quote to interpolate

Second, the variable "products" is never actually set to anything except when you do it here.

$products='$products';

Your form should point to your script, looks like you figured that out, but the two methods are post and get. Get gives you an ugly query string in the browser, and has some size limitations (not as much as it used to) but use post when you can

<form method="post" action="yourscript.php">
<textarea name="comments" id="comments"></textarea>
<!-- etc. -->
</form>

So your script looks for inclimg named elements in the form - for the above, it would look for comments:

$comments = $_POST['comments'];

Then the valid parameters of mail are to, subject, message body, with the from in the headers. Will get into that in a sec, but now you'd have

$sent=mail($to,$subject,$comments,$headers);

See how that works? So populate your variables **something** like this (warning below.)

$to = 'company_email@example.com';
$email = $_POST['email']; // assuming form field named "email"
$comments = $_POST['comments'];
$subject='Company Name Site Inquiry';

$body = "
<p>Message from: $email</p>
<p>Message: $comments</p>
";

$headers = "From: $email\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$sent=mail($to,$subject,$body,$headers);

That will work . . . sorta.

There's a **big** catch to my "sorta." When you start setting up to accept user input, the first thing you should learn after getting it to work is filtering input and cleansing the input, forms are the prime target for spamming and hacking web sites. Dig around this site for many threads on this topic. Don't fall victim to "users of this site won't be doing any of that" - your users aren't the ones you have to worry about.

kateharp

2:45 am on Aug 9, 2010 (gmt 0)

10+ Year Member



wow. thanks for the detailed response and spamming issues. Now I have some homework to do!