0

Not sure if this is an XID issue or what, but occasionally it will be successful but 99% of time it fails. I want someone to seamlessly turn on "vacation mode" by just checking radio button without submit button or refresh... it goes and just sets a variable in database and spits out "success or fail".

I'm using same custom ajax code throughout site and it works except this one page. I'm sending a hidden XID, and tried stripping it down as much as possible.

Error it spits out for me (which i have no clue what that means):

{!-- ra:000000007029234a0000000076a13fa1 --}

Here's my form basically:

<form name="vacationModeForm" class="clearfix vacationModeForm" id="vacationModeForm" action="{site_url}ajax/set_vacation_mode" method="POST" data-postback='vacationPostback' >    

<input type="radio" name="wk{embed:selectedWeek}_Active" value="0" onchange="$('.vacationModeForm').submit();" {if wk{embed:selectedWeek}_Active == 0} checked='checked' {/if} > OF<input type="radio" name="wk{embed:selectedWeek}_Active" value="1" onchange="$('.vacationModeForm').submit();" {if wk{embed:selectedWeek}_Active == 1} checked='checked' {/if} > ON
<input type="hidden" name="XID" value="{XID_HASH}" />

Here is the simple ajax template setting a variable (I've stripped down as much as i could and even hardcoded)

ee()->db->update('pc_users',
    array(
        'wk2_Active'  => '1'
    ),
    array(
        'member_id' => '1618'
    )
);  

if ( (ee()->db->affected_rows()) > 0) {
    echo 'Success...';
}
else {
    echo "Failed";
    ee()->db->error();
}

Here's the custom ajax call used throughout. it gets to the ajax template fine (I removed all the other classes for the .on, as I'm using this on every form throughout site and it works great on at least 10 diff. ajax calls/forms

$('body').on("submit", ".vacationModeForm", function(e){
    e.preventDefault();
    var form = $(this);
    var postback = form.attr('data-postback');
    // only submit filled in fields so you don't get php undefined
    var fields = $(this).find(":input").filter(function () {
        return $.trim(this.value).length > 0;
    }).serialize();
    console.log('ajax form submitting to: '+form.attr('action'));
    $.ajax({
        type:"POST",
        url:form.attr('action'),
        data:fields,
        dataType:"html",
        beforeSend: function() {
            $('.'+postback+'').show().html('<span class="fa fa-spinner fa-pulse fa-3x fa-fw"></span>');
        },
        success: function(response){ 
            $('.'+postback+'').show().html(response);
            console.log(fields);
        },
        error: function(xhr, status, error) {
          alert(xhr.responseText);
        }
    });
});
  • I'm not seeing any jQuery or vanialla JavaScript ajax calls in your code, just a regular POST... – jrothafer Aug 10 '15 at 18:23
  • sorry, i updated my question with the custom ajax code. it never "alerts" on error so not sure if that is set up right. it gets to the ajax template fine – Chief Aug 10 '15 at 19:02
  • It seems that if I hardcode the db variable 'wk2_Active' to the opposite of what is set (1 or 0), and refresh page it works ONCE. If you keep clicking on the 2 radio buttons to toggle it and keep setting that variable though, it doesn't work again unless i Manually change the db variable to opposite of what my script has. – Chief Aug 10 '15 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.