Page 1 of 1

Getting variables from form and create mail(newbie question)

Posted: Sat May 22, 2004 11:56 am
by aikman
I want to get all the variables from the form and then send them in a mail. I want to send the mail in the same PHP page as the form is located.

Here's the form:

Code: Select all

<form>
    <table width="594" border="1">
      <tr>
        <td width="147" height="19">Naam:</td>
        <td width="359"><input name="naam" type="text" id="naam" size="70"></td>
      </tr>
      <tr>
        <td>Straat en nummer:</td>
        <td><input name="straat" type="text" id="straat" size="70"></td>
      </tr>
      <tr>
        <td>Postcode:</td>
        <td><input name="postcode" type="text" id="postcode" size="70"></td>
      </tr>
      <tr>
        <td>Gemeente:</td>
        <td><input name="gemeente" type="text" id="gemeente" size="70"></td>
      </tr>
      <tr>
        <td>Email-adres:</td>
        <td><input name="email" type="text" id="email" size="70"></td>
      </tr>
      <tr>
        <td>Tel. of GSM-nummer: </td>
        <td><input name="telefoon" type="text" id="telefoon" size="70"></td>
      </tr>
      <tr>
        <td colspan="2"><input name="button" onClick:'mailFunction' value="Bestelling verzenden per email"></td>
      </tr>
    </table>
</form>
So when the button is clicked the mailFunction should start. How do you define a function in PHP?
So the mail function should be something like this:

Code: Select all

<?php
/* recipients */
$to  = "mary@example.com" . ", " ; // note the comma
$to .= "kelly@example.com";

/* subject */
$subject = "User info";

/* message */
$message = '
<html>
<body>
Naam: <? echo _GET[naam];?>   /* How do you get that form variable???*/
Straat: <? echo _GET[straat];?>
and so on...
</body>
</html>
';

/* and now mail it */
mail($to, $subject, $message);
?>
Can anyone help me with this code?

Thanks

Posted: Sat May 22, 2004 12:03 pm
by launchcode
I would suggest mailing the form values before you echo the HTML out. But to answer your question, if the form was POSTED (method=post) then use:

$name = $_POST['naam'];

and if the form wasn't using the post method, use this:

$name = $_GET['naam'];

(the quotes are important).

Posted: Sat May 22, 2004 12:08 pm
by zoidberg
Here's a simple I wrote awhile ago.

I doubt if it's rfc compliant or whatever, but it works well.

Like I said it's very simple but a good place to start.


Feedback Form Processor:
<?

$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$subject=$_POST['subject'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$comments=$_POST['comments'];

mail("recipient here","$subject","Sender: $firstname $lastname\n$email\n $phone\nMessage: $comments","From: $email\r\n");

header("Location: thx.php");

?>

Posted: Sat May 22, 2004 12:15 pm
by Nay
Just on the note, here's a neater way you can write the message:

Code: Select all

/* message */
$message = <<< MSG

<html>
<body>

hello, my name is
{$_POST['naam']}
and my number's
{$_POST['telefoon']}

</body>
</html>

MSG;
hope it helps,

-Nay