php - How to format 2 variables for 1 ajax call so I can use the data easily? -


i have click event...

$('#save').on('click', function(){      var employee = $('#employee').serialize(); // form name, phone, email etc.      var training = []; // empty array      $('.entry').each(function(){          var tid = $(this).attr('id'); // number         var start = $(this).find('#start').val(); // date         var expiration = $(this).find('#expiration').val(); // date          var entry = [tid, start, expiration]; // create array data          training.push(entry); // push each entry array      });      $.ajax({          method: 'post',         url: 'brain.php',         data: {'training':training, 'employee':employee},         success: function(results) {              console.log(results)          }       });  }); 

the php receiving call looks this...

 $employee = $_post['employee']; // has string of input values  $training = $_post['training']; // has array of entries 

now works, receive both variables, they're kinda hard work purposes, putting them database. don't know how data out easily. this...

$firstname = $_post['employee']['first-name']; // first-name input $lastname  = $_post['employee']['last-name']; // last-name input  $entry1 = $_post['training'][0]; // array data found in 0 $entry2 = $_post['training'][1]; // array data found in 1 

my question is, have change in javascript or php want , make stuff easier work with? think i'm missing key concept here.

in opinion, since don't have data, can manually create object employee instead of using serialize. can use objects training entries, instead of arrays, each spot in training array has 1 object represent training id corresponding location in training array. that's little wordy, here's mean:

$('#save').on('click', function(){      var employee = {         "first-name":$('#employee').find('[name=first-name]').val(),         "last-name":$('#employee').find('[name=last-name]').val()     };      var training = []; // empty array      $('.entry').each(function(){          var tid = $(this).attr('id'); // number         var start = $(this).find('#start').val(); // date         var expiration = $(this).find('#expiration').val(); // date          var entry = {start:start,expiration:expiration}; // create object data          training[tid] = entry; // push each entry array, using id of entry index      });      var data = {employee:employee, training:training};      //post function here - use data variable data }); 

and here's fiddle: http://jsfiddle.net/x2hfb719/


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -