我正在创建一个wordpress小部件(联系我们表格)。我想在数据库中发送表单数据

时间:2016-02-17 11:37:53

标签: php wordpress

这是我的HTML部分:

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

这是php部分:

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

单击提交按钮没有发生任何事情..只是到页面顶部。我搜索了所有重要的链接但没有找到。我错过了什么?

3 个答案:

答案 0 :(得分:1)

说明: 联系表格有4件事名称,电子邮件,留言,主题都是必填字段。您可以根据自己的要求进行修改。您可以使用此代码,在插件中写入并放置在插件文件夹中。

适用于打印前端表格

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

对于后端发送电子邮件给管理员

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您可以轻松打印

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

    return ob_get_clean();
}

add_shortcode( 'my_contact_form', 'contactf_shortcode' );

&GT;

答案 1 :(得分:0)

通过你在html中显示的内容,似乎你没有为Form声明正确的标签。定义表单在提交时将执行的操作。您的HTML代码需要一些调整。

答案 2 :(得分:0)

  

是的,我现在得到了解决方案.....

而不是行:

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

我写道:

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