John Davidson

Database entry removed on browser refresh, Ajax PHP jQuery

0 comments
Message:


I have a form that updates columns in a database table on submit of a form via ajax. Everything works great, the database table columns get the information however once the browser is refreshed the information is removed from the database. I have php set to execute the database update if a meta_value isn't present but the meta_value is in the database as it's created when the form is submitted as well.


I would like the information to remain in the database until or unless the meta_value has been removed or isn't present.


Any insight is appreciated.


PHP


add_action('wp_ajax_hide_this', 'hide_this_by_id');
add_action('wp_ajax_nopriv_hide_this', 'hide_this_by_id');
function hide_this_by_id()
{
global $wpdb;
$wpdbPrefix = $wpdb->prefix . 'swpm_members_tbl';
$postdVlaue2 = $_POST['hidebtn2'];
$this_user = $_POST['thisuser'];
$this_num = $_POST['thisnum'];


if (is_user_logged_in()) {
$member_id = SwpmMemberUtils::get_logged_in_members_id();

$query = "SELECT * FROM " . $wpdb->prefix . "swpm_members_tbl WHERE member_id = %d";
$userData = $wpdb->get_row($wpdb->prepare($query, $member_id));

$membership_levels = $userData->membership_level;

$labelID4 = $membership_levels;

$insertdisUr = $wpdb->update( $wpdbPrefix, array( 'this_user' => $this_user), array( 'member_id' => $member_id));
$insertdisId = $wpdb->update( $wpdbPrefix, array( 'this_id' => $this_num), array( 'member_id' => $member_id));

} else {
$not_loggedin = 1;
}

if ($labelID4 == 10 ) {
$userlvlMeta2 = 1;
$alredyclick3 = get_user_meta($member_id, 'hidden-info', true);
if (empty($alredyclick3) && $postdVlaue2 == 1) {
$insertdisUr;
$insertdisId;
$alredyclick3 = 1;
}
}

$return4 = array(
'hIdethis2' => $this_hide2,
'userlvlMeta2' => $userlvlMeta2,
'userlvlNolog' => $not_loggedin,
);

echo json_encode($return4);

die();
}

jQuery


function doAjaxRequest4(hidebtn2,getthisInfo,getthisInfo2) {

jQuery.ajax({
url: ajax_sib_front_object.ajax_url,
data: {
'action': 'hide_this',
'thisuser': getthisInfo,
'thisnum': getthisInfo2,
'hidebtn2': hidebtn2
},
dataType: 'JSON',
type: "post",
success: function (data) {
console.log(data.test);
var input = jQuery('.thisuser > input');
var input2 = jQuery('.thisnumber > input');
var is_name = input.length > 3;
var is_name2 = input2.length > 3;

if (!data.hIdethis2 == 1 && data.userlvlMeta2 == 1) {
jQuery("#this_col_1").addClass("enable_this");
} else if (data.hIdedisc2 == 1 && is_name && is_name2 ) {
jQuery("#this_col_1").removeClass("enable_this");
jQuery("#this_col_2").addClass("enable_this");

}
}
});
}

Function Called by


if ($('body').is('.page-id-9999') || $('body').is('.page-id-1111')) {

var thisbtn = document.querySelector('#this_verf_form > div > .wpcf7');

thisbtn.addEventListener('wpcf7submit', function (event) {
var status = event.detail.status;
console.log(status);
if (status === 'mail_sent') {
jQuery('#this_submtID').val("Submitted");
}
setTimeout(function () {
doAjaxRequest4(1,getthisInfo,getthisInfo2);
}, 3500);
}, false);
}


My Answer:
>prefix;_ $id = $_POST['id'];_ $meta_value = get_post_meta($id, 'hidden', true);_ if (!$meta_value) {_ update_post_meta($id, 'hidden', 'yes');_ $wpdb->update($wpdb->prefix . 'your_table_name', array('column_name' => 'value'), array('id' => $id));_ }_ wp_die();_}_

In the code above, the `hide_this_by_id` function is called when the form is submitted via AJAX. It checks if the `hidden` meta value is present for the post ID. If it is not present, it updates the post meta with the value 'yes' and updates the database table with the new information.

To ensure that the information remains in the database even after a browser refresh, you can modify the condition in the `hide_this_by_id` function to only update the database table if the `hidden` meta value is 'yes'. This way, the information will only be removed from the database if the `hidden` meta value is removed or not present.

Here is the modified code snippet:

php
function hide_this_by_id() {
global $wpdb;
$wpdbPrefix = $wpdb->prefix;
$id = $_POST['id'];
$meta_value = get_post_meta($id, 'hidden', true);
if ($meta_value === 'yes') {
$wpdb->update($wpdb->prefix . 'your_table_name', array('column_name' => 'value'), array('id' => $id));
}
wp_die();
}


By making this change, the database table will only be updated if the `hidden` meta value is 'yes', ensuring that the information remains in the database unless the `hidden` meta value is removed or changed.

Rate this post

3 of 5 based on 2872 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us