【问题标题】:Wordpress contact formWordPress 联系表格
【发布时间】:2013-02-17 06:19:50
【问题描述】:

我在我的 Wordpress 网站上有一个非常基本的联系表(硬编码),但我无法正常工作。它通过 XAMPP 在本地运行良好,我确信这是我刚刚缺少的东西,但任何帮助将不胜感激。提前致谢!。我也在使用我创建的模板

<?php /* Template Name: contact */?>

<?php get_header(); ?>

<?php

//vars declared to store form input
$name=$email=$comment=$phone="";
//Error vars - to relay error message to the form 
$nameError=$emailError=$commentError="";
$error_message="";
$sentMessage="";
$status=0; //Will monitor if all fields have no errors and increment if so.

function sanitise_var($string){
    htmlentities($string);
    strip_tags($string);
    return stripslashes($string);
}

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

if($_POST['name']==""){ 
    $nameError="Please enter a name";
    $error_message="Oops, error in the form. Please check";

}

else {
    $name=$_POST['name'];
    ++$status;

}

if($_POST['email'] == "" || !preg_match("/^[a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", $_POST['email'])){ 
    $error_message="Oops, error in the form. Please check";
    $emailError="Please enter a valid email";

}

else{
    $email=$_POST['email'];
    ++$status;
}

if(!$_POST['phone']=="") $phone=$_POST['phone'];

if($_POST['comment']==""){ 
    $error_message="Oops, error in the form. Please check";
    $commentError="Please enter a message";
}

else{
    $comment=$_POST['comment'];  
    ++$status;

 }


}//submitted if statement


if($status==3 && $_POST['submitted']){ 
    $sentMessage="From: $name, email: $email, Phone: $phone, Comment: $comment";
    wp_mail("mathornley@gmail.com", "From Android Scoop contact form", $sentMessage);
    echo "Thanks, your email was sent successfully!";
}

else{

echo<<<SOQ

<div class="entry-content">         

    <h1 class="entry-title">Contact</h1>

        <p class="contact">
            If you have a query drop us a line using the form below. We're always  happy to hear from people with ideas for posts and content they'd like to           feature or maybe write about. Or maybe you just have some feedback you'd like to share with us. Why not just swing by and say hello. 
        </p>       
        <p class="requiring">* Denotes required fields</p>       

        <div class="form_left">     

    <form action="/contact/" method="POST">
                <p><label>Name:</label><input type="text" name="name" value="$name"/></p> 
                <p class="error">$nameError</p> 
                <p><label>Email</label><input type="text" name="email" value="$email"/></p>
                <p class="error">$emailError</p> 
                <p><label>Phone:</label><input type="text" name="phone" value="$phone"/></p> 
                <input type="hidden" name="submitted" value="yes"/>
                <input type="submit" value="Send your message"/>                        
        </div>

        <div class="form_right">        
                <p><label>Message:</label><br/><textarea name="comment" rows="20" cols="20">$comment</textarea></p>
                <p class="error">$commentError</p> 
            </form>
        </div>
</div>

SOQ;

}
?> 

<?php get_footer();?>

【问题讨论】:

  • 错误是什么?不工作就是这么模糊
  • 对不起..是的...它检查表单正常但不会提交它,我得到一个页面未找到错误。我想知道这是否是由于 wordpress 造成的,因为它在我的本地服务器上本地运行良好。
  • 外星人先生是正确的 - 这应该有助于stackoverflow.com/questions/4369/…
  • 不,它没有,我对 PHP 很好,只是我觉得经验不足。我确实尝试过 $file = realpath($_SERVER["DOCUMENT_ROOT"] . "/contact/");这也失败了。我在挣扎。

标签: php wordpress forms contact


【解决方案1】:

我不知道 wordpress,但是按照一般的 PHP 规则,根据你在评论中回复我的内容,错误就在这里

<form action="/contact/" method="POST">
              ----^----

【讨论】:

  • 是的,我知道 php 表单的一般规则,这就是为什么本地一切正常,因为脚本再次运行以验证提交后的内容。我也尝试过发帖到 /contact/,the_permalink();和绝对网址只是为了确定并且没有工作。它在本地的工作方式似乎很奇怪。我想知道我是否遗漏了一些与 Wordpress 相关的内容。
【解决方案2】:

尝试使用空白值进行如下操作:

<form action="" method="POST">

如果这不起作用,请尝试将第一个输入字段的名称参数重命名为其他名称,例如:

<input type="text" name="myname" value="$name"/>

【讨论】:

  • 不..我已经尝试了各种方法,但似乎没有任何效果。它变得非常令人沮丧。这是页面androidscoop.co.uk/contact,我最后一次尝试是您的建议..
  • 我在上面的答案中又添加了一个建议。我在您的网站上尝试过,并且为您的第一个输入字段修改“名称”参数似乎有效。
  • 超级巨星!非常感谢,但我不确定这是如何解决的?对不起,因为我的愚蠢,我不能投票给你!。
  • 看起来“name”的变量名导致与 WordPress 发生冲突。
  • 啊,我明白了。无论如何,非常感谢您的时间!我很感激。我看看有没有其他人有同样的问题。我认为可能是最终提交表单时导致问题的 URL。无论如何都很棒。
【解决方案3】:

您为什么不使用一些开箱即用的 WordPress 联系表单?比如Contact Form 7就不错了。

Contact Form 7 是一款开源软件,可以管理多个联系表单,而且您可以通过简单的标记灵活地自定义表单和邮件内容。该表单支持基于 Ajax 的提交、验证码、Akismet 垃圾邮件过滤等。

安装

  • 将整个contact-form-7 文件夹上传到/wp-content/plugins/ 目录。
  • 通过“插件”菜单激活插件 WordPress。
  • 您会在 WordPress 管理员中找到“联系”菜单 面板。
  • 创建一个联系表单,将其复制为url,然后将其粘贴到您想要的任何位置。
  • 联系表将准确显示在其位置。

如果你想要一个例子,你可以check out this site看看它的外观和感觉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 2022-01-05
    • 2017-01-26
    • 2015-09-22
    相关资源
    最近更新 更多