Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> JavaScript serializeArray to PHP array, Jak?
Blackhole
post
Post #1





Grupa: Zarejestrowani
Postów: 283
Pomógł: 1
Dołączył: 15.11.2004
Skąd: Mikołów

Ostrzeżenie: (0%)
-----


Cześć.
Wysyłam sobie Ajaxem z jQuery formularz:
  1. <script>
  2. function cfg_gru_save(nvp) {
  3. $.ajax({
  4. url: nvp+"cfg_ajax.php",
  5. type: "POST",
  6. data: {func: "gru_save",
  7. form: $('#gru_form').serialize()
  8. },
  9. dataType: "json",
  10. success: function(data,status) {
  11. alert("Grupy zostały zmienione.");
  12. },
  13. error: function(xhr, status, errorThr) {
  14. alert("Jakiś błąd.\n\nxhr: "+xhr+"\nStatus: "+status+"\nerrorThr: "+errorThr);
  15. }
  16. });
  17. }
  18. </script>
  19.  
  20. $gru_f = file($nvp."grupy.txt", FILE_IGNORE_NEW_LINES);
  21. echo '<form id="gru_form" method="POST" action="">';
  22. echo '<table cellpadding="0" cellspacing="1">';
  23. for ($g=0; $g<count($gru_f); $g++) {
  24. $gru_l = explode("|", $gru_f[$g]);
  25. echo '<tr><td><input type="hidden" name="gru_id[]" value="'.$gru_l[0].'"><input type="text" value="'.$gru_l[1].'" name="gru_name[]"/></td></tr>';
  26. }
  27. echo '<tr><td><input type="button" value="Zmień" onclick="blur(); cfg_gru_save(\''.$nvp.'\')"/></td></tr>';
  28. echo '</table></form>';

Jak zrobić, by w cfg_ajax.php uzyskać dane tego formularza w postaci łatwej do przetwarzania tablicy o strukturze array("gru_id"->array(...), "gru_name"=>array(...)) ?
Próbowałem już na różne sposoby, ale nie mogę dojść do rozwiązania:
  1. <?function gru_save($form) {
  2. $p = "";
  3. //$form = str_replace("=","[]=",$form);
  4. log_me("form", $form);
  5. parse_str($form, $fr);
  6. /*
  7. ob_start();
  8. var_dump($fr);
  9. $frr = ob_get_clean();
  10. */
  11. log_val("fr", $fr);
  12. for ($f=0; $f<count($fr); $f++) {
  13. $p.= "fr ".$fr['gru_id'][$f]."|".$fr['gru_name'][$f]."\n";
  14. }
  15. for ($f=0; $f<count($form['gru_id']); $f++) {
  16. $p.= "form ".$form['gru_id']."|".$form['gru_name']."\n";
  17. }
  18. file_put_contents("grupy_test.txt", $p);
  19. echo json_encode("");
  20. }
  21. //=====================================
  22.  
  23. switch ($_POST['func']) {
  24. case 'gru_save': gru_save($_POST['form']); break;
  25. }
  26. ?>
Go to the top of the page
+Quote Post

Posty w temacie
- Blackhole   JavaScript serializeArray to PHP array   9.07.2014, 19:39:06
- - Pyton_000   var_dump($_POST)   10.07.2014, 10:43:15


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 25.12.2025 - 13:37