0

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.

PHP is 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);
}

1 Answer 1

0

Turns out that the $wpdb->update was running based on user being signed in via PHP. The variables that were placed into the condition statements were being ignored. To fix the issue I removed the $wpdb->update portion of the code from the 'user is signed in' condition and moved it to the 'if empty' conditional statements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.