通过电子邮件发送的联系表格

时间:2016-03-03 18:25:31

标签: php html

我正在尝试在我的网络项目上获取myContact Us表单以使用php,这样当输入完整的姓名和电子邮件地址时,它会向我发送一封电子邮件,然后显示成功消息,如果他们无法填写形成一个错误消息显示说他们再试一次。我的代码不起作用,我真的不明白为什么,它只是显示错误信息,无论什么甚至输入所有信息。这是我的PHP代码: -

`<?php`

$name = $email = "";

if (isset ($_POST ['fullname']))
    $name = fix_string ($_POST ['fullname']);
    if (isset ($_POST ['email'])) $email = fix_string ($_POST ['email']);

    $email_to = "my@eamiladdress.com";

    $email_subject = "Question asked";

    echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
        'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
        <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
            <head>
                <link href='myelearningtool.css' rel='stylesheet' type='text/css' />
                <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
                <title>Registration Confirmation E-Learning Tool Prototype</title>
                <div class='page'>
                    <div class='header'>
                        <div id='logo'>
                            <img src='images/BS_kids_ELearningToollogo.png' alt='E-Learning Tool Prototype v1' />
                        </div>
                        <ul id='navigation'>
                            <li><a href='****************.html'>Logout</a></li>
                            <li><a href='**************.html'>Activities</a></li>
                            <li><a href='****************.html'>Videos</a></li>
                            <li><a href='****************.html'>Glossary</a></li>
                            <li><a href='********'>Contact Us</a></li>
                        </ul>
                    </div><!--end of header div -->
                    <div class='content'><!--start of content -->
                        <div class='contact_details'>
                            <p>Thanks for that, someone will email you back as soon as possible.</P>
                            </div><!--end of contact details-->
                        </div>
                    </div><!--end of content div-->
                </div>
            </div>
        </html>"; 

$fail  = validate_fullname($name);
$fail += validate_email($email);

if ($fail == ""){
    echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
        'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
        <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
            <head>
                <link href='myelearningtool.css' rel='stylesheet' type='text/css' />
                <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
                <title>Registration Confirmation E-Learning Tool Prototype</title>
                <div class='page'>
                    <div class='header'>
                        <div id='logo'>
                            <img src='images/BS_kids_ELearningToollogo.png' alt='E-Learning Tool Prototype v1' />
                        </div>
                        <ul id='navigation'>
                            <li><a href='****************.html'>Return To Home Page</a></li>
                        </ul>
                    </div><!--end of header div -->
                    <div class='content'><!--start of content -->
                        <p>Ooops yous missed something off.  Please try again</p>
                        <fieldset><!--details section-->
                            <legend><b>Contact Us</b></legend>
                            <label for='name'> Your Full Name*:</label> <input type='text' name='fullname' id='name' maxlength='50' size='30' tabindex='1' accesskey='q' />
                            <label for='email'>Email Address*:</label><input type='text' name='email' id='email' maxlength='50' size='30' tabindex='2' accesskey='w' />
                        </fieldset><!--end of fieldset-->
                    <div class='message'>
                        <fieldset>
                            <legend>Message*</legend>
                            <textarea name='message' id='msge' cols='130' rows='10'>Please type your question or query here</textarea>
                        </fieldset>
                        <p><input type='submit' value='Enter' /></p>
                    </div><!--end of content div-->
                </div>
            </div>
        </html>";
}

function validate_fullname($name){
    return ($name == "")? "No name was entered<br>": "";
}
function validate_email($email){
    if ($email == "")
        return "No email was entered<br>";
    else if (!((strpos($email, ".") > 0) && (strpos($email, "@") > 0)) || preg_match ("/[^a-z A-Z 0-9.@_-]/", $email))
        return "The Email address is invalid<br>";
    return "";
}
function fix_string($string){
    if (get_magic_quotes_gpc()) $string = stripslashes($string);
        return htmlentities ($string);
}

?>

任何人都可以帮助我,并告诉我哪里出错了。

我已根据建议对代码进行了一些更改。我不知道我真的不明白为什么它不起作用,它也没有使用css样式表。下面是新编辑的代码: -

<?php

    if (isset($_POST ['fullname']) || isset($_POST ['email'])):
        $name = fix_string($_POST ['fullname']);
        $email = fix_string($_POST ['email']);

        $email_to = "my@email.com";
        $email_subject = "My E-Learning Tool Query";

        $fail  = validate_fullname($name);
        $fail += validate_email($email);

        if($fail!=""):
?>
    <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
    'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
    <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
        <head>
            <link href='myelearningtool.css' rel='stylesheet' type='text/css' />
            <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
            <title>Contact Us E-Learning Tool Prototype</title>
            <div class='page'>
                <div class='header'>
                    <div id='logo'>
                        <img src='images/BS_kids_ELearningToollogo.png' alt='E-Learning Tool Prototype v1' />
                    </div>
                    <ul id='navigation'>
                        <li><a href='http://******/myelearningtool.html'>Return To Home Page</a></li>
                    </ul>
                </div><!--end of header div -->
                <div class='ContactUs'><!--start of content -->
                    <p>Ooops yous missed something off.  Please try again</p>
                    <form method='post' action='php/contactUs.php' onsubmit='return validation(this)' ><!-- Start of the form to be submitted -->
                        <fieldset><!--details section-->
                            <legend><b>Contact Us</b></legend>
                            <label for='name'> Your Full Name*:</label> <input type='text' name='fullname' id='name' maxlength='50' size='30' tabindex='1' accesskey='q' />
                            <label for='email'>Email Address*:</label><input type='text' name='email' id='email' maxlength='50' size='30' tabindex='2' accesskey='w' />
                        </fieldset><!--end of fieldset-->
                    <div class='message'>
                        <fieldset>
                            <legend>Message*</legend>
                            <textarea name='message' id='msge' cols='130' rows='10'>Please type your question or query here</textarea>
                        </fieldset>
                        <p><input type='submit' value='Enter' /></p>
                    </div>
                </div><!--end of contact us div-->
            </div>
        </div>
    </html>
<?php 
    else:
?>      
    <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
    'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
    <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
        <head>
            <link href='myelearningtool.css' rel='stylesheet' type='text/css' />
            <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
            <title>Registration Confirmation E-Learning Tool Prototype</title>
            <div class='page'>
                <div class='header'>
                    <div id='logo'>
                        <img src='images/BS_kids_ELearningToollogo.png' alt='E-Learning Tool Prototype v1' />
                    </div>
                    <ul id='navigation'>
                        <li><a href='http://******myelearningtool.html'>Logout</a></li>
                        <li><a href='http://******/myelearningtoolactivities.html'>Activities</a></li>
                        <li><a href='http://******t/myelearningtoolVideos.html'>Videos</a></li>
                        <li><a href='http://******t/myelearningtoolglossary.html'>Glossary</a></li>
                        <li><a href=' '>Contact Us</a></li>
                    </ul>
                </div><!--end of header div -->
                <div class='content'><!--start of content -->
                    <div class='ContactUs'>
                        <p>Thanks for that, someone will email you back as soon as possible.</P>
                    </div><!--end of contact us-->
                </div><!--end of content div-->
            </div>
        </div>
    </html>
<?php
    endif;
?>

<?php 
    else:
?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

    <head>
        <link href="myelearningtool.css" rel="stylesheet" type="text/css" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Contact Us Page for e-Learning Tool Prototype</title><!--this is the title of the page, I have chosen this title as it states exactly what the web page is -->
    </head>
    <body>
        <div class="page">
            <div class="header"><!--start of the header which will contain the banner and navigation-->
                <ul id="navigation" style="text-align: center">
                    <li><a href="http://******/myelearningtool.html">Home Page</a></li>
                    <li><a href="">Activites</a></li>
                    <li><a href="">Videos</a></li>
                </ul>
                <div id="logo" style="text-align: center">
                    <img src="images/BS_kids_ELearningToollogo.png" alt="E-Learning Tool Prototype v1" /><!--logo created in photoshop-->
                </div><!--end of logo div-->
            </div><!--end of header div -->
            <div class="ContactUs">
                <form method="post" action="php/contactUs.php" onsubmit="return validation(this)" ><!-- Start of the form to be submitted -->
                    <fieldset><!--details section-->
                        <legend><b>Contact Us</b></legend>
                        <label for="name"> Your Full Name*:</label> <input type="text" name="fullname" id="name" maxlength="100" size="30" tabindex="1" accesskey="q" />
                        <label for="email">Email Address*:</label><input type="text" name="email" id="email" maxlength="50" size="30" tabindex="2" accesskey="w" />
                    </fieldset><!--end of fieldset-->
                    <div class="message">
                        <fieldset>
                            <legend>Message*</legend>
                            <textarea name="message" id="msge" cols="130" rows="10">Please type your question or query here</textarea>
                        </fieldset>
                        <p><input type="submit" value="Enter" /></p>
                    </div>
                </form>
            </div><!--end of div-->
        </div><!--end of page div-->
    </body><!--end of body div-->
</html><!--end of html div-->

<?php

endif;



    $fail  = validate_fullname($name);
    $fail += validate_email($email);

function validate_fullname($name){
    return ($name == "")? "No name was entered<br>": "";
}
function validate_email($email){
    if ($email == "")
        return "No email was entered<br>";
    else if (!((strpos($email, ".") > 0) && (strpos($email, "@") > 0)) || preg_match ("/[^a-z A-Z 0-9.@_-]/", $email))
        return "The Email address is invalid<br>";
    return "";
}
function fix_string($string){
    if (get_magic_quotes_gpc()) $string = stripslashes($string);
        return htmlentities ($string);
}

?>

1 个答案:

答案 0 :(得分:0)

<强>更新

问题太小我错过了,

您需要使用.=而不是+=

$fail  = validate_fullname($name);
$fail .= validate_email($email);

还从底部删除第二次出现的代码。

并关闭第一个html块上的表单标记

- 原始答案 -

除了遗漏form标签之外,您可以使用不同的sytnax重写if语句,这样您就可以正常输入html而无需回复它。

解决这个例子,它应该适合你。

<?php

if (isset($_POST ['fullname']) || isset($_POST ['email'])):
    $name = fix_string($_POST ['fullname']);
    $email = fix_string($_POST ['email']);

    $email_to = "my@eamiladdress.com";
    $email_subject = "Question asked";

    $fail  = validate_fullname($name);
    $fail .= validate_email($email);

    if($fail!=""):
?>
    <!--Your error html-->
<?php 
    else:
?>
    <!--Your success html-->
<?php
    endif;
?>

<?php 
else:
?>

<!--Your form html-->

<?php
endif;

//Your functions

?>