Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice with jQuery and AJAX calls

I have a question about best practices when using jQuery/JavaScript/Ajax. Lets say that I have some tasks and there is a calendar for every task. The User is able to click on a day in a task calendar and book the task at the specific day via AJAX. I have to store the date and the ID of the task somewhere and i am using really bizarre IDs for that such as:

<span class="day_field" id="date_13-02-2013_task_4">13.02.2013</span>

Then i just attach an listener like this:

$('.day_field').on('click',function(){
    var date = $(this).id.split('_')[1];
    var task_id = $(this).id.split('_')[3];
    //place for some validation
    $.post('book_task.php',{task_id: task_id, date: date},function(data){
        //something cool with the result
    });
});

My question is: Is this the right way how to do it? I am not pretty sure, because the IDs can be really long + it contains ID in database which is not probably save at all.

Thanks! T.

like image 225
user1377911 Avatar asked Feb 26 '13 22:02

user1377911


People also ask

Can jQuery and AJAX be used together?

jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!

Should I use jQuery for AJAX?

So, the real question is: should you use jQuery to perform your Ajax requests? And the answer is: yes, if you're already using jQuery - if not, you can include jQuery or another Ajax-supporting JS library, or you can implement the Ajax functionality in vanilla JS.

Which is better jQuery or AJAX?

jQuery uses ajax for many of its functions, but it nothing else than a library that provides easier functionality. With jQuery you dont have to think about creating xml objects ect ect, everything is done for you, but with straight up javascript ajax you need to program every single step of the ajax call.

Is AJAX still used in 2021?

Yes, people still use Ajax for web applications. If you have ever submitted a form on a modern website, chances are it uses Ajax in some capacity.


3 Answers

Use HTML5 data attributes:

<span class="day_field" data-date="13-02-2013" data-task="4">13.02.2013</span>

$('.day_field').on('click',function(){
    var date = $(this).data("date");
    var task_id = $(this).data("task");
    //place for some validation
    $.post('book_task.php',{task_id: task_id, date: date},function(data){
        //something cool with the result
    });
});
like image 149
Barmar Avatar answered Oct 03 '22 01:10

Barmar


The right wayA better way to do it would be to store the data in either data attributes, or make the span an anchor tag and store the param string desired in the href attribute.

<span class="day_field" data-date="13-02-2013" data-task="4>13.02.2013</span>

or

<a class="day-field" href="?task_id=4&date=13-02-2013">13.02.2013</a>

with this for the anchor tag:

$('.day_field').on('click',function(e){
    e.preventDefault();
    $.post("foo.php",this.href,handler);
});
like image 27
Kevin B Avatar answered Oct 03 '22 02:10

Kevin B


Instead of an ID, you can use custom data attributes, like this:

<span class="day_field" data-date="date_13-02-2013_task_4">13.02.2013</span>

And then you can access the value like this in jQuery:

$(".day_field").data("date");
like image 36
zakangelle Avatar answered Oct 03 '22 02:10

zakangelle