Wordpress主页表单不起作用

时间:2016-09-06 20:40:24

标签: php html wordpress forms

我试图在我的网站上制作联系表格。

我使用的脚本过去曾为我的php静态网站工作过。

现在我试图在我的wordpress主页上实现它,它似乎无法正常工作。

当我提交时,它会重定向到博客页面,或者重定向到"主页"但是改为使用博客条目。

我应该在提交时获得回声验证,但它不会发生。

我尝试过动作="",action ="#",PHP_SELF,action =" /",action =& #34; index",action =" index.php",没有人工作。

这是我的代码:

<?php

$send = $_POST['send']

// Address to get the mails
$EmailTo = "sebastianferreira58@gmail.com";


// Form input fields
$name = $_POST['name']; 
$email = $_POST['email']; 

$subject = explode('|', $_POST['dropdown']);;

$message = $_POST['message']; 

if (isset($send)) {

    if (empty($email)) {
        $output = 'Hey, I need your email to reply';
    }

    elseif (empty($name)) {
        $output = 'Ups, you forgot your name';
    }

    else{
        mail("sebastianferreira58@gmail.com", $subject, $message, "From: $email\n");
        $output = 'Thanks, your submission has been sent';
    }
}

?>

我的HTML表单

<form method="post" action="">

                <p><?php echo $output; ?></p>

                <label for="name">Your beautiful name goes here</label>
                <input type="text" name="name" id="name">

                <label for="mail">I need your email to write you back, type it below</label>
                <input type="email" name="mail" id="mail">

                <label for="dropdown">Are you looking for help in something specific or just wanted to say HI?</label>
                <select name="dropdown" id="dropdown">
                    <option>Just wanted to say Hi!</option>
                    <option>I need help with my brand</option>
                    <option>I want to launch a website</option>
                    <option>Other</option>
                </select>

                <label for="else">Is there something else you want to tell me?</label>
                <textarea name="else" id="else">

                </textarea>

                <button class="btn btn-red" name="send" type="submit">SEND</button>

            </form>

2 个答案:

答案 0 :(得分:0)

尝试在表单操作中使用完全限定的url,即www.yourdomain.com/form-processor-script.php。

作为旁注,你通常不应该有一个表单提交给自己 - 这是不好的&#34;形式&#34;,因为它允许用户使用浏览器重新提交表单数据刷新按钮。它还为您的表示层注入逻辑。查看Post-Redirect-Get或PRG模式。

答案 1 :(得分:0)

经过一些研究后,我发现运行表单操作的最佳方法是将表单html和处理转换为WP插件。

您可以在下面的代码中看到它

<?php

/*
    Plugin Name: Contact Form Plugin
    Plugin URI: http://ferrius.co
    Description: Simple non-bloated WordPress Contact Form
    Version: 1.0
    Author: Sebastian Ferreira
    Author URI: http://ferrius.co
*/


function html_form_code() {

    echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
    echo '<br>';

    echo '<label for="cf-name">Your beautiful name goes here</label>';
    echo '<input type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '"/>';

    echo '<label for="cf-email">I need your email to write you back, type it below</label>';
    echo '<input type="email" name="cf-email" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '"/>';

    echo '<label for="cf-subject">Are you looking for help in something specific or just wanted to say HI?</label>';
    echo '<input type="text" name="cf-subject" pattern="[a-zA-Z ]+" value="' . ( isset( $_POST["cf-subject"] ) ? esc_attr( $_POST["cf-subject"] ) : '' ) . '" />';

    echo '<label for="cf-message">Is there something else you want to tell me?</label>';
    echo '<textarea name="cf-message">' . ( isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : '' ) . '</textarea>';

    echo '<button class="btn btn-red" type="submit" name="cf-submitted" value="SEND"/>SEND</button>';
    echo '</form>';

}

function deliver_mail() {

    // if the submit button is clicked, send the email
    if ( isset( $_POST['cf-submitted'] ) ) {

        // sanitize form values
        $name    = sanitize_text_field( $_POST["cf-name"] );
        $email   = sanitize_email( $_POST["cf-email"] );
        $subject = sanitize_text_field( $_POST["cf-subject"] );
        $message = esc_textarea( $_POST["cf-message"] );

        // get the blog administrator's email address
        $to = "sebastianferreira58@gmail.com";

        $headers = "From: $name <$email>" . "\r\n";

        // If email has been process for sending, display a success message
        if ( wp_mail( $to, $subject, $message, $headers ) ) {
            echo '<div>';
            echo '<p>Thanks for contacting me, expect a response soon.</p>';
            echo '</div>';
        } else {
            echo 'An unexpected error occurred';
        }
    }
}

function cf_shortcode() {
    ob_start();
    deliver_mail();
    html_form_code();

    return ob_get_clean();
}

add_shortcode( 'ferrius_contact_form', 'cf_shortcode' );

?>

希望它对某人有用。