Guest User

Untitled

a guest
Feb 21st, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. $.ajax({
  2. data:{'async':$('#form').serializeArray()},
  3. type:"POST",
  4. async: true,
  5. success: function(data) {
  6. $("#container").html(data);
  7. },
  8. failure: function(data) {
  9. alert('error');
  10. }
  11. });
  12.  
  13. $form->setData($request->getPost('async'));
  14. if (! $form->isValid()) { /*do stuff*/}
  15.  
  16. array{
  17. filedset1=aray{
  18. el_1.1=>'value',
  19. el_1.2=>'value',
  20. [...]
  21. },
  22. filedset2=aray{
  23. el_2.1=>'value',
  24. el_2.2=>'value',
  25. [...]
  26. },
  27. [...]
  28. };
  29.  
  30. array{
  31. filedset1[el_1.1]=>'value',
  32. filedset1[el_1.2]=>'value',
  33. [...]
  34. filedset2[el_2.1]=>'value',
  35. filedset2[el_2.2]=>'value',
  36. [...]
  37. };
  38.  
  39. $form->setData($this->parseSerializedArray($request->getPost('async')));
  40. if (! $form->isValid()) { /*do stuff*/}
  41.  
  42. [...]
  43.  
  44. public function parseSerializedArray($array)
  45. {
  46. $result=array();
  47.  
  48. foreach($array as $item){
  49.  
  50. if (preg_match("/^w+[w+]$/",$item['name'])){
  51. $str=explode("[",$item['name']);
  52. $key=$str[0];
  53. $val=rtrim($str[1],']');
  54.  
  55. if (!array_key_exists($key,$result)){$result[$key]=array();}
  56. $result[$key][$val]=$item['value'];
  57. }
  58. //if the element doesn't belong to any fielset...
  59. else{$result[$item['name']]=$item['value'];}
  60. }
  61.  
  62. return $result;
  63. }
  64.  
  65. $.ajax({
  66. data:{'async':$('#form').serialize()}, //output: querystring
  67. async: true,
  68. success: function(data) {
  69. $("#container").html(data);
  70. },
  71. failure: function(data) {
  72. alert('error');
  73. }
  74. });
  75.  
  76. parse_str($request->getPost('async'),$data);//output: multidimensional array
  77.  
  78. $form->setData($data);
  79. if (! $form->isValid()) { /*do stuff*/}
Add Comment
Please, Sign In to add comment