3 Replies - 309 Views - Last Post: 15 November 2017 - 12:05 AM Rate Topic: -----

#1 jamescv31   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 06-October 13

PHP and Ajax navigation went incorrect to show data

Posted 13 November 2017 - 10:55 PM

I'm having an ongoing development on developing a website using Filemaker API capabilities, actually some problems arise are managed to solved immediately as possible since I owned the API PDF codes.

However when it comes to Ajax Navigation, there is something going on

When I click next record, the increment goes fine so goes also to prev record (decrement)

However when I click last record it detect correctly but when navigating previously it turns out to reset from 1.

Also it seems my navigation is not working correctly when the record number does not set properly.

Test_pagination.php

<?php
    session_start();
    ?>
    <!DOCTYPE html>
    <html lang = "en">
        <head>
            <meta charset = "UTF-8" name = "viewport" content = "width=device-width, initial-scale=1" />
        </head>
    <body>
        <nav class = "navbar navbar-default">
            <div class = "container-fluid">
                <a class = "navbar-brand" href = "https://sourcecodester.com">Sourcecodester</a>
            </div>
        </nav>
        <div class = "row">
            <div class = "col-md-3"></div>
            <div class = "col-md-6 well">
                <h3 class = "text-primary">Simple Ajax Pagination With PHP</h3>
                <hr style = "border-top:1px dotted #000;"/>
                <div class = "table-responsive" id = "load_data">   

                </div>  
            </div>
        </div>



    <form action="<?php $_SERVER['PHP_SELF'];?>" method="get" name="search" id="search" >

    <input name="page" type="hidden"  value="1"/>
    <input name="skip" id="skipinfo" type="hidden"  value="0"/>
    <input name="pleasef" id="pleasef" type="hidden" value="<?php echo $_SESSION['testt']; ?>"/>


    </form>


    </body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="ajax.js"></script>
    </html>





Filemaker API capabilities, book_data.php
 <?php
            session_start();
                include('FileMaker.php');
                $fm = new FileMaker('PFAM NEW', 'armontsys.ddns.net', 'Admin', 'enzio');


             $request = $fm->newFindAllCommand('LayContact');


            if(empty($_GET['skip']))
                {
                $skip=0; // Default shows the first record
                }
                else
                {
                $skip=$_GET['skip'];
                }
            if(empty($_GET['max']))
                {
                $max=1; // Default for record navigation is 1 record at time
                }
                else
                {
                $max=$_GET['max'];
                }
            $request->setRange($skip, $max);

            # Execute the find request
            $result = $request->execute();

            if (FileMaker::isError($result)) 
                {
                echo "<p>" . $result->getMessage() . "</p>";
                exit;
                }
            # Get the result record set as an array of record objects. This is technically your found set of records.
            $records = $result->getRecords();


        if(empty($_GET['skip']) or empty($_GET['max']))
            {
            $currentRecord = 1;
            }
            else
            {
            $currentRecord = $_GET['skip'] + $_GET['max'];
            }
        $found = $result->getFoundSetCount();
        $total = $result->getTableRecordCount();

        $_SESSION['testt'] = $found;
        echo '<h3 id="totalrecords"> Record ' . $currentRecord . ' of ' . $found . '</h3>';

        if(empty($_GET['Com']))
            {
            $firstName='';
            }
            else
            {
            $firstName=$_GET['Com'];
            }

                #skip = $_GET['skip'];
                if(empty($_GET['skip']))
                {
                $skip='0';
                }
                else
                {
                $skip=$_GET['skip'];
                }

            $_SESSION['skip_latest'] = $_GET['skip'] ;
            # FIRST RECORD 
            if ($skip > 0 )
                {
                //echo '<a href="' . $_SERVER['PHP_SELF'] . '?skip=0&max=1&FirstName=' . $firstName . '"><img src="Images/first_record.jpg" border=0/></a>';

                echo '<a id="first_record" href="' . $_SERVER['PHP_SELF'] . '?skip=0&max=1"><img src = "images/first_record.jpg" alt="first" id="first"/></a>';

                }
            else
                {




                //echo '<img src="Images/first_record.jpg" border=0/>';
                }   
            ?>  
            </td>
         <td>
        <?php
            # PREVIOUS RECORD
            $previous = $skip-1;
            if ($skip > 0)
                {
                //echo '<a href="' . $_SERVER['PHP_SELF'] . '?skip=' . $previous . '&max=1&FirstName=' . $firstName . '"><img src="Images/previous_record.jpg" border=0/></a>';

                echo '<a id="previous_info"  href="' . $_SERVER['PHP_SELF'] . '?skip=' . $previous . '&max=1"> <img src = "images/previous_record.jpg" alt="prev" id="prev"/> </a>';
            }
        else
            {
            //echo '<img src="Images/previous_record.jpg" border=0/>';


            }
        ?>
        </td>
        <td>
    <?php
#NEXT RECORD
        $next = $skip+1;
        if ($skip+1 < $found)
            {
            //echo '<a href="' . $_SERVER['PHP_SELF'] . '?skip=' . $next . '&max=1&FirstName=' . $firstName . '"><img src="Images/next_record.jpg" border=0/></a>';

                echo '<a  href="' . $_SERVER['PHP_SELF'] . '?skip=' . $next . '&max=1"> <img src = "images/next_record.jpg"  alt="next" id="next"/></a>';
            }


            else
                {
                //echo '<img src="Images/next_record.jpg" border=0/>';



                }
            ?>
            </td>
            <td>
            <?php
#LAST RECORD
                $last = $found-1;
                if ($skip+1 < $found)
                    {



                        echo '<a id="last_record" href="' . $_SERVER['PHP_SELF'] . '?skip=' . $last . '&max=1"><img src = "images/last_record.jpg" alt="last" id="last"/></a>';
                    }
                else
                    {
                    //echo '<img src="Images/last_record.jpg" border=0/>';
                    }


            foreach ($records as $record) 
                {


                    /////// Client information ////////////////
                    @$_SESSION['ID_record'] = $record->getRecordId();
                    @$_SESSION['Company'] =$record->getField('Company');
                    @$_SESSION['Address'] =$record->getField('Address Type 1');

                    echo '<h1>' . $_SESSION['ID_record'] .'</h1>';
                    echo '<h1>' . $_SESSION['Company'] .'</h1>';
                    echo '<h1>' . $_SESSION['Address'] .'</h1>';

            }
            ?>
             <input name="test_latest" id="latestpamore" value ="<?php echo $_SESSION['skip_latest']; ?>"/>




Ajax.js (I really don't know why when I navigate to the last record of page and click previous one, then it does not show correctly the data same goes on opposite when navigate the first record and click next.



        $(document).ready(function(){
            load_data();

            $(document).on('click', '#next', function(){
                 // we cancel the normal submission of the form    

                load_data();
                event.preventDefault();

            });



                $(document).on('click', '#first', function(){
                 // we cancel the normal submission of the form    


                $.ajax({
                    url: 'book_data.php',
                    method: 'GET',
                    data: 'skip=' + "0" +'&max=1',
                    success: function(res){
                        $('#load_data').html(res);

                    }
                });

                            event.preventDefault();
            });



                $(document).on('click', '#last', function(){
                 // we cancel the normal submission of the form    

                allrecords = parseInt($('#pleasef').val()) - 1;
                $.ajax({
                    url: 'book_data.php',
                    method: 'GET',
                    data: 'skip=' + allrecords +'&max=1',
                    success: function(res){
                        $('#load_data').html(res);

                    }
                });

                            event.preventDefault();
            });







                $(document).on('click', '#prev', function(){
                 // we cancel the normal submission of the form    

                info_previous = parseInt($('#skipinfo').val()) - 1;
                $.ajax({
                    url: 'book_data.php',
                    method: 'GET',
                    data: 'skip=' + info_previous +'&max=1',
                    success: function(res){
                        $('#load_data').html(res);
                        parseInt($('#skipinfo').val(info_previous + 0))

                    }
                });

                            event.preventDefault();
            });




        });



            function load_data(){

                info = parseInt($('#skipinfo').val()) + 0;
                $.ajax({
                    url: 'book_data.php',
                    method: 'GET',
                    data: 'skip=' +  info +'&max=1',
                    success: function(res){
                        $('#load_data').html(res);
                        parseInt($('#skipinfo').val(info + 1))

                    }
                });


            }




Is This A Good Question/Topic? 0
  • +

Replies To: PHP and Ajax navigation went incorrect to show data

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: PHP and Ajax navigation went incorrect to show data

Posted 14 November 2017 - 06:17 AM

                    /////// Client information ////////////////
                    @$_SESSION['ID_record'] = $record->getRecordId();
                    @$_SESSION['Company'] =$record->getField('Company');
                    @$_SESSION['Address'] =$record->getField('Address Type 1');


Why are you suppressing warnings? There could be useful information that you're missing.

Your code is repetitive. That should tell you that you can make it shorter and easier to debug if you use loops and/or functions.
        if(empty($_GET['skip']) or empty($_GET['max']))
            {
            $currentRecord = 1;
            }
            else
            {
            $currentRecord = $_GET['skip'] + $_GET['max'];
            }


So it looks like $_GET['skip']or $_GET['max'] is empty. Have you verified this?
Was This Post Helpful? 0
  • +
  • -

#3 jamescv31   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 06-October 13

Re: PHP and Ajax navigation went incorrect to show data

Posted 14 November 2017 - 06:14 PM

Thank for your reply, well to clarify it may sound repetitive but its based on Filemaker API source code; otherwise I managed to solve the problem by modifying the code from ajax.

The reason I use @ from Session variables is sometimes it shows undefined index when the data gathered from Filemaker Database is empty.

Thanks for the reply.

This post has been edited by jamescv31: 14 November 2017 - 06:15 PM

Was This Post Helpful? 0
  • +
  • -

#4 benanamen   User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 169
  • Joined: 28-March 15

Re: PHP and Ajax navigation went incorrect to show data

Posted 15 November 2017 - 12:05 AM

View Postjamescv31, on 14 November 2017 - 06:14 PM, said:

The reason I use @ from Session variables is sometimes it shows undefined index when the data gathered from Filemaker Database is empty.

Thanks for the reply.


Stop using @ to suppress errors and fix the code. Don't use a variable if you have not checked that it is is set.

By the way, your code is vulnerable to an XSS Attack. Do not use PHP_SELF. Just remove the action altogether.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1