0

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);

?>
6
  • does not this: action = "https://localhost/xxxx/form-to-email.php" mean that it is supposed to go to your PHP file? Commented Jan 8, 2022 at 6:43
  • also, I believe each "input" tag requires the "name" attribute, not just the "label" tag. E.g.: <input name="city" type="text"> But I could be wrong, there are a lot of nuances in HTML Commented Jan 8, 2022 at 6:45
  • "Refuses to connect" confirm that you actually do have "form-to-email.php" in the "xxxx" main folder of your Document Root. Also confirm there are no syntax errors in that PHP code. Try running it from the command line. You may need to comment out the $_POST lines as those will only be effective from the web server. Commented Jan 8, 2022 at 6:46
  • What is the URL of the HTML file containing the form? Commented Jan 8, 2022 at 18:04
  • @UncaAlby Yes, it is supposed to, but the server is unable to connect to it. Also, my whole project, the project folder with html css and php is in the WAMP directory folder. Commented Jan 10, 2022 at 6:31

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

"In the future you'll also look in to santizing your strings when you read them from a form to protect your site/server." This is very true, but might I suggest that be done immediately in order to get into the habit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.