Wordpress中的自定义表单操作

时间:2014-03-26 15:37:53

标签: wordpress forms

我使用了一段时间以来一直使用的自定义表单。表单通过保存在同一页面上的PHP进行验证,并将结果返回给同一页面上的用户。

我正在构建我的第一个WordPress网站,似乎表单操作有问题。我已经为此操作尝试了多个路径,但它返回到404页面,并且电子邮件无法发送。

我不确定这种表格方法是否适用于WP,或者我是否只是一个愚蠢的菜鸟。 (我打赌后来)。

我宁愿不使用表单插件,但如果所有其他方法都失败了,我可能不得不这样做。

非常感谢任何帮助

<?php

    /*
        Template Name:Contact Page
    */

?>

<?php
if($_POST)
{
$javascript_enabled = trim($_POST['browser_check']);
$contact_name = trim($_POST['name']);
$email = trim($_POST['email']);
$antispam = trim($_POST['AntiSpam']);
$msg = trim($_POST['msg']);
//mail settings
$enquiry_address = "blah@blah.com";
$headers = "From: ".$email;
$message = "Contact name: $contact_name\nContact Email: $email\nMessage: $msg";
$to = $enquiry_address;
$antispam_answer = "35";
    if ( $name == "" )
    {
        $result = "Please enter your NAME";
    }
    elseif (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)) 
    {
        $result = "Enter a valid EMAIL address";
    }
    elseif($antispam <> $antispam_answer) {
    $result = "The Anti-Spam answer is not correct.";
    }
    elseif ( strlen($msg) < 10 )
    {
        $result = "Message must be more than 10 characters";
    }
    else
    {           
            mail($to, $subject, $message, $headers);
        if( $selfcopy == "yes" )
            mail($email, $subject, $message, $headers);
        $result = "Your mail has been sent succesfully! Thanks for your enquiry";       
    }
}
    if($javascript_enabled == "true") {
        echo $result;
        die();
    }
?>

<?php get_header(); ?>

<div class="content">

    <div class="row">
        <div id="map"></div>
    </div>

    <div class="main2">

    <div class="row narrow clearfix">

        <div class="half">


            <form name="contactform" id="form" method="post" action="" class="clearfix">

                <div id="result"><?php if($result) echo "<div class='message'>".$result."</div>"; ?></div>

                <input type="text" class="text" placeholder="CONTACT NAME *" required name="name" value="<?php echo $contact_name; ?>" />

                <input type="email" class="text" placeholder="CONTACT EMAIL ADDRESS *" required name="email" value="<?php echo $email; ?>" />

                <input type="text" name="AntiSpam" class="text" maxlength="2" required placeholder="ANTISPAM - WHAT IS 20 + 15 ? *" />

                <textarea class="textarea" name="msg" placeholder="YOUR MESSAGE *" required value="<?php echo $msg; ?>" ></textarea>

                <input type="submit" name="submit" value="SEND" id="submit"/>

            </form>


            <script type="text/javascript">
                document.contactform.browser_check.value = "true"; //sets the hidden input(browser_check) value as true if the javascript is enabled.
                $("#submit").click(function(){
                $('#result').html('<img src="_images/loading.gif" class="loading-img" alt="loader image">').fadeIn();
                var input_data = $('#form').serialize();
                $.ajax({
                type: "POST",
                url:  "<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>",
                data: input_data,
                 success: function(msg){
                $('.loading-img').remove(); //Removing the loader image because the validation is finished
                $('<div class="message">').html(msg).appendTo('div#result').hide().fadeIn('slow'); //Appending the output of the php validation in the html div
                }                      
                }); 
                return false;
                }); 
            </script>
        </div><!--/half-->

        <div class="half last">

        </div><!--/half-->
    </div><!--/row-->
    </div><!--/main-->

</div><!--/content-->

<?php get_footer(); ?>

2 个答案:

答案 0 :(得分:2)

请使用以下代码帮助您

<form name="contactform" id="form" method="post" action="<?php echo  get_permalink($page_id); ?>" class="clearfix">


</form>

在表单操作中使用get_permalink()函数设置表单操作页面url。

答案 1 :(得分:1)

一个对我有用的解决方案:

在提交表单之前,如果您正在使用:

<input type="text" name="name" value=""> 

然后你必须用:

替换它
<input type="text" name="myname" value="">

name="name"停止WordPress中的表单操作...

相关问题