0

I have a jQuery script that sends POST data via AJAX to a php file that then creates a table. I have used firebug to check the console and everything gets created properly. On the success: I reload the window and the table isn't displayed.

I know the table html was created properly from the php file because I commented out the reload so I could see exactly what it created) I have lots of things displayed on my page and would like the table in a particular spot. How can I get the table to actually display where I want it to?

<script>
$(document).ready(function () {
    $("#stayinfobutton").click(function () {
        var id = $('#id').val();
        var dataString = {
            id: id
        };
        console.log(dataString);
        $.ajax({
            type: "POST",
            url: "classes/table_auto_guests.php",
            data: dataString,
            cache: false,
            /*success: function(html)
                    {
                        window.location.reload(true);
                    }*/
        });
    });
});
</script>
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
Burning Hippo
  • 787
  • 1
  • 20
  • 47

1 Answers1

1

The window.location call will reload a new page in the browser window, this will loose the data returned to the ajax call by the server.

Usually the response to Ajax calls is loaded directly into your page, something like:

success: function(html)
{
   $('#guestsTable').html(html);
   $('userForm').hide();
}

I made up the guestsTable div and userForm names, ;).

The return data may need some coercing to make it into html (I'm assuming your using JQuery), hopefully the php script will set it to html/text in the header, also dataType: html can be passed in the $.ajax({...}) call.

Alternatively, if the classes/table_auto_guests.php returns a full page which you want to load in the browser, Ajax may not be what you are looking for. This post contains code on how to submit the data as a form, and load the result as a new page.

Community
  • 1
  • 1
Henry Florence
  • 2,848
  • 19
  • 16