繁体   English   中英

PHP 的 WAMP 问题

[英]WAMP Issue With PHP

请原谅我糟糕的格式,我正在自学 html、css 和 php。

我的 WAMP www 文件夹中有一个文件夹,其中包含 html、css 和 php。 我正在尝试将表单中输入的信息发送到 email 地址。 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." 此外,好吧,一切,我在这里做错了什么?

这是 html 文件

<!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>

这是 php 文件

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

?>

在 HTML 中,当您使用label时,您必须提供与在 label 中使用的完全相同的 id。

您还可以通过在每个必需的输入字段中放置所需的属性来让浏览器进行检查。

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

在您的 PHP 中,您需要在字符串之外连接变量。 您需要关闭字符串,连接变量名,连接新字符串并关闭它,连接变量名等等。 我已经添加了多行以使其更易于阅读:

$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";

将来,当您从表单中读取字符串以保护您的站点/服务器时,您还将考虑对字符串进行消毒。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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