dotws86260 2016-03-03 18:25
浏览 52
已采纳

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

I am trying to get myContact Us form on my web project to work with php so that when the full name and email address is entered with comments it sends me an email and then displays a success message and if they fail to fill in the form them an error message displays saying for them to try again. My code is not working and I dont really understand why, it just displays with error message no matter what even when all info is entered. this is my php code below: -

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

?>

Can anyone please help me and advise on where I am going wrong.

I have made some changes to my code as suggested. I am at a loss I not really understanding why it isnt working, it is also not using the css style sheet. below is the new edited code: -

<?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条回答 默认 最新

  • donglao9606 2016-03-03 19:11
    关注

    UPDATE

    The problem was so small I missed it,

    you need to use .= not +=

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

    also remove the second occurrence of that code from the bottom.

    and close the form tag on your first block of html

    --original answer--

    Aside from missing the form tag, you can rewrite your if statements using a different sytnax, this allows you to type out html normally without having to echo it.

    Work around this example and it should work for you.

    <?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
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题