📜 ⬆️ ⬇️

Thoughtful event controller on the page

image Surely you have faced such tasks:

- Load ajax data on the page;
- By clicking on the object to get a response from the server;
- Make a conclusion (not) dependent list on ajax.

Written controller solves these problems, it can:
')
- upload any amount of specified data per page;
- Control events for created objects and load new ones;
- Track the dependence of SELECT lists with any nesting.

The size of this monster is just a few lines.

With comments a little more lines:

$(document).ready(function() { //    function post_ajax(url, data, inn) { $.ajax({ type: "POST", url: url, data: data, success: function(data) { $(inn).html(data); } }); return; } //  #need_ajax              jQuery.each($("div#need_ajax"), function(i) { post_ajax($(this).data("url"), $(this).data("post"), $(this).data("html")); }); //     ,    ,     //      '.city, .do_ajax $('body').on('click', '.city, .do_ajax', function() { //         ,        $(this).data("inpost"); var my_select = $("option:selected", this).val() ? $("option:selected", this).val() : $(this).data("inpost"); //   ,      ?    ?    $(this).data("clean")  if ($(this).data("clean")) { var to_clean = $(this).data("clean").split(" "); if (to_clean) $.each(to_clean, function(i) { $("#" + this).empty(); }); } //              ?   $(this).data("next")   my_select   $(this).data("url") var to_next = $(this).data("next"); if (to_next && my_select != 0) post_ajax($(this).data("url"), $(this).data("post") + "=" + my_select, "#" + to_next); }); }); 


How it works

Add to page:

 <div id='ob_tel_data' name='ob_tel_data'> <a class='ob_hide do_ajax' href='javascript:void();' data-inpost='".$o[id]."' data-post='ajax_contacts' data-next='ob_tel_data' data-url='/?ajax_contact' ></a> </div> 


Where are we:

data-url = '/? ajax_contact' - address for the request
data-post = 'ajax_contacts' - variable name
data-inpost = '". $ o [id]."' - variable data
data-next = 'ob_tel_data' - where to insert the received response html

As a result, we get a button, when clicking on which the request is made and the html is updated in the given data-next

How can I make dependent select
To draw a select, add:

 <div id='need_ajax' data-url='/map.php?' data-post='g1=1' data-html='#html_g1'></div> <div name="html_g1" id="html_g1" ></div><div name="html_g2" id="html_g2" ></div><div name="html_g3" id="html_g3"></div> 


Here - need_ajax indicates that you need to load data from the data-url address and data-post request and insert it into data-html = '# html_g1'
After that, the data is also recursively processed by the function and can update the data and clear the garbage.

In PHP, the city selection handler itself looks like this:

 #       $assoc = array( "g1" => array("clean" => "html_g2 html_g3", "next" => "html_g2", "url" => "/map.php?", "post" => "g2"), "g2" => array("clean" => "html_g3", "next" => "html_g3", "url" => "/map.php?", "post" => "g3"), "g3" => array("clean" => "", "next" => "", "url" => "/map.php?", "post" => "g4"), "g4" => array("cookie" => "city[3]") ); #   ? php.net/manual/ru/wrappers.php.php # php://input -   AJAX  $_POST = @file_get_contents("php://input"); #    if ($_POST) { $a = explode("=", $_POST); $r77 = @mysql_query("SELECT * from ajax_city where ". ( $a[0] == "g1" ? " main='1' " : " parentid='".number_format($a[1], 0, '', ''). "'") ." ORDER BY pos DESC , text ASC "); if (@mysql_num_rows($r77) > 0) { #   $select = ' <select name="'.$a[0].'" id="'.$a[0]. '" class="city" data-clean="'.$assoc[$a[0]][clean]. # -       '" data-next="'.$assoc[$a[0]][next]. # -      '" data-url="'.$assoc[$a[0]][url]. # -       '" data-post="'.$assoc[$a[0]][post]. # -      '" > <option value=0>  </option> '; while ($o = @mysql_fetch_array($r77)) { $select. = "<option value='".$o[cityid]."' ".($o[cityid] == $a[1] ? " SELECTED" : "")." >".$o[text]."</option>\n"; } $select. = "</select>"; } echo $select; exit; 


Demo on - button (Show contact details →), by select (City selection)

What for? The controller can serve any number of buttons and selektov on the page, update, fill, clear dependent lists and buttons.

This is not a big but useful code.
Thanks for attention.

Source: https://habr.com/ru/post/233469/


All Articles