Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

emailing a received input

the mail() function

Moderators: macek, egami, gesf

emailing a received input

Postby tt3955 » Mon Oct 03, 2011 6:24 pm

Hi everyone, I'm really confused on how I go about emailing an input from a form. I tried to look it up, but I'm not even sure how to really word my question.

I tried this but I'm not sure if I'm even close.

Code: Select all
<html>
<body>
<?php

  <table width="300" cellpadding="10" cellspacing="10">
    <tr>
        <form id="form1" name="form1" method="post" action="">
          <p>
            <label for="email"></label>
            <span class="d">Type anything here!<br/> </p>       
          <p>
            <input type="text" name="$message" />
            <input type="submit" name="mysubmit" id="submit" value="Send" />
          </p>
         
$to = "myemailaddress@email.com";
$subject = "test mail";
$message = "$message";
mail($to,$subject,$message);
echo "Mail Sent.";

</form>
?>
</body>
</html>

Am I even close?

tt3955
New php-forum User
New php-forum User
 
Posts: 9
Joined: Sun Oct 02, 2011 5:41 pm

Re: emailing a received input

Postby KelvinLee18 » Mon Oct 03, 2011 11:47 pm

Try this. I've just tried it and it works.

Code: Select all
<html>
<body>

    <form id="form1" name="form1" method="post" action="">
        <p>
            <label for="email"></label>
            <span>Type anything here!</span>
        </p>       

        <p>
            <input type="text" name="message" />
            <input type="submit" name="mysubmit" value="Send" />
        </p>
    </form>
<?php       

if(isset($_POST["mysubmit"])) {
    $to = "email@yourdomain.com";
    $subject = "test mail";
    $message = $_POST["message"];
    if(mail($to,$subject,$message)) {
        echo "Mail Sent.";
    } else {
        echo "Failed!";
    }
   
}
?>
         
</body>
</html>


To make things simple I've left out the <table>. When you make a form, it's just a plain html, no php. And remember to assign name="namehere" to every input, as the inputs would be passed to php with the name.

When it comes to processing the input from the form,
Code: Select all
if(isset($_POST["mysubmit"])) {

This code checks if a form is submitted, if yes, then it continues to execute the code in it.

Oh, and one more thing is that, when you choose method="post", the inputs will be stored in an array $_POST when it is passed to PHP, $_GET for method="get".

I suggest you reading some great tutorials found out there, like this one in w3schools.
http://www.w3schools.com/php/php_forms.asp
KelvinLee18
New php-forum User
New php-forum User
 
Posts: 36
Joined: Sat Oct 01, 2011 4:44 pm


Return to PHP coding => Mail

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron