1

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'] && !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');
?>
5
  • Of course; needed to call global $wpdb; But the form fields are not being posted. Hmm... Commented Jul 20, 2011 at 14:22
  • Ok, after using name in the fields (I thought that was antiquated?) it seems to be working just fine. Commented Jul 20, 2011 at 14:46
  • Oh also, apparently serialize($_POST['responseFields']) needed to use double quotes... Commented Jul 20, 2011 at 14:59
  • IF you solved this yourself, then post an answer to your own question and accept it please. This keeps people from reading through an already solved problem. Commented Jul 21, 2011 at 0:58
  • Bro, I tried to but I had to wait to answer my own question. Commented Jul 21, 2011 at 12:48

1 Answer 1

2

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');
?>
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for perseverance, eh! I didn't know you had to wait before answering your own. Maybe you need to generate rep before you can do it right away.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.