-1

I have a basic idea on how to do AJAX request but what I really want is to send a response immediately after fetching data, not waiting for all of the mysql query results to finish. Is there any way to do this? Can you give an idea?

What I really imagined is for example:

When the document is ready, then the client shall send request of all the list in the database. While PHP on the server side is fetching data, it'll send the rows as json then the client keeps on parsing those response until the server side cannot fetch rows anymore.

Is it possible? If so, do you have an idea?

1 Answers1

-1

I have small piece of code for this. might be you accept!

JQuery:

<script>

    $(document).ready(function() {
        view();
    });

    function register()
    {
        var f = $('#fname').val();
        var l = $('#lname').val();

        $.ajax({
            url:'index.php/control/insert',
            data :{fname : f, lname : l},
            type:'post',
            success:function(msg){
                return view();
            }
        });
    }

  function view()
  {
        $.ajax({
            url:'index.php/control/show',
            data :{},
            type:'post',
            success:function(msg){
                $('#table').html(msg);
            }
        });
    }
</script>

HTML Form:

<form action="" method="post">
    <table align="center" border="11">
        <tr>
            <td>First name</td>
            <td><input type="text" name="fname" id="fname" /></td>
        </tr>
        <tr>
            <td>Last Name</td>
            <td><input type="text" name="lname" id="lname" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="button" id="submit" name="submit" onclick="register()" value="Register"></td>
        </tr>
    </table>
</form>

My Controller for Mysql function:

public function insert()
{
    $sql = "INSERT INTO MyGuests (firstname, lastname) VALUES ('".$_REQUEST['fname']."', '".$_REQUEST['lname']."')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    }
}
public function show()
{
    $sql = "SELECT * FROM MyGuests";

    if ($data = $conn->query($sql) === TRUE) {
        echo "New record created successfully";
    }

    echo "<br><table border='1' align='center'>
            <tr>
                <th>Firstname</th>
               <th>Lastname</th>
            </tr>";
            foreach($data as $s)
            {
               echo "<tr><td>".$s->fname."</td>";
                echo "<td>".$s->lname."</td></tr>";
            }
    echo "</table>" ;
}

make sure code is not tested this is the sample for your requirement, aim to aware call immediate function. here i have called view() function just after inserting data in to the database.

Geee
  • 2,217
  • 15
  • 30