简体   繁体   中英

WAMP Issue With PHP

Please excuse my terrible formatting, I am teaching myself html, css, and php.

I have a folder on my WAMP www folder, with html, css, and php. I am trying to get the information inputed in the form to get sent to an email address. My html with css shows up fine on the localhost,however when I click the submit button the url on the localhost changes to the php file, but I get a "localhost refused to connect." Besides, well, everything, what am I doing wrong here?

This is the html file

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

    

    <body background="image/wallpaperbetter.jpg">

        <p> //code

        </p>

    </body>

    <div>
        <form method = "post" name = "myemailform" action = "https://localhost/xxxx/form-to-email.php">

            <label for="name">Name(optional):</label>
            <input type = "text"><br>

            <label for="Home">Home:</label>
            <input type = "text"><br>

            <label for="City">City:</label>
            <input type = "text"><br>

            <label for="State">State:</label>
            <input type = "text"><br>

            <label for="Zip Code">Zip Code:</label>
            <input type = "text"><br>

            <label for="Country">Country:</label>
            <input type = "text"><br>

            <input type = "submit" value = "submit">


        </form>
    </div>
    

</html>

This is the php file

<?php

if(!isset($_POST['submit']))
{
    echo "error; you need to submit the form!";
}

//Collect input
$name = $_POST['name'];
$home = $_POST['Home'];
$city = $_POST['City'];
$state = $_POST['State'];
$zip_code = $_POST['Zip Code'];
$country = $_POST['Country'];

//Validate
if(empty($home)||empty($city)||empty($state)||empty($zip_code)||empty(country))
{
    echo "Please enter all fields. (Name is Optional)";
}



$email_from = 'xxxx';
$email_subject = "New Submission";
$email_body = "New submission\nName:$name\nHome:$home\nCity:$city\nState:$state\nZip Code:$zip_code\nCountry:country"
$to =  "xxxx";
$headers = "xxxx";

//Send the email
mail($to,$email_subject,$email_body);

?>

In the HTML when you use label for you have to give your input exactly the same id as you used in the label.

You can also let the browser do the empty checking by putting the required attribute in each of your required input fields.

<div>
    <form method = "post" name = "myemailform" action = "https://localhost/xxxx/form-to-email.php">

    <label for="name">Name (optional):</label>
    <input id="name" type = "text"><br>

    <label for="Home">Home:</label>
    <input id="Home" type = "text" required><br>

    <label for="City">City:</label>
    <input id="City" type = "text" required><br>

    <label for="State">State:</label>
    <input id="State" type = "text" required><br>

    <label for="Zip Code">Zip Code:</label>
    <input id="Zip Code" type = "text" required><br>

    <label for="Country">Country:</label>
    <input id="Country" type = "text" required><br>

    <input type = "submit" value = "submit">

</form>
</div>

In your PHP you need to concatenate your variables outside of your strings. You need to close the string, concatenate the variable name, concatenate the new string and close it, concatenate the variable name and so forth. I've put in on multiple lines to make it easier to read:

$email_from = 'xxxx';
$email_subject = "New Submission";
    $email_body =   "New submission\nName:" . $name . 
                    "\nHome:" . $home . 
                    "\nCity:" . $city .
                    "\nState:" . $state .
                    "\nZip Code:" . $zip_code .
                    "\nCountry:" . $country";
$to =  "xxxx";
$headers = "xxxx";

In the future you'll also look in to santizing your strings when you read them from a form to protect your site/server.

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