简体   繁体   中英

Cannot POST /get.php

I have looked at many of the other threads on Stack Overflow although I cannot see why my code is not submitting or it will just return the php code itself. I tried download XAMPP although I do not understand how to use it at all. I am trying to email the form to the specified email address, however it will either give me a Cannot POST or it will return the code itself(before I added the extra variables). Any help would be greatly appreciated.

Here is my PHP file

<?php 
if (isset($_POST['submit'])) {
    $to = $_POST['email'];
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $email = $_POST['email'];
    $dayPhone = $_POST['dayPhone'];
    $homePhone = $_POST['homePhone'];
    $streetAddress = $_POST['streetAddress'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zipCode = $_POST['zipCode'];
    $comments = $_POST['comments'];

    $message = "
    To: $to \n
    First Name: $firstName \n
    Last Name: $lastName \n
    Email: $email \n
    Day Phone: $dayPhone \n
    Home Phone: $homePhone \n
    Street Address: $streetAddress \n
    City: $city \n
    State: $state \n
    Zip Code: $zipCode \n
    Comments: $comments \n
    ";

    $from = "me@gmail.com";
    $headers = "FROM: " . $from;

    mail($to, $subject, $message, $headers);
    if(mail($to, $subject, $message, $headers)){
        echo "Mail Sent.";
    }
    else {
        echo "failed";
    }
}

?>

And here is my HTML file. As you can see, I have many variables in my HTML. I am trying to at least get the PHP to show the variables in a separate window so I know it's working, then I can work on getting the email to send separately

<DOCTYPE! html>
<html>
<head>
    <link rel = "stylesheet" type = "text/css" href = "styles.css">

    <div class = "Mart">Mikes Auto Mart</div>
    <div class = "header">123 Main Street <br> Anywhere, UT 88888 <br> Phone: 801:123:4567 <br> Toll Free: 1-888-888-8888</div>
    <table style = "width:100%" class = "homeTable">
    <tr>
        <td><a href = "index.html"><u>Home</u></a></td>
        <td><a href = "Inventory.html"><u>Inventory</u></a></td>
        <td><Financing</td>
        <td>About Us</td>
        </tr>
    </table>

</head>
<body>

        <br><br>
<form action = "get.php" method= "post">

First Name: <input type = "text" name = "firstName" width = "50px"><br>
Last Name: <input type = "text" name = "lastName" width = "50px"><br>
Email Address: <input type = "text" name = "email" width = "50px"><br>
Day Phone: <input type = "text" name = "dayPhone" width = "50px"><br>
Home Phone: <input type = "text" name = "homePhone" width = "50px"><br>
Street Address: <input type = "text" name = "streetAddress" width = "50px"><br>
City: <input type = "text" name = "city" width = "50px"><br>
State: <input type = "text" name = "state" width = "50px"><br>
Zip Code: <input type = "text" name = "zipCode" width = "50px"><br>
Comments: <input type = "text" name = "comments" width = "100px"><br>
<input type = "submit"  name = "submit">
    </form>
</body>
</html>

Thanks again for any advice you have to offer!

Your $_POST parameters appear good; the problem simply seems to be that you have neglected to create the $subject variable. Simply creating this variable (or assigning it to a $_POST parameter) should resolve your problem: $subject = $_POST['subject'] ?: 'You have mail'; .

To ensure that your variables are correctly set when your $_POST data is omitted I'd also recommend setting default values, as can be seen in the $subject line above, which makes use of the ternary operator . If the $_POST['subject'] is set, it is used. If not, the 'You have mail' subject is used instead.

Note that you don't need to call mail() in addition to if(mail()) , as the conditional itself will trigger the send. As it stands, your code will mail out twice, so you'll want to remove the one that's not inside of the conditional.

As an aside, you also have the $to and $email variables both coming from $_POST['email'] ; you can save on a variable here.

Based on the details you provided about not being able to install XAMPP, and that you are seeing the "code itself," I'm guessing you don't have a working webserver that is even executing the PHP code. You'll need one, as you wont be able to execute PHP simply by pointing your browser locally.

If you think it is beyond your skills to install XAMPP, perhaps you might think about a shared webhost (Hostgator, Dreamhost, many, many others) to host your code for you. Alternatively, and this is what I would recommend, you can learn how to install a LAMP (Linux, Apache, MySQL, PHP) stack on a small, cheap VPS. There are a million HOWTOs and walkthroughs on how to do this online. One of the ones I recommend is at Digital Ocean , who also rent out such cheap VPSes for a few cents an hour (their cheapest $5/month server is plenty for messing around with stuff like this).

You haven't declared the $subject variable, this is a required param in PHP's mail() , otherwise you won't be able to send the mail and your script will fail.

And you're calling the mail function twice times. Your PHP script could be as this:

<?php
// Verifies if the submit button was triggered
if (isset($_POST['submit'])) {

    // Defines the message content
    $to = $_POST['email'];
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $email = $_POST['email'];
    $dayPhone = $_POST['dayPhone'];
    $homePhone = $_POST['homePhone'];
    $streetAddress = $_POST['streetAddress'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zipCode = $_POST['zipCode'];
    $comments = $_POST['comments'];

    $subject = 'Whatever';

    // Mounts the message
    $message = "
    To: $to \n
    First Name: $firstName \n
    Last Name: $lastName \n
    Email: $email \n
    Day Phone: $dayPhone \n
    Home Phone: $homePhone \n
    Street Address: $streetAddress \n
    City: $city \n
    State: $state \n
    Zip Code: $zipCode \n
    Comments: $comments \n
    ";

    $from = "me@gmail.com";
    $headers = "FROM: " . $from;

    // Checks if the email has been sent
    if(mail($to, $subject, $message, $headers)){
        echo "Mail Sent.";
    }
    else {
        echo "failed";
    }
}
?>

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