Drukowana wersja tematu

Kliknij tu, aby zobaczyć temat w orginalnym formacie

Forum PHP.pl _ AJAX _ [Ajax,jQuery] Przypisanie danych przed wysłaniem

Napisany przez: damianooo 28.06.2016, 19:21:26

mam przykładowy formularz:

  1. {% for key,match in matches %}
  2. <http://december.com/html/4/element/form.html>
  3. <http://december.com/html/4/element/input.html type="text" name="firstname[]" id="firstname" '>
  4. <http://december.com/html/4/element/input.html type="text" name="lastname[]" id="lastname" '>
  5. </http://december.com/html/4/element/form.html>
  6. {% endfor %}


i wysyłam dane z niego ajaxem do bazy danych:

  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3. $("#submit").click(function(){
  4. $.ajax({
  5. url:"{{ path('costam') }}",
  6. type:"POST",
  7. data:{
  8. firstname: $("#firstname").val(),
  9. lastname: $("#lastname").val(),
  10. },
  11. ....
  12. </script>


Niestety w bazie po wysłaniu pojawia się tylko jeden nowy rekord, a powinienem mieć 10 nowych rekordów ponieważ tyle przesyłałem w formularzu.

Co robię źle ?
Czy w kodzie jQuery w sekcji "data" mam dobrze zrobione przypisanie danych ?

Napisany przez: viking 28.06.2016, 19:59:48

Dane wyciągasz po id które w dokumecie może być jedno unikalne.

Napisany przez: Comandeer 28.06.2016, 20:32:19

A czemu nie wsadzić tego do jednego formularza i wówczas go posłać po zserializowaniu → http://api.jquery.com/serialize/

Napisany przez: damianooo 28.06.2016, 21:13:51

Viking rozumiem a więc po klasie a nie po ID ...
a więc w formularzu ma być tak:

  1. <http://december.com/html/4/element/input.html type="text" name="firstname[]" id="firstname" class="firstname">


a w skrypcie tak:

  1. data:{
  2. firstname: $(".firstname").val(),
  3. lastname: $(".lastname").val(),
  4. },


?

tylko chyba nadal coś nie tak bo dodaje mi ciągle po jednym rekordzie.

Napisany przez: Comandeer 28.06.2016, 22:00:36

Kombinujesz. Zrób jeden formularz, coś typu:

Kod
<form>
{% for key,match in matches %}
<input type="text" name="firstname[{{ key }}]" id="firstname" '>
<input type="text" name="lastname[{{ key }}]" id="lastname" '>
{% endfor %}
</form>

I proponuję przesłać ten formularz po serializacji.

Napisany przez: damianooo 29.06.2016, 12:22:05

ok spróbuję z serializacją.

nie wiem jednak czy ja to robię dobrze:

  1. <http://december.com/html/4/element/div.html class="form">
  2. <http://december.com/html/4/element/div.html class="coupon_form">
  3. {% for key,match in matches %}
  4. <http://december.com/html/4/element/input.html type="hidden" name="match_id[]" id="match_id" class="match_id" value="{{ match.match_id }}">
  5. <http://december.com/html/4/element/div.html class="coupon_form_desc">
  6. {{ match.description }}
  7. </http://december.com/html/4/element/div.html>
  8. <http://december.com/html/4/element/div.html class="coupon_form_host">
  9. {{ match.host }}
  10. </http://december.com/html/4/element/div.html>
  11. <http://december.com/html/4/element/div.html class="coupon_form_host_type">
  12. <http://december.com/html/4/element/input.html type="text" name="hostType[]" id="hostType" class="hostType" maxlength='1'>
  13. </http://december.com/html/4/element/div.html>
  14. <http://december.com/html/4/element/div.html class="coupon_form_dash">-</http://december.com/html/4/element/div.html>
  15. <http://december.com/html/4/element/div.html class="coupon_form_guest_type">
  16. <http://december.com/html/4/element/input.html type="text" name="guestType[]" id="guestType" class="guestType" maxlength='1'>
  17. </http://december.com/html/4/element/div.html>
  18. <http://december.com/html/4/element/div.html class="coupon_form_guest">
  19. {{ match.guest }}
  20. </http://december.com/html/4/element/div.html>
  21. <http://december.com/html/4/element/div.html class="coupon_form_term">
  22. {{ match.term }}
  23. </http://december.com/html/4/element/div.html>
  24. {% endfor %}
  25. </http://december.com/html/4/element/div.html>
  26. <http://december.com/html/4/element/input.html id="submit" type="submit" value="Submit">
  27. </http://december.com/html/4/element/div.html>
  28.  
  29. <http://december.com/html/4/element/script.html type="text/javascript">
  30. $(document).ready(function(){
  31. $("#submit").click(function(){
  32. $.ajax({
  33. url:"{{ path('typerka_types') }}",
  34. type:"POST",
  35. data: $(".form").serialize(),
  36. dataType:"JSON",
  37. success:function(data){
  38. console.log(data);
  39. alert(data);
  40. },
  41. error: function() {
  42. console.log("Wystąpił błąd");
  43. }
  44. });
  45. });
  46. });
  47. </http://december.com/html/4/element/script.html>


jak patrzę w Firebugu to console.log wyświetla mi "wystąpił błąd" - dlaczego ?
Poza tym dostaję następujący kod błędu serwera: 500 Internal Server Error

Napisany przez: Comandeer 29.06.2016, 14:02:02

A jaki błąd dokładnie rzuca Firebug?

Napisany przez: damianooo 29.06.2016, 20:06:27

a jednak działą smile.gif

wielkie dzięki za pomoc.

Pozdrawiam

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)