简体   繁体   English

Wordpress联系表格

[英]Wordpress contact form

I've a very basic contact form on my Wordpress site (hard coded) and I can't get things to work. 我在Wordpress网站上有一个非常基本的联系表格(硬编码),我无法使用。 It works locally fine through XAMPP and I'm sure it's something I'm just missing but any help would be greatly appreciated. 它通过XAMPP在当地很好地工作,我确信这是我只是缺少的东西,但任何帮助将不胜感激。 Thanks in advance!. 提前致谢!。 I'm also using a template I created 我也在使用我创建的模板

<?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; SOQ;

}
?> 

<?php get_footer();?>

Try to use blank value for action like: 尝试使用空白值进行操作,例如:

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

If that doesn't work try renaming name parameter of the first input field to something else like: 如果这不起作用,请尝试将第一个输入字段的name参数重命名为:

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

Am not aware about the wordpress but as the general PHP rules go, and according to what you replied me in the comment, the error lies here 我不知道wordpress,但正如一般的PHP规则,并根据你在评论中回复我,错误在这里

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

Why won't you use some out-of-the-box contact form for WordPress? 为什么不为WordPress使用一些开箱即用的联系表格? For example, Contact Form 7 is pretty good. 例如, Contact Form 7非常好。

Contact Form 7 is an open source software and can manage multiple contact forms, plus you can customize the form and the mail contents flexibly with simple markup. 联系表格7是一个开源软件,可以管理多个联系表格,您还可以通过简单的标记灵活地自定义表格和邮件内容。 The form supports Ajax-powered submitting, CAPTCHA, Akismet spam filtering and so on. 该表单支持Ajax驱动的提交,CAPTCHA,Akismet垃圾邮件过滤等。

Installation 安装

  • Upload the entire contact-form-7 folder to the /wp-content/plugins/ directory. 将整个contact-form-7文件夹上载到/ wp-content / plugins /目录。
  • Activate the plugin through the 'Plugins' menu in WordPress. 通过WordPress中的“插件”菜单激活插件。
  • You will find 'Contact' menu in your WordPress admin panel. 您将在WordPress管理面板中找到“联系”菜单。
  • Create a contact form, copy it's url , paste it everywhere you want. 创建一个联系表单,复制它的url ,将其粘贴到您想要的任何地方。
  • Contact form will appear exactly in it's location. 联系表格将准确显示在其位置。

If you want an example, you can check out this site to see it's look & feel. 如果你想要一个例子,你可以查看这个网站 ,看看它的外观和感觉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM