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