admin管理员组文章数量:1325236
I have a PHP script that gets details from a checkbox and a text field. The console.log data is correct and so is the updated data in the database, however, when I try and get the data, using $wpdb or get_user_meta(), I receive incorrect data (i receive previously updated data, even though in the database te data is different).
Here's my code:
//Form
<form id="donations" method="POST" >
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$option = get_the_title();
$id = get_the_ID();
?>
<input type="radio" class="charity" id="charity-<?php echo $id ?>" name="charity" value="<?php echo $id ?>" <?php if ($_POST[$id] == $id) echo 'checked'; ?> >
<label for="<?php echo $id ?>"><?php echo $option; ?></label>
<br>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<div class="more-don--wrapper">
<label for="more_donation">The standard card cost is R50.00. <br>Enter a different amount below if you would like to donate more (must be more than R50.00).</label>
<input type="number" min="60" step="10" placeholder="50.00" id="more_donation" name="more_donation" value="<?php if (isset($_POST['more-donation'])){ echo $_POST['more-donation']; }else{ echo '50'; } ?>"/>
<span class="more_donation_tc">All additional amount will go directly to the nominated charity.</span>
</div>
<!--<input class="button" type="submit" value="Update Selection" />-->
</form>
//Ajax script
jQuery( 'document' ).ready( function( $ ) {
// Form submission listener
//$( '#donations' ).submit( function() {
$('.choose-charity').click(function() {
// Grab our post meta value
var charity = $( '#donations .charity:checked' ).val();
var more_donation = $( '#donations #more_donation' ).val();
// Do very simple value validation
//if( $( '#donations #charity #donation' ).val().length ) {
$.ajax( {
url : ajax_url, // Use our localized variable that holds the AJAX URL
type: 'POST', // Declare our ajax submission method ( GET or POST )
data: { // This is our data object
action: 'um_donation', // AJAX POST Action
'charity': charity, // Replace `um_key` with your user_meta key name
'more_donation': more_donation,
}
})
.success( function( results ) {
console.log( 'User Meta Updated! ' + charity + ' ' + more_donation );
})
.fail( function( data ) {
console.log( data.responseText );
console.log( 'Request failed: ' + data.statusText );
});
//} else {
// Show user error message.
//}
return false; // Stop our form from submitting
});
});
// Data update script
function donations_callback() {
// Ensure we have the data we need to continue
if( ! isset( $_POST ) || empty( $_POST ) || ! is_user_logged_in() ) {
// If we don't - return custom error message and exit
header( 'HTTP/1.1 400 Empty POST Values' );
echo 'Could Not Verify POST Values.';
exit;
}
$user_id = get_current_user_id(); // Get our current user ID
$charity = sanitize_text_field( $_POST['charity'] ); // Sanitize our user meta value
$more_donation = sanitize_text_field( $_POST['more_donation'] ); // Sanitize our user email field
if( !empty($charity) ) {
update_user_meta( $user_id, 'charity', $charity ); // Update our user meta
}else {
update_user_meta( $user_id, 'charity', '' );
}
if( !empty($more_donation) ) {
update_user_meta( $user_id, 'more_donation', $more_donation ); // Update our user meta
} else {
update_user_meta( $user_id, 'more_donation', '' );
}
//update_field('charity', $charity, 'user_'.$user_id.'');
//update_field('more_donation', $more_donation, 'user_'.$user_id.'');
exit;
}
add_action( 'wp_ajax_nopriv_um_donation', 'donations_callback' );
add_action( 'wp_ajax_um_donation', 'donations_callback' );
And this is my loop in a tabular form. The first tab is where the form (above) is and the last tab is where I retrieve the data and the code is as follows:
<?php
// get saves meta values (charity and amount)
/*
$charity_id = esc_html( get_user_meta( $user_id, 'charity' , true ) );
$charity = get_the_title( $charity_id );
$donation_amount = esc_html( get_user_meta( $user_id, 'more_donation' , true ) );
*/
global $wpdb;
// get charity id
$charity_meta_key = "charity";
$charity_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT meta_value
FROM $wpdb->usermeta
WHERE user_id = %d
AND meta_key = %s",
$user_id,
$charity_meta_key
)
);
// get charity name
$charity = get_the_title( $charity_id );
// get donation amount
$donation_meta_key = "more_donation";
$donation_amount_db = $wpdb->get_var(
$wpdb->prepare(
"SELECT meta_value
FROM $wpdb->usermeta
WHERE user_id = %d
AND meta_key = %s",
$user_id,
$donation_meta_key
)
);
$donation_amount = $donation_amount_db;
?>
The commented out get_user_meta returns the same output as the $wpdb. How would I go about solving my issue - it is a cache issue? Any point in the right direction will be greatly appreciated.
I have a PHP script that gets details from a checkbox and a text field. The console.log data is correct and so is the updated data in the database, however, when I try and get the data, using $wpdb or get_user_meta(), I receive incorrect data (i receive previously updated data, even though in the database te data is different).
Here's my code:
//Form
<form id="donations" method="POST" >
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$option = get_the_title();
$id = get_the_ID();
?>
<input type="radio" class="charity" id="charity-<?php echo $id ?>" name="charity" value="<?php echo $id ?>" <?php if ($_POST[$id] == $id) echo 'checked'; ?> >
<label for="<?php echo $id ?>"><?php echo $option; ?></label>
<br>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<div class="more-don--wrapper">
<label for="more_donation">The standard card cost is R50.00. <br>Enter a different amount below if you would like to donate more (must be more than R50.00).</label>
<input type="number" min="60" step="10" placeholder="50.00" id="more_donation" name="more_donation" value="<?php if (isset($_POST['more-donation'])){ echo $_POST['more-donation']; }else{ echo '50'; } ?>"/>
<span class="more_donation_tc">All additional amount will go directly to the nominated charity.</span>
</div>
<!--<input class="button" type="submit" value="Update Selection" />-->
</form>
//Ajax script
jQuery( 'document' ).ready( function( $ ) {
// Form submission listener
//$( '#donations' ).submit( function() {
$('.choose-charity').click(function() {
// Grab our post meta value
var charity = $( '#donations .charity:checked' ).val();
var more_donation = $( '#donations #more_donation' ).val();
// Do very simple value validation
//if( $( '#donations #charity #donation' ).val().length ) {
$.ajax( {
url : ajax_url, // Use our localized variable that holds the AJAX URL
type: 'POST', // Declare our ajax submission method ( GET or POST )
data: { // This is our data object
action: 'um_donation', // AJAX POST Action
'charity': charity, // Replace `um_key` with your user_meta key name
'more_donation': more_donation,
}
})
.success( function( results ) {
console.log( 'User Meta Updated! ' + charity + ' ' + more_donation );
})
.fail( function( data ) {
console.log( data.responseText );
console.log( 'Request failed: ' + data.statusText );
});
//} else {
// Show user error message.
//}
return false; // Stop our form from submitting
});
});
// Data update script
function donations_callback() {
// Ensure we have the data we need to continue
if( ! isset( $_POST ) || empty( $_POST ) || ! is_user_logged_in() ) {
// If we don't - return custom error message and exit
header( 'HTTP/1.1 400 Empty POST Values' );
echo 'Could Not Verify POST Values.';
exit;
}
$user_id = get_current_user_id(); // Get our current user ID
$charity = sanitize_text_field( $_POST['charity'] ); // Sanitize our user meta value
$more_donation = sanitize_text_field( $_POST['more_donation'] ); // Sanitize our user email field
if( !empty($charity) ) {
update_user_meta( $user_id, 'charity', $charity ); // Update our user meta
}else {
update_user_meta( $user_id, 'charity', '' );
}
if( !empty($more_donation) ) {
update_user_meta( $user_id, 'more_donation', $more_donation ); // Update our user meta
} else {
update_user_meta( $user_id, 'more_donation', '' );
}
//update_field('charity', $charity, 'user_'.$user_id.'');
//update_field('more_donation', $more_donation, 'user_'.$user_id.'');
exit;
}
add_action( 'wp_ajax_nopriv_um_donation', 'donations_callback' );
add_action( 'wp_ajax_um_donation', 'donations_callback' );
And this is my loop in a tabular form. The first tab is where the form (above) is and the last tab is where I retrieve the data and the code is as follows:
<?php
// get saves meta values (charity and amount)
/*
$charity_id = esc_html( get_user_meta( $user_id, 'charity' , true ) );
$charity = get_the_title( $charity_id );
$donation_amount = esc_html( get_user_meta( $user_id, 'more_donation' , true ) );
*/
global $wpdb;
// get charity id
$charity_meta_key = "charity";
$charity_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT meta_value
FROM $wpdb->usermeta
WHERE user_id = %d
AND meta_key = %s",
$user_id,
$charity_meta_key
)
);
// get charity name
$charity = get_the_title( $charity_id );
// get donation amount
$donation_meta_key = "more_donation";
$donation_amount_db = $wpdb->get_var(
$wpdb->prepare(
"SELECT meta_value
FROM $wpdb->usermeta
WHERE user_id = %d
AND meta_key = %s",
$user_id,
$donation_meta_key
)
);
$donation_amount = $donation_amount_db;
?>
The commented out get_user_meta returns the same output as the $wpdb. How would I go about solving my issue - it is a cache issue? Any point in the right direction will be greatly appreciated.
Share Improve this question edited Aug 21, 2020 at 10:21 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Aug 21, 2020 at 8:04 TheLovableTypeTheLovableType 32 bronze badges1 Answer
Reset to default 0The tab you where you Displayed the data doesn't refresh after the Ajax call. Refresh the page in your ajax sucess call back to get the proper results.
本文标签: loopDatabase query and getusermeta return incorrect data
版权声明:本文标题:loop - Database query and get_user_meta return incorrect data 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742166942a2426000.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论