Ask your WordPress questions! Pay money and get answers fast! (more info)

Make Ajax Function More Efficient WordPress

  • SOLVED

Hello all,

For a site for booking time slots, I have an ajax function that checks if the user selected time overlaps with any existing time blocks. The function itself works well, however, it tends to give false positives or negatives at times, but then will give the correct answer when re-checked. I am asking for anyone with ajax knowledge to help with the code a bit to make it work more consistently.

Here is a screen cast re creating the problem: [[LINK href="http://screencast-o-matic.com/watch/cll2F9VhRp"]]http://screencast-o-matic.com/watch/cll2F9VhRp[[/LINK]]

Here is the page with a sample form: [[LINK href="https://hookahi.com/guides/hucks-charters/"]]https://hookahi.com/guides/hucks-charters/[[/LINK]]

Here is the ajax code:[[LINK href="http://pastie.org/5531690"]] http://pastie.org/5531690[[/LINK]]

I am looking for edits to the code that will help solve the problem, I have no experience with ajax, so unfortunately just ideas on how to solve it don't help me much.

Answers (2)

2012-12-14

jazbek answers:

Hi ktrusak,

I've taken a look at your code and it appears that each time you change the date in the datepicker, then the ajax request gets sent twice for some reason. The first time with the old value, and the second time with the new value. Sometimes the responses clash with each other and the request for the old date will overwrite the request for the new date.

What happens when you remove this line:

$('.cart .gfield input.datepicker').change(function() {$(this).blur();});
And then change the following line to this:
$('.cart').find('.gfield_time_hour input, .gfield_time_minute input, .gfield_time_ampm select, .gfield input.datepicker').change(function() {

( note that the difference is that at the end of that line, I changed it from from ".blur(function(){" to ".change(function(){" )

?


Kyle comments:

Thanks for the reply,

It is working much better now! Just what I was looking for. Is there some way to get it to work as the user is typing in the hour/minute input, and not just when they click out of the box too?


jazbek comments:

Yes, but the problem you want to avoid is two requests being active simultaneously, if the responses come back out of order, which is entirely possible, then the wrong result will be showing.

It would take some more complicated code changes to accomplish this. If you'd like to continue with that upgrade, I'd suggest posting a new question. :)


Kyle comments:

Gotcha, makes sense. Thank you for your help!

2012-12-14

Arnav Joy answers:

you have not shown , code for , how you are finding status , i mean php code.

one more thing have you noticed when the problem occurs and when you get success result


Kyle comments:

The problem occurs mainly when selecting a new date from the datepicker, but sometimes with the other inputs. I pmed you the php.

Also it does not fire when selecting an am/pm time which would help.