Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony2][Ajax]podać zmienną do kontrolera używając ajax
twojastara
post 28.04.2015, 11:46:03
Post #1





Grupa: Zarejestrowani
Postów: 124
Pomógł: 0
Dołączył: 25.11.2014

Ostrzeżenie: (10%)
X----


Chciałbym podać zmienną do kontrolera używająć ajax przy zdarzeniu onchange, wsadzić ją w sesję i wydrukować jej zawartość na nowej stronie.


  1. //test1.html.twig with $.ajax script and <input> tag.
  2.  
  3. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"</script>
  4. <script type='text/javascript'>
  5.  
  6. function myFunction() {
  7. var elementy = document.getElementsByClassName('inputISBN');
  8. var data = elementy[0].getAttribute('value');
  9.  
  10. $.ajax({
  11. url: "{{ path('test') }}",
  12. type: "POST",
  13. data: data ,
  14. success: function() {alert("ok");}
  15. });
  16. }
  17. </script>
  18.  
  19. <form action="#" method="#">
  20. <input type="number" class="inputISBN" name="name"
  21. value="666" onchange="myFunction()">
  22. </form>


Here are Actions in Controller:

  1. /**
  2.   * @Route("/test1", name="test1")
  3.   * @Template()
  4.   */
  5. public function test1Action()
  6. {
  7. return array();
  8. }
  9.  
  10.  
  11. /**
  12.   * @Route("/test", name="test")
  13.   * @Template()
  14.   */
  15. public function testAction(Request $request)
  16. {
  17. $data = $request->request->get('data');
  18. $session = $request->getSession();
  19. $session->set('data',$data );
  20. return array();
  21. }
  22.  
  23.  
  24. /**
  25.   * @Route("/test2", name="test2")
  26.   * @Template()
  27.   */
  28. public function test2Action(Request $request)
  29. {
  30. $session = $request->getSession();
  31. $data = $session->get('data');
  32. echo '<pre>',print_r($data),'</pre>';
  33. return array();
  34. }


Dostaję alert('ok') po zmianie wartościw tagu input,ale po otwarciu test2.html.twig print_r($data) zwraca 1.
Go to the top of the page
+Quote Post
Turson
post 28.04.2015, 11:52:47
Post #2





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


A co dałby alert(data) ?
Poza tym spróbuj:
data: { "data": data } ,
Go to the top of the page
+Quote Post
twojastara
post 28.04.2015, 13:56:16
Post #3





Grupa: Zarejestrowani
Postów: 124
Pomógł: 0
Dołączył: 25.11.2014

Ostrzeżenie: (10%)
X----


Działa. Możesz podac link do dokumentacji, która taki zapis podaje? W błędnym zapisie opierałem się na Stackoverflow.
Go to the top of the page
+Quote Post
Turson
post 28.04.2015, 14:26:01
Post #4





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


Dokumentacja jQuery
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 28.04.2024 - 15:44