开发者

Wordpress: saving form data to db

开发者 https://www.devze.com 2023-03-21 11:49 出处:网络
So I have a front form that a user fills out. It\'s mandatory, so replaces post content until they\'re filled it out. I want that saved into a table I\'ve created... I\'m missing something, but I\'m n

So I have a front form that a user fills out. It's mandatory, so replaces post content until they're filled it out. I want that saved into a table I've created... I'm missing something, but I'm not sure what.

Eventually I'll wrap the form in an if, to determine if the user's submitted the form or not, but for now, I'm just trying to get the bloody thing to work!

Thanks all.

<?php function make_user_feedback_form() {

    global $current_user;
    if ( is_user_logged_in() ) {

        $ufUserID = $current_user->ID;
        $ufResponses = serialize($_POST['responseFields']);
        if ( 'POST' == $_SERVER['REQUEST_METHOD'] &&开发者_Python百科; !empty( $_POST['action'] ) && $_POST['action'] == 'updateFeedback' ) {
            $ufDataUpdate = $wpdb->insert( 'wp_user_feedback', array( 'date' => current_time('mysql'), 'user' => $ufUserID, 'responses' => $ufResponses ) );
        }?>

    <ol>
        <form method="post">
            <li>Question 01<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 02<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 03<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 04<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 05<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 06<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 07<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 08<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 09<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 10<br /><input type="text" id="responseFields[]" value="" /></li>
            <li><input name="submit" type="submit" id="submit" class="submit button" value="Send feedback" /></li>
            <?php wp_nonce_field( 'updateFeedback' ); ?>
            <input name="action" type="hidden" id="action" value="updateFeedback" />
        </form>
    </ol>
    <?php }
}

add_action('the_content','make_user_feedback_form');
?>


Again, single quotes broke the serialize($_POST["responseFields"]) portion, I had to call $wpdb, and it seems like using name (as opposed to id) on fields is preferable? Anyway, the below code works.

<?php function make_user_feedback_form() {
    global $wpdb;
    global $current_user;
    if ( is_user_logged_in() ) {

        $ufUserID = $current_user->ID;
        $ufResponses = serialize($_POST["responseFields"]);
        if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'updateFeedback' ) {
            $ufDataUpdate = $wpdb->insert( 'wp_user_feedback', array( 'date' => current_time('mysql'), 'user' => $ufUserID, 'responses' => $ufResponses ) );
        }?>

    <ol>
        <form method="post">
            <li>Question 01<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 02<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 03<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 04<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 05<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 06<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 07<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 08<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 09<br /><input type="text" id="responseFields[]" value="" /></li>
            <li>Question 10<br /><input type="text" id="responseFields[]" value="" /></li>
            <li><input name="submit" type="submit" id="submit" class="submit button" value="Send feedback" /></li>
            <?php wp_nonce_field( 'updateFeedback' ); ?>
            <input name="action" type="hidden" id="action" value="updateFeedback" />
        </form>
    </ol>
    <?php }
}

add_action('the_content','make_user_feedback_form');
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消