1

I have a form in my index.php which requires user email and name. After he submits the form, I save the user to database. User gets saved and can be found on both wp_users and wp_usermeta. Now when that new created user goes to my quiz page, and takes the quiz test and collects points, I want that quiz score to be saved in wp_usermeta table row. I'm also using $_SESSION, but I think it's not working. I must mention that I can save the score on its own without the user being created. This is the code I wrote yesterday that is working But I want now the score to be saved for new registered user Can't save ajax value to database This is the code I have now and I think it should be working, but no luck.

<form action="index.php" method="post">
                                        <input type="text" placeholder="Enter Email" name="email_contact" required>
                                        <input type="text" placeholder="Enter full name" name="fullname_contact" required>


                                    </div>
                                         <button type="submit" name="submit_contact" class="signupbtn">Start quiz</button>
                                     </form>
                                     <?php
                                     if (isset($_POST['submit_contact'])) {
                                       $email = $_POST['email_contact'];
                                       $full = $_POST['fullname_contact'];

                                        $_SESSION['name'] = register_new_user( $full, $email );

                                       }

                                         echo $_SESSION['name'];

                                      ?>


                                </div>.....

functions.php

function save_quiz_score() {


     $score = $_POST['score'];

$currentUser = get_current_user_id();

    update_user_meta($currentUser, "first_name", $score, $score);

    //update_user_meta($_SESSION['name'], "first_name", $score, $score);

     //session_destroy();
 }

add_action('wp_ajax_nopriv_save_score', 'save_quiz_score');
add_action('wp_ajax_save_score', 'save_quiz_score');

As you can see, the $score doesn't get added to first_name row enter image description here

  • 1
    i think it's because you're using the same value for the new and previous values. – inarilo Dec 12 '17 at 18:55
  • If $_POST['score'] is correct and everything else is working, just the meta save - as @inarilo said, it's definitely because of your 4th parameter, $score, on [update_user_meta()](https://codex.wordpress.org/Function_Reference/update_user_meta), right on the docs it says "..will return false if the previous value is the same as new value" which you have it as. I would remove the 4th parameter, its optional and not needed in this case. – David Sword Dec 13 '17 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.