HTML表单:指向空白PHP页面的操作

时间:2015-08-18 19:00:09

标签: javascript php html forms

编辑:道歉,我很忘记添加HTML。我把它包含在下面。

我是HTML的新手,我目前正在尝试合并一个"联系人"形成Weebly生成的网页。 HTML似乎注册了点击,但似乎JavaScript和PHP执行有问题。提交表单时,只需将表单指向空白的PHP页面即可。如果并非所有条目都已完成,则应该启动JavaScript。我的JavaScript如下:

注意:我的JS位于标题中。

<script>
    function has_id(id) {
        try {
            var tmp = document.getElementById(id).value;
        } catch (e) {
            return false;
        }
        return true;
    }

    function has_name(nm) {
        try {
            var tmp = cfrm.nm.type;
        } catch (e) {
            return false;
        }
        return true;
    }

    function $$(id) {
        if (!has_id(id) && !has_name(id)) {
            alert("Field " + id + " does not exist!\n Form validation configuration error.");
            return false;
        }
        if (has_id(id)) {
            return document.getElementById(id).value;
        } else {
            return;
        }
    }

    function $val(id) {
        return document.getElementById(id);
    }

    function trim(id) {
        $val(id).value = $val(id).value.replace(/^\s+/, '').replace(/\s+$/, '');
    }

    var required = {
        field: [],
        add: function(name, type, mess) {
            this.field[this.field.length] = [name, type, mess];
        },
        out: function() {
            return this.field;
        },
        clear: function() {
            this.field = [];
        }
    };
    var validate = {
        check: function(cform) {
            var error_message = 'Please fix the following errors:\n\n';
            var mess_part = '';
            var to_focus = '';
            var tmp = true;
            for (var i = 0; i < required.field.length; i++) {
                if (this.checkit(required.field[i][0], required.field[i][1], cform)) {} else {
                    error_message = error_message + required.field[i][2] + ' must be supplied\n';
                    if (has_id(required.field[i][0]) && to_focus.length === 0) {
                        to_focus = required.field[i][0];
                    }
                    tmp = false;
                }
            }

            if (!tmp) {
                alert(error_message);
            }
            if (to_focus.length > 0) {
                document.getElementById(to_focus).focus();
            }
            return tmp;
        },
        checkit: function(cvalue, ctype, cform) {
            if (ctype == "NOT_EMPTY") {
                if (this.trim($$(cvalue)).length < 1) {
                    return false;
                } else {
                    return true;
                }
            } else if (ctype == "EMAIL") {
                exp = /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
                if ($$(cvalue).match(exp) == null) {
                    return false;
                } else {
                    return true;
                }
            }
        },
        trim: function(s) {
            if (s.length > 0) {
                return s.replace(/^\s+/, '').replace(/\s+$/, '');
            } else {
                return s;
            }
        }
    };

    required.add('Email_Address', 'EMAIL', 'Email Address');
    required.add('Your_Message', 'NOT_EMPTY', 'Your Message');
</script>

我的HTML(抱歉它不在那里:/)

<form enctype="multipart/form-data" name="contactform" method="post" action="contactformprocess.php" onsubmit="return validate.check(this)">
    <div id="466589977852666939-form-parent" class="wsite-form-container" style="margin-top:10px;">
        <ul class="formlist" id="466589977852666939-form-list">
            <h2 class="wsite-content-title" style="text-align:left;">SEND US AN EMAIL</h2>

            <div>
                <div class="wsite-form-field" style="margin:5px 0px 5px 0px;">
                    <label class="wsite-form-label" for="Full_Name">Name </label>
                    <div class="wsite-form-input-container">
                        <input id="Full_Name" class="wsite-form-input wsite-input wsite-input-width-370px" type="text" name="Full_Name" maxlength="80" />
                    </div>
                    <div id="instructions-270584481949385592" class="wsite-form-instructions" style="display:none;"></div>
                </div>
            </div>

            <div>
                <div class="wsite-form-field" style="margin:5px 0px 5px 0px;">
                    <label class="wsite-form-label" for="Email_Address">Email <span class="form-required">*</span></label>
                    <div class="wsite-form-input-container">
                        <input id="Email_Address" class="wsite-form-input wsite-input wsite-input-width-370px" type="text" name="Email_Address" maxlength="100" />
                    </div>
                    <div id="instructions-270584481949385592" class="wsite-form-instructions" style="display:none;"></div>
                </div>
            </div>

            <div>
                <div class="wsite-form-field" style="margin:5px 0px 5px 0px;">
                    <label class="wsite-form-label" for="Your_Message">Your Message <span class="form-required">*</span></label>
                    <div class="wsite-form-input-container">
                        <textarea id="Your_Message" class="wsite-form-input wsite-input wsite-input-width-370px" name="Your_Message" style="height: 200px"></textarea>
                    </div>
                    <div id="instructions-137987539423347553" class="wsite-form-instructions" style="display:none;"></div>
                </div>
            </div>
        </ul>
    </div>
    <div style="display:none; visibility:hidden;">
        <input type="text" name="wsite_subject" />
    </div>
    <div style="text-align:left; margin-top:10px; margin-bottom:10px;">
        <input type='submit' style='position:absolute;top:0;left:-9999px;width:1px;height:1px' /><a class='wsite-button' onclick="document.contactform.submit(); return false"><span class='wsite-button-inner'>Submit</span></a>
    </div>
</form>

我的PHP位于名为 contactformprocess.php 的同一目录中。

<?php

if(isset($_POST['Email_Address'])) {

    $email_to = "foo@bar.com"; // your email address
    $email_subject = "Contact Form Message"; // email subject line
    $thankyou = "thankyou.htm"; // thank you page

    function died($error) {
        echo "Sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    if(!isset($_POST['Full_Name']) ||
        !isset($_POST['Email_Address']) ||
        !isset($_POST['Your_Message']) ||   
        ) {
        died('Sorry, there appears to be a problem with your form submission.');        
    }

    $full_name = $_POST['Full_Name']; // required
    $email_from = $_POST['Email_Address']; // required
    $comments = $_POST['Your_Message']; // required

    $error_message = "";

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(preg_match($email_exp,$email_from)==0) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\r\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Full Name: ".clean_string($full_name)."\r\n";
    $email_message .= "Email: ".clean_string($email_from)."\r\n";
    $email_message .= "Message: ".clean_string($comments)."\r\n";

$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
header("Location: $thankyou");
?>
<script>location.replace('<?php echo $thankyou;?>')</script>
<?php
}
die();
?>

感谢您的时间,感谢任何意见。

0 个答案:

没有答案