简体   繁体   中英

Wordpress Homepage Form not working

I'm trying to make a contact form work on my website.

I'm using a script that already worked for me in the past for a php static website.

Now I'm trying to implement it on my wordpress homepage, and it doesn't seems to work.

When I submit, it redirects to the blog page, or to the "homepage" but with the blog entries instead.

I should be getting echo validation on submit but it doesn't happen.

I've tried with action="", action="#", PHP_SELF ,action="/", action="index", action="index.php", and no one works.

This is my code:

<?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';
    }
}

?>

And my HTML form

<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>

Try using the fully qualified url in the form action, ie, www.yourdomain.com/form-processor-script.php.

As a side note, you shouldn't generally have a form submitting to self - this is bad "form", as it allows users to resubmit the form data with the browser's refresh button. It also injects logic into your presentation layer. Look into the Post-Redirect-Get or PRG pattern.

After doing some research, I found that the best way to run the form action is to turn the form html and the processing into a WP plugin.

You can see it in the code below

<?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' );

?>

Hope it's useful for someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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