maddy maddy - 22 days ago 10
jQuery Question

Get div content value during print post data?

I am new to wordpress and php and I want to get div content data when I

print_r($_POST)


I used
add_action( 'save_post', 'wpse_save_meta_fields' );
wordpress hook to save data or update data.

When I do
print_r($_POST)
I get all things except selected_element_all div content.

How could I get data or resolve this ?

jquery code ( for get data from another file)

jQuery(document).ready(function() {
var postid = "<?php echo $post_id;?>";
jQuery("#sel_all_mmy").click(function() {
jQuery.ajax({
data: {
'post_id': postid
},
type: 'POST',
datatype: 'json',
url: "<?php echo get_template_directory_uri();?>/get_all_make_model.php?",
success: function(data) {
jQuery(".selected_element_all").append(data);
}
});
});
});




HTML Div where i append data

<div class="selected_element_all" style="font-size:15px"> </div>


And finally PHP code i used

<?php

function wpse_save_meta_fields( $post_id ) {
global $wpdb;

echo "<pre>";
print_r($_POST);
exit();
}

add_action( 'save_post', 'wpse_save_meta_fields' );
?>

Answer

Try like this way

take a input hidden field in your div just like

<input type=hidden name="my_val" class="my_val" value="">   </div>

then use it in your jquery script like

jQuery(document).ready(function(){
    var postid ="<?php echo $post_id;?>";
    jQuery("#sel_all_mmy").click(function(){
        jQuery.ajax({
        data:{'post_id':postid},
        type:'POST',
        datatype: 'json',
        url: "<?php echo get_template_directory_uri();?>/get_all_make_model.php?",
        success: function(data){
             jQuery(".selected_element_all").data(data);
             jQuery(".my_val").val(data);
            }
        });
    });
});

And print_r that value in your PHP function like print_r($_POST['my_val']);

I hope it's working.

Comments