Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Location
    Tokyo, Japan
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts

    passing values from html <input> to php mail()

    There has to be an easy way to do this, I just can't seem to find it...

    I have an .html form that once submitted will run "mail.php" (action="mail.php" method="post").

    I would like mail.php to grab the information from the form and send it to a specified (hardcoded) email address.

    below is mail.php
    (Note: this script works if I hardcode all the variables i.e - $msgBody = "blah blah blah")
    PHP Code:
        $to"some@address.co.jp";
        
    //$from = $_POST["from"];  not necessary (for me) at the moment. or is it?
        
    $subject $_POST["subject"];
        
    $msgBody $_POST["msgBody"];
        
    mail("$to","$subject","$msgBody");
        echo 
    "finished!"
    here is the .html form:
    Code:
    <form action="php/mail.php" method="post" >
    		<!-- <label>To:<input type="text" size="30" disabled></label><br/> -->
    		<input class="from" type="text" size="30" name="from" title="from" value="from_person@somwhere.co.jp" disabled>
    		<br/><br/>
    		<input class="subject" type="text" size="30" name="subject" title="subject" value="Subject">
    		<br/><br/>
    		<textarea class="msgBody" rows="13" cols="65" wrap="hard" title="msgBody" name="msgBody">type message here</textarea>
    		<br/><br/>
    		<input class="btn_send" type="submit" name="send" value="send" align="middle" />
    		<br/>
    	</form>
    How do I get the values from the text boxes and textarea to post to mail.php?

    Thanks

    Yak

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You don't need the quotes for the variables within the mail function, you can just use:
    PHP Code:
    mail($to,$subject,$msgBody); 
    Also, if you want to get all the data from the form into the message body, you could use a loop-through, something like this:
    PHP Code:
    $to"some@address.co.jp"
    $subject $_POST["subject"]; 

    $msgBody '';
    foreach(
    $_POST['msgBody'] as $key => $value) {
        
    $msgBody .= $key ": " $value "\r\n"
    }
    mail($to$subject$msgBody); 
    echo 
    "finished!"
    You might want to strip out some of the post data though, as that will include things like the button name and value. You can strip out unwanted stuff with a line in the foreach loop, something like: if($key != 'send') etc.

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Location
    Tokyo, Japan
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    alright, I know this is going to sound a little weird, bear with me...

    It was my understanding, originally, that the quotes were not necessary around the variables in the mail() function. However, once I remove them from my script, nothing works. Also when I add anything extra/different to the script, nothing works.
    When I say nothing works, I mean absolutly nothing happens. The word "finished!" doens't even echo.

    for example...

    this works:
    PHP Code:
    $to "valid@address.co.jp";
    $subject "Mail Test";
    $msg "If I don't get this working soon I will destroy this computer";
    $headers "From: anothervalid@address.com\r\n";
    mail("$to""$subject""$msg""$headers");
    echo 
    "finished!"
    this does nothing:
    PHP Code:
    $to "valid@address.co.jp";
    $subject "Mail Test";
    $msg "If I don't get this working soon I will destroy this computer";
    $headers "From: anothervalid@address.com\r\n";
    mail($to$subject$msg$headers);
    echo 
    "finished!"
    this does nothing:
    PHP Code:
    $to "valid@address.co.jp";
    $subject $_POST["subject"];
    $msg "If I don't get this working soon I will destroy this computer";
    $headers "From: anothervalid@address.com\r\n";
    mail("$to""$subject""$msg""$headers");  //with or without "quotes"
    echo "finished!"
    and, I hate to say it...this does nothing:
    PHP Code:
     $to"some@address.co.jp"
    $subject $_POST["subject"]; 

    $msgBody '';
    foreach(
    $_POST['msgBody'] as $key => $value) {
        
    $msgBody .= $key ": " $value "\r\n"
    }
    mail($to$subject$msgBody); 
    echo 
    "finished!"

    Any suggestions?

    Thanks,

    Yak

    It may have to do with the fact I'm using a Japanese computer ( the character set is S_JIS ). This has caused me grief in the past.

  • #4
    New Coder
    Join Date
    Feb 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The following works for me:
    PHP Code:

    $email 
    $_POST["email"];
    $mail_to "you@blah.com";
    $mail_subject "Subject";
    $mail_from "From: $email";
    $mail_message "You have a order!";

    mail$mail_to$mail_subject$mail_message$mail_from ); 
    I hope that helps

    Jason

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Location
    Tokyo, Japan
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks jasong...I forgot I could do it that way.

    I seem to have it working...I'm still not sure why my method doesn't work.

    Also, I still need to keep the "quotes" in the mail().


    here's the working code...

    PHP Code:
    $form_subject $_POST["subject"];
    $form_msg $_POST["msgBody"];

    $to "valid@address.co.jp";
    $subject "$form_subject";
    $msg "$form_msg";
    $headers "From: another.valid@adrress.com\r\n";
    mail("$to""$subject""$msg""$headers");
    echo 
    "finished!"

    anybody have any suggestions to why my previous methods were not working?

    Thanks,

    Yak

  • #6
    Regular Coder westmatrix99's Avatar
    Join Date
    Dec 2006
    Location
    South Africa
    Posts
    307
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Here is some code try it.

    PHP Code:
    <?php
    require('class.phpmailer.php');
    $mail = new PHPMailer();
    $mail->IsSMTP(); 
    $mail->Host 'localhost';
    $mail->From 'email@domain.com';
    $mail->FromName 'Name';
    $mail->AddBCC (email@domain.comName);
    $mail->AddReplyTo('email@domain.com''Name');// optional name
    $mail->WordWrap 50// set word wrap to 50 characters
    //$mail->AddAttachment('image.jpg', 'name of image'); // optional name
    $mail->IsHTML(true); // set email format to HTML
    $mail->Subject 'Some subject';
    $mail->Body '';
    $mail->AltBody '';
    if(!
    $mail->Send())
    {
    echo 
    'Message could not be sent. <p>';
    echo 
    'Mailer Error: ' $mail->ErrorInfo;
    exit;
    }
    echo 
    'Message has been sent';
    ?>
    Cheers
    West

  • #7
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I use the contact script here and it works fine
    http://www.joe2torials.com/view_tutorial.php?view=68
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #8
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,049
    Thanks
    2
    Thanked 317 Times in 309 Posts
    westmatrix99, check the date of posts that you add responses to. This one of from over a year and a half ago.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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