Mukii kumar
Mukii kumar

Reputation: 179

I am creating a wordpress widget (Contact Us form). I want to send the form data in database

Here is my HTML part:

<input type="text" name="wd-name" />
<input type="submit" value="Send Message" name="send" />

Here is the php part:

function db_connect(){
global $wpdb;
$table_name = $wpdb->prefix . "test"; //'test' is table name

if( $_POST["send"] != '' && $_POST["wd-name"] != ''){
    $table_name = $wpdb->prefix ."test";
    $name = strip_tags($_POST['wd-name'], '');
    $wpdb->insert(
            $table_name,
                array(
                    'name' => $name
                )
            );
   }
}

if( isset($_POST['send']) ) db_connect();
register_activation_hook(__FILE__, 'db_connect');

on click the submit button nothing is happening..it just going to top of the page. I searched all the important links but nothing found. What thing am I missing?

Upvotes: 0

Views: 86

Answers (3)

Mukii kumar
Mukii kumar

Reputation: 179

Yeah i got the solution now.....

instead of lines:

$wpdb->insert(
        $table_name,
            array(
                'name' => $name
            )
        );
////Code...
if( isset($_POST['send']) ) db_connect();
register_activation_hook(__FILE__, 'db_connect');

I wrote:

$wpdb->insert(
        'test',
            array(
                'name' => $name
            )
        );
add_shortcode('shortcode_name', 'db_connect');

Upvotes: 0

Vasim Shaikh
Vasim Shaikh

Reputation: 4532

Description : Contact form have 4 things Name,email,Message,subject all are required field here.You can modify as per you requirement.You can use this code,write in plugin and place inside plugin folder.

For print frontend form

function html_form_code() {
    echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
    echo '<p>';
    echo 'Your Name (required) <br/>';
    echo '<input type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '" size="40" />';
    echo '</p>';
    echo '<p>';
    echo 'Your Email (required) <br/>';
    echo '<input type="email" name="cf-email" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '" size="40" />';
    echo '</p>';
    echo '<p>';
    echo 'Subject (required) <br/>';
    echo '<input type="text" name="cf-subject" pattern="[a-zA-Z ]+" value="' . ( isset( $_POST["cf-subject"] ) ? esc_attr( $_POST["cf-subject"] ) : '' ) . '" size="40" />';
    echo '</p>';
    echo '<p>';
    echo 'Your Message (required) <br/>';
    echo '<textarea rows="10" cols="35" name="cf-message">' . ( isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : '' ) . '</textarea>';
    echo '</p>';
    echo '<p><input type="submit" name="cf-submitted" value="Send"></p>';
    echo '</form>';
}

For Backend Send email to admin

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 = get_option( 'admin_email' );

        $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 contact will get back to you very soon.</p>';
            echo '</div>';
        } else {
            echo 'An unexpected error occurred';
        }
    }
}

Genrate Shortcode Its easy where you want to print

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

    return ob_get_clean();
}

add_shortcode( 'my_contact_form', 'contactf_shortcode' );

?>

Upvotes: 1

BeeFaauBee
BeeFaauBee

Reputation: 358

By what you've shown in your html, it seems you didn't declare proper tags for Form. Define action as to what your form will do on submit. Your HTML code needs some tweaks.

Upvotes: 0

Related Questions