如何使我的表单使用PHP工作

时间:2018-01-20 15:32:25

标签: javascript php html

我不知道我做错了什么我尝试了一些PHP代码,但没有真正的工作我无法让我的表单发送电子邮件并在表单的同一页面显示成功消息。

我将我的网站上传到了byethost,它支持我看到的php,如果它很重要我也创建了数据库

此外,我的所有文件都是.php

这是我的PHP代码,它位于新文件调用联系表单上,我将其添加到表单代码上的action标记

    <?php
    $action=$_REQUEST['action'];
    if ($action=="") /** display the contact form */
    {
    ?>
        <?php
    }
    else/* send the submitted data */
    }
    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $message=$_REQUEST['message'];
    if (($name=="")||($email=="")||($message==""))
    {
    echo "All fields are required, please fill <a href=\"\">the form</a> again.";  
    }
    else
    $from="From: $name<$email>\r\nReturn-path: $email";
    $subject="Message sent using your contact form";
    mail("youremail@yoursite.com", $subject, $message, $from);
    echo "Email sent!";
    }
    }
    ?>

这是我的HTML代码,它位于填写调用Contact.php中:

<section id="articles"> <!-- A new section with the articles -->

<!-- Article 1 start -->  

<article id="article1"> <!-- The new article tag. The id is supplied so it can be scrolled into view. -->
    <h2>Contact </h2>
    <div class="line"></div> <!-- Dividing line -->
    <div class="articleBody clear">
    <div class="container">
    <form name="contactform" id="contact" action="contact-form.php" method="post" onsubmit="return checkform(this);">
    <h3 class="ContactForm">Message Me</h3>
    <h4>Message me today, and get reply with in 24-48 hours!</h4>

    <fieldset>
      <input name="name" id="name" placeholder="Your name" type="text" tabindex="1" title="At least 2 Character and only English Letters!"  pattern="[A-Za-z]{2,20}" required autofocus>
    </fieldset>

    <fieldset>
      <input name="email" id="email" placeholder="Your Email Address" type="email" title="Name@Example.com"  pattern="[A-Za-z0-9.-_]{2,25}@[a-zA-Z0-9]{2,25}[.]{1}[a-zA-Z]{2,5}[.]{1}[a-zA-Z]{2,4}$|[A-Za-z0-9.-_]{2,25}@[a-zA-Z0-9]{2,25}[.]{1}[a-zA-Z]{2,5}$" tabindex="2" required>
    </fieldset>

    <fieldset>
    <textarea name="message" id="message" placeholder="Type your Message Here...." tabindex="3" required></textarea>
    </fieldset> 

    <fieldset>
      <button name="submit" type="submit" id="submit" data-submit="...Sending">Submit</button>
    </fieldset>

    <!-- START CAPTCHA -->
    <br>
    <div class="capbox">

    <div id="CaptchaDiv"></div>

    <div class="capbox-inner">
    Type the above number:<br>

    <input type="hidden" id="txtCaptcha">
    <input type="text" name="CaptchaInput" id="CaptchaInput" size="15"><br>

    </div>
    </div>
    <br><br>
    <!-- END CAPTCHA -->
    </form>
    <span id="sucessMessage"> </span>

    <!-- Captcha Script -->
    <script>  
    function checkform(theform){
    var why = "";

    if(theform.CaptchaInput.value == ""){
    why += "- Please Enter CAPTCHA Code.\n";
    }
    if(theform.CaptchaInput.value != ""){
    if(ValidCaptcha(theform.CaptchaInput.value) == false){
    why += "- The CAPTCHA Code Does Not Match.\n";
    }
    }
    if(why != ""){
    alert(why);
    return false;
    }
    }

    var a = Math.ceil(Math.random() * 9)+ '';
    var b = Math.ceil(Math.random() * 9)+ '';
    var c = Math.ceil(Math.random() * 9)+ '';
    var d = Math.ceil(Math.random() * 9)+ '';
    var e = Math.ceil(Math.random() * 9)+ '';

    var code = a + b + c + d + e;
    document.getElementById("txtCaptcha").value = code;
    document.getElementById("CaptchaDiv").innerHTML = code;

    // Validate input against the generated number
    function ValidCaptcha(){
    var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
    var str2 = removeSpaces(document.getElementById('CaptchaInput').value);
    if (str1 == str2){
    return true;
    }else{
    return false;
    }
    }

    // Remove the spaces from the entered and generated code
    function removeSpaces(string){
    return string.split(' ').join('');
    }
    </script>

    </div>
    </div>
</article>

1 个答案:

答案 0 :(得分:0)

首先,您需要创建一个 form_handler.php 文件来处理您的表单, 然后你必须通过php将你的表单数据(之后已经提交)发送到那个页面,然后通过php在form_handler.php页面中接收表单数据并处理它!