简体   繁体   中英

PHP not grabbing form information

I'm relatively new to PHP and I'm having trouble getting my PHP to take my form data and submit it in an email. The email sends to me with the plain text I have in it but all the form data isn't there. Does anybody have any ideas of what's wrong?

Here's the HTML:

<div id="box3" class="clearfix">
        <form action="contact.php" method="post" enctype="text/plain">
            <div id="greyContainer" class="clearfix">
            <p id="text5">
            Contact Us
            </p>
            <p id="text6">
            Feel free to contact us and leave a message if you have any general questions&#x21;
            </p>

            <label id="formgroup">
                <p id="text7">
                Name&#x3a;
                </p>
                <input id="name" type="text" value="Full Name" name="name"></input>
            </label>
            <label id="formgroup1">
                <p id="text8">
                Number&#x3a;
                </p>
                <input id="phone_number" type="text" value="Phone Number" name="phone_number"></input>
            </label>
            <label id="formgroup2">
                <p id="text9">
                Email&#x3a;
                </p>
                <input id="email" type="text" value="Email Address" name="email"></input>
            </label>
            <label id="formgroup3">
                <p id="text10">
                Message&#x3a;
                </p>
                <textarea id="message_block" name="message_block" >Message or question...</textarea>
            </label>
            <p id="text11">
            <span id="textspan">AVA Roofing &amp; Siding<br />12 Arcade Ave.<br />Amherst, NY 14226<br />716.602.3947</span><br />&nbsp;
            </p>
            <img id="image1" src="img/envelope.png" class="image" />
            <a href="thank_you.html">
                <input id="input" type="submit" value="Submit" name="submit" ></input>
            </a>

            <div id="whiteLine" class="clearfix">
            </div>
    </form>
        </div>

    </div>

Here's the PHP:

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

 $to = "example@gmail.com" ; 
$from = $_POST['email'] ; 
$name = $_POST['name'] ;
$headers = "From: $from"; 
$subject = "AVA Roofing Contact Form"; 
$message2 = $_POST['message_block'];
$number = $_POST['phone_number'];

$message = "A visitor has submitted the following requirements. \n\nName:         $name\n\nEmail: $email\n\nNumber: $number\n\nMessage: $message";

$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to, $subject, $message, $headers);

$send = mail($to, $subject, $message, $headers);  
if($send) 
{header( "Location: http://www.example.com/folder/thank_you.html" );} 
else 
{print "We encountered an error sending your mail, please notify                    webmaster@YourCompany.com"; }

?>

You've got the wrong encoding on your form:

<form action="contact.php" method="post" enctype="text/plain">
                                        ^^^^^^^^^^^^^^^^^^^^^

Since you're telling the browser to send it as text/plain, it will not be processed by PHP. For this simple form, you don't need to specify ANY encoding. Just accept the defaults.

If you insist on specifying an encoding, it should be application/x-www-form-urlencoded for standard forms, or multipart/form-data if you're doing file uploads.

You can send to other file like file.php and in this file the action you would have something like this:

   $recipiente = "your@email.com ";
   $asunto = "Formulario";
   $message ="name: ".$name."<br>";
   $message .="email: ".$email."<br>";
   $message .="coment: ".$coment."<br>";

   $message = stripslashes($message);

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

   mail($recipiente,$asunto,$message,$headers);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM