0

This code below only works if i click on the button twice and I've got no clue why it does that.

Any hints would be appreciated. guess it's a .click function that's missing ?

<button onclick="getSummary('1');">button</button>

function getSummary(id)
    {
       $.ajax({ type: "GET",
                url: 'ajax_test.php',
                data: "user=<?php echo $_GET['user'];?>",
                success: function(data) {
        $('#summary').html(data);}
        });
    }
chrjoh88
  • 27
  • 8
  • 2
    Perhaps the first Ajax call is failing, but you only have `success:` so you wouldn't notice. Add `error:` and see if it goes there. As a side, it is always recommended to specify the type (e.g. `type="button"`) for the ` – Racil Hilan Jul 06 '17 at 00:05
  • Thanks! totally forgot about `error:` for troubleshooting ! – chrjoh88 Jul 06 '17 at 00:07
  • As it stands your code works pretty fine and it fires after a first click returning the response. perhaps... https://stackoverflow.com/a/6085669/383904 ? Always log your error responses (as suggested), read the [docs](https://stackoverflow.com/a/6085669/383904) and make sure to pass the correct data. P.S: what's the `'1'` in `getSummary('1');` ? You never use it. – Roko C. Buljan Jul 06 '17 at 00:17
  • got it to work now got one question if you guys know a good solution load the ajax when page starts I've got this code at the moment and i want to have it load when page loads. `function getSummary() { $("button").click(function(){ $.ajax({ type: "GET", url: 'ajax_test.php', data: "user=", dataType: 'html', success: function(data) { $('#summary').html(data); }, error: function() { alert("something went wrong while sending data to server");} }); }); }` – chrjoh88 Jul 06 '17 at 02:37
  • Don't post code in comments, it's very hard to read. Edit your question and add it there if it is related. If not, start another question. – Racil Hilan Jul 06 '17 at 03:04

0 Answers0