Part 1: jQuery & # 8594; MySQL & # 8594; jQuery & # 8594; HTML

I am developing an application that relies heavily on jQuery for user interaction.
(and if your browser does not support jQuery, update or do not use my application :)

As usual, one has functions for GET, SET and DELETE data from the table.

In my application, I gather and arrange a lot of information without reloading the page. For this, I mainly use jQuery.post.

Typical code in my JS file is as follows:

jQuery.post("mypath/jquery_getset_data.php", { instance: 'getItems_A', itemID: itemID_value},
  function(data) {
    populateItemList(data);
  }); 

jquery_getset_data.php contains many if statements :

if($_POST['instance'] == 'getItems_A'){
  // PHP code to get and process data from MySQL DB
}

if($_POST['instance'] == 'setItems_A'){
  // PHP code to process and insert data to MySQL DB
}

Here is my question:

  • Is the best way to interact between the JS file and jquery_getset_data.php?

  • " " createStoreList? . 1.

1: , .

  function createStoreList(data)
  {
    var ul = jQuery("<ul/>");

    // We need to build the html structure in order for this to be registered in DOM.
    // If we don't, jQuery events like .click, .change etc. will not work.      
    for (var i = 0; i < data.length; i++)  
    {
      ul.append(
         jQuery("<li/>")
         .attr("id", "listItem_"+data[i].id)
         .append(jQuery("<span/>")
            .addClass("btnRemoveItem")
            .attr("title", "Remove store from list")
            .attr("id", data[i].id)
            .click(function() { removeItemA(this); })  
          )
         .append(data[i].name + ', ' + data[i].street)
        );              
    }
    return ul;  
  }

2 , switch. , .

    .click(function() {
        switch(instance)
        {
          case 'removeListItemA': removeListItemA(this); break; 
          case 'removeListItemA': removeListItemB(this); break;
          case 'removeListItemA': removeListItemC(this); break;
        }
    })
+5
2

jquery_getset_data.php, , if.

class ICommand
{
     public:
          function execute( );
};

class CommandGetItemA
{
     public:
           function execute( )
           {
               //do some staff here
           };
};

:

CommandsMap['getItemA'] = new CommandGetItemA( );
CommandsMap['setItemA'] = new CommandGetItemB( );
....

CommandsMap[ $_POST['instance']].execute( );

, , . , , ?

, , , :

.click(function() {
      window[instance]( this);   
});

"" - , , ;

+3

, , - jquery_getset_data.php. switch if. jQuery $.post , , script, (//etc), GET ajax ($.load $.get), HTTP .

+1

All Articles