www.webdeveloper.com
Results 1 to 10 of 10

Thread: php mail() function in a form

  1. #1
    Join Date
    Apr 2008
    Posts
    136

    php mail() function in a form

    I'm trying to create a form using the php mail() function. The user will enter a name, e-mail, address and a question. When submitted it will send a message to them. I'm trying to create it putting the php code on the same page as the form. I haven't even tested the code yet, because when just viewing the form, some of the php code is displayed on the html page.

    Here's my code:

    <?
    $to = "$_POST['email']";
    $subject = "Your Registration";
    $body = "Your registration has been received.";
    $headers = "From:KenB624@yahoo.com\r\n";

    if (mail($to, $subject, $body, $headers))
    {
    echo("<p>Message has been sent!</p>");
    }
    else
    {
    echo("<p>Error! Message delivery attempt failed.</p>");
    }
    ?>

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

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Sending Mail</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />


    </head>

    <body>
    <form>
    <h2 align="center">Sending Mail</h2>

    <br />
    <div>
    <p>Enter your name, password and a question, then click "Submit":</p><br />
    <p>Name:<input type="text" name="name" size="20"></p>
    <p>E-mail:<input type="text" name="email" size="20"></p>
    <p>Question:<input type="text" name="question" size="20"></p>
    </div>
    <br />
    <div><input type="submit" name="submit" value="Submit" /></div>
    <br />
    <div>
    <input type="reset" name="Reset" value="Start Over" />
    </div>

    </form>



    </body>
    </html>

  2. #2
    Join Date
    Feb 2009
    Location
    Nottingham
    Posts
    30
    Hey Michiken

    Try this.

    PHP Code:
    <?
    if(isset($_POST['submit'])){

    $to $_POST['email'];
    $subject "Your Registration";
    $body "Your registration has been received.";
    $headers "From:KenB624@yahoo.com\r\n";

    if (
    mail($to$subject$body$headers))
    {
    echo(
    "<p>Message has been sent!</p>");
    }
    else
    {
    echo(
    "<p>Error! Message delivery attempt failed.</p>");
    }
    }
    ?>

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

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Sending Mail</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />


    </head>

    <body>
    <form action="<? $_SERVER['PHP_SELF']; ?>" method="post">
    <h2 align="center">Sending Mail</h2>

    <br />
    <div>
    <p>Enter your name, password and a question, then click "Submit":</p><br />
    <p>Name:<input type="text" name="name" size="20"></p>
    <p>E-mail:<input type="text" name="email" size="20"></p>
    <p>Question:<input type="text" name="question" size="20"></p>
    </div>
    <br />
    <div><input type="submit" name="submit" value="Submit" /></div>
    <br />
    <div>
    <input type="reset" name="Reset" value="Start Over" />
    </div>

    </form>



    </body>
    </html>

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,154
    Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    Also, I would suggest always using the full <?php tag instead of <? to ensure compatibility with any PHP configuration.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Apr 2008
    Posts
    136
    Thanks, but the php code still shows when viewing it through the web browser. I'm using Internet Explorer, version 6.0.29.

    <?
    if(isset($_POST['submit'])) {
    $to = $_POST['email'];
    $subject = "Your Registration";
    $body = "Your registration has been received.";
    $headers = "From:KenB624@yahoo.com\r\n";

    if (mail($to, $subject, $body, $headers))
    {
    echo("<p>Message has been sent!</p>");
    }
    else
    {
    echo("<p>Error! Message delivery attempt failed.</p>");
    }
    }
    ?>

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

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Sending Mail</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />


    </head>

    <body>
    <form action="<? $_SERVER['PHP_SELF']; ?>" method="post">
    <h2 align="center">Sending Mail</h2>

    <br />
    <div>
    <p>Enter your name, password and a question, then click "Submit":</p><br />
    <p>Name:<input type="text" name="name" size="20"></p>
    <p>E-mail:<input type="text" name="email" size="20"></p>
    <p>Question:<input type="text" name="question" size="20"></p>
    </div>
    <br />
    <div><input type="submit" name="submit" value="Submit" /></div>
    <br />
    <div>
    <input type="reset" name="Reset" value="Start Over" />
    </div>

    </form>



    </body>
    </html>

  5. #5
    Join Date
    Apr 2008
    Posts
    136
    NogDog and martswite,

    I've included both of your recommendations in my code but the php code is still visible through the web browser.

    <?php
    if(isset($_POST['submit'])) {
    $to = $_POST['email'];
    $subject = "Your Registration";
    $body = "Your registration has been received.";
    $headers = "From:KenB624@yahoo.com\r\n";

    if (mail($to, $subject, $body, $headers))
    {
    echo("<p>Message has been sent!</p>");
    }
    else
    {
    echo("<p>Error! Message delivery attempt failed.</p>");
    }
    }
    ?>

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

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Sending Mail</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />


    </head>

    <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <h2 align="center">Sending Mail</h2>

    <br />
    <div>
    <p>Enter your name, password and a question, then click "Submit":</p><br />
    <p>Name:<input type="text" name="name" size="20"></p>
    <p>E-mail:<input type="text" name="email" size="20"></p>
    <p>Question:<input type="text" name="question" size="20"></p>
    </div>
    <br />
    <div><input type="submit" name="submit" value="Submit" /></div>
    <br />
    <div>
    <input type="reset" name="Reset" value="Start Over" />
    </div>

    </form>



    </body>
    </html>

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,154
    Are you running it from your web host or locally from your PC? If the host, does it, in fact, support PHP? If your PC, do you have PHP and a web server installed and running, and are you accessing the page via a "localhost" URL (as opposed to double-clicking the file icon or loading it via the File->Open menu option of the browser)?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Apr 2008
    Posts
    136
    I'm running it from a web host that does support php.

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,154
    I copied-and-pasted your last version and ran it locally and it seemed to function fine (PHP 5.2.6).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,154
    One more (hopefully stupid) question, did you give the file a ".php" suffix instead of ".html"?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  10. #10
    Join Date
    Apr 2008
    Posts
    136

    resolved [resolved]

    LOL! I had it as an .html file. Changed it to a .php extension and now it works. Thank you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles