Steve Kim
Steve Kim

Reputation: 5591

User meta updating giving no value

So, I have the following to update the user meta:

<?php if ( is_user_logged_in() ) { ?>
    <?php echo '<input class="input" type="text" id="rhc_phone" name="rhc_phone" placeholder="Phone number" value="' .$current_user -> rh_phone. '"/>' ;?>                          
    <?php update_user_meta( $current_user->ID, 'rh_phone', esc_attr( $_POST['rhc_phone'] ) ); ?>            
<?php }else{ ?>
        <input class="input" type="text" id="rhc_phone" name="rhc_phone" placeholder="Phone number" value=""/>
<?php } ?>

However when I click submit, there is no value (not even the previous value).

What am I doing wrong?

Thanks!

EDIT : This is my form which is sent via ajax:

<?php
    $current_user = wp_get_current_user();
    $rhcs_close = $_REQUEST['rhc_post_id'];//same as post id
?>
<div class="rh_contact_form">
   <div class="my_textfield rhc_phone_number_class">    
        <?php if ( is_user_logged_in() ) { ?> 
            <?php echo '<input class="my_textfield__input" type="text" id="rhc_phone" name="rhc_phone" placeholder="Phone number" value="' .$current_user -> billing_phone. '"/>' ;?>
        <?php }else{ ?>
            <input class="my_textfield__input" type="text" id="rhc_phone" name="rhc_phone" placeholder="Phone number" value=""/>
        <?php } ?>                                              
    </div>
    <div class="my_textfield rhc_ask_class">    
        <?php if ( is_user_logged_in() ) { ?> 
            <?php echo '<input class="my_textfield__input" type="text" id="rhc_ask" name="rhc_ask" placeholder="Ask me" value="' .$current_user -> my_question. '"/>' ;?>
        <?php }else{ ?>
            <input class="my_textfield__input" type="text" id="rhc_ask" name="rhc_ask" placeholder="Ask me" value=""/>
        <?php } ?>                                              
    </div>  
    <div class="rh_contact_button">         
        <button class="rh_contact_me">
            <i class="icons">done</i>
        </button>
    </div> 
</div>
<script type="text/javascript" >
function my_form_validate(element) {
    $('html, body').animate({scrollTop: $(element).offset().top-100}, 150);
    element.effect("highlight", { color: "#F2DEDE" }, 1500);
    element.parent().effect('shake');
  }

jQuery(document).ready(function($) {

$('body').on('click', '.rh_contact_me', function() {

    if($('#rhc_phone').val() === '') {
        my_form_validate($('#rhc_phone'));            

    } else if($('#rhc_ask').val() === '') {               
        my_form_validate($('#rhc_ask'));

    } else {
        var data = {
            'action': 'my_send_message',                
            'phone': $('#rhc_phone').val(),             
            'to_author': $('#rhc_ask').val()
        };

        var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
        $.post(ajaxurl, data, function(response) {
            if(response === 'success'){
                alert('Message Sent Successfully!');
                $('.rhp_close').click();
            }
        });
    } 
});
});
</script>

Functions.php

class MY_Posts {    
public static function my_send_message() {        
    if (isset($_POST['message'])) {            
        $to = $_POST['to_author'];
        $headers = 'From: My Page <' . $_POST['email'] . '>';
        $subject = "Hi";            
        ob_start();             
        ?>
        <div>
            Hi
        </div>          

        <?php           
        $message = ob_get_contents();            
        ob_end_clean();
        $mail = wp_mail($to, $subject, $message, $headers);            
        if($mail){
            echo 'success';
        }
    }        
    exit();        
}        
public static function my_mail_content_type() {
    return "text/html";
}
}

So, when the user inputs the "phone number" and "ask" fields in the form below, it sends a form to the admin (page author). But at the same time, I am trying to make it so that it updates these two fields (which are user meta keys).

I am not sure what the best way is to do so.

I am guessing two scenarios: ("phone number" field as an example)

Of course, if the user is not logged in, then there is no value to work with.

Upvotes: 0

Views: 243

Answers (1)

Script47
Script47

Reputation: 14530

You can't send an AJAX request to a class. What you need to do is have a handler file which will handle the AJAX request and will run the function if it receives the data.

handler.php

if (isset($_POST['someKeyBeingSentByAJAX'])) {
     // Run your function here.
}

You can't directly send an AJAX request to a class.

Proper Example

<?php
class Car {     
    public static function getCarType() {
        return "Super Car";
    }
}
?>

Then in your handler file,

<?php
require_once 'Car.php';

if(isset($_POST['getCarType'])) {
     $result = Car::getCarType();

     echo $result;
}
?>

You'd post your AJAX request to the handler, you could make specific handlers for each request or you could have a generic AJAX handler, however that file could get quite big and hard to maintain.

Upvotes: 1

Related Questions