0

I am building a web page that gets data through a php file and with manipulation with AJAX and JQuery, displays the said data into a table.

In the buttons i have created, I want to be able to change the "page" to different pieces of data and have them displayed into the table. Currently the problem is that the data I have parsed through the query is returning the main page of data.

I believe the problem is in my page data function but i do not know how to fix this.

//Function to add the page data to the assigned buttons 
            $('#Buttons').on('click', 'button', function() {

                $('#Table1 tbody').remove();

                    $('#Table1').append("<tbody></tbody>");

                    var ap = 1
                    for(ap; ap <= 15; ap++){
                        if ($(this).attr('id') == ap){


                            $.getJSON('https://www.cs.kent.ac.uk/people/staff/lb514/hygiene/hygiene.php?=retrieve&pages=' + ap, function(data){
                                console.log(data)

                                var row
                                var t = 0;
                                    for(t; t < data.length; t++){

                                        $('#Table1').append( "<tr>" + "<td>" + data[t].business + "</td>", "<td>" + data[t].address + "</td>", "<td>" + data[t].rating + "</td>", "<td>" + data[t].date + "</td>"+"</tr>")

                                    }


                                }) 
                            }
                        }

The complete code

    <!DOCTYPE HTML>
<html>
    <head>
    <meta charset="utf-8" /> 
    <title>  Food Ratings </title>
    <style>

        h1{
            size:20px;
            color:#F44336;
            underline:bold;
        }

        table, th, td{
            border: 1px solid #E91E63;
        }

        #Buttons{


            padding: 15px 32px;
            text-align: center;

        }

        p{
            padding:5px;
            padding-bottom:10px;
        }


        body{
            background-color:#B3E5FC;
            color:#9C27B0;
        }

    </style>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
            <script>

            $(document).ready(function(){
                //First function to display the table
                $.get('https://www.cs.kent.ac.uk/people/staff/lb514/hygiene/hygiene.php', {
                op: "demo",

                }, function(data){ 

                    var row;
                    console.log(data);
                    data = $.parseJSON(data);

                    for(var i = 0; i < data.length; i++){
                        row = $('<tr/>');
                        row.append( "<td>" + data[i].business + "</td>", "<td>" + data[i].address + "</td>", "<td>" + data[i].rating + "</td>", "<td>" + data[i].date + "</td>");

                        $('#Table1').append(row);
                        }       
                });
                //Function to create the buttons to select the pages
                $.get('https://www.cs.kent.ac.uk/people/staff/lb514/hygiene/hygiene.php', {
                op : "pages",
                }, function(data){

                    var butNum = 1;
                    console.log(data)
                    data = $.parseJSON(data);

                        for(butNum; butNum <= data.pages; butNum++) {

                            $('#Buttons').append('<button id='+butNum +'>' + butNum+ '</button>');

                            }
                    }) ;
                //Function to add the page data to the assigned buttons 
                $('#Buttons').on('click', 'button', function() {

                    $('#Table1 tbody').remove();

                        $('#Table1').append("<tbody></tbody>");

                        var ap = 1
                        for(ap; ap <= 15; ap++){
                            if ($(this).attr('id') == ap){


                                $.getJSON('https://www.cs.kent.ac.uk/people/staff/lb514/hygiene/hygiene.php?=retrieve&pages=' + ap, function(data){
                                    console.log(data)

                                    var row
                                    var t = 0;
                                        for(t; t < data.length; t++){

                                            $('#Table1').append( "<tr>" + "<td>" + data[t].business + "</td>", "<td>" + data[t].address + "</td>", "<td>" + data[t].rating + "</td>", "<td>" + data[t].date + "</td>"+"</tr>")

                                        }


                                    }) 
                                }
                            }

                        });


                        $("#searchForm").submit(function(event) {

                                // Stop form from submitting normally
                                event.preventDefault();

                                        // Get some values from elements on the page:
                                        var $form = $(this),
                                            term = $form.find( "input[name='s']").val(),
                                            url = $form.attr("action");

                                        // Send the data using post
                                        var posting = $.post( 'https://www.cs.kent.ac.uk/people/staff/lb514/hygiene/hygiene.php', {s: term});

                                        // Put the results in a div
                                        posting.done(function(data) {
                                            var content = $(data).find("#content");
                                            $("#Table1").empty().append(content);
                            })  
                        });

        });     
        </script>
        <body>

            <h1>
                Food Standards Agency
            </h1>

        <br>
        <br>

            <div id="Buttons"> </div>
            <table id="Table1"  cellspacing="0" width="100%">

                <thead>
                    <tr>
                        <th>Business</th>
                        <th>Address</th>
                        <th>Rating</th>
                        <th>Date</th>
                </thead>
                <tbody>
                </tbody>
                </table>

                <form action="/" id="searchForm">
                    <input type="text" name="s" placeholder="Search...">
                    <input type="submit" value="Search">
                </form>

        <p>
            This site list all of the resturants in canterbury with their location and their rating according to the Food standards agency
        </p>

    </body>
</html>
WindWalker
  • 41
  • 1
  • 6
  • Possible duplicate of [Prevent browser caching of jQuery AJAX call result](http://stackoverflow.com/questions/367786/prevent-browser-caching-of-jquery-ajax-call-result) – Daniel Corzo Dec 05 '16 at 22:33
  • I'm having trouble understanding the issue. Is the url you are passing correct? Does the link have the correct data when you paste it in the browser? Are you able to parse it as a javascript object? – Goose Dec 05 '16 at 22:34
  • https://www.cs.kent.ac.uk/people/staff/lb514/hygiene/hygiene.php is returning the same data no matter the query string. It's not an ajax issue if I'm understanding this right. The problem is likely in `hygiene.php`, can you share it? – Goose Dec 05 '16 at 22:37
  • @Goose unfortunately i can't share the php file, as i don't have access to it myself. The url is correct, i think the error is the way that i am passing my data through the JQuery commands – WindWalker Dec 05 '16 at 22:40
  • 1
    It looks like an issue with the PHP page and its queries. If you take the URL and manipulate the URL vars in your browser, the data doesn't change. – princessjackie Dec 05 '16 at 22:57
  • Either the query parameters have a different name or the server is behaving incorrectly – JSelser Dec 06 '16 at 00:34
  • @WindWalker you'll have to prove that for this to move forward then – Goose Dec 06 '16 at 01:33

0 Answers0