0

Is there anyway to load the newly inserted rows to the database without refreshing the page using jQuery? I am able to send the data to the database without refreshing using jquery but I am stuck at showing that data back without refreshing the page. How do I display the data from the table without refreshing page and make it appear under the table in index.php which I have to display the retrieved data? Thanks

This is my index.php

<html>
        <head>
            <script src="jquery.js" type="text/javascript"></script>
            <script type="text/javascript">
            $(document).ready(function(){
            $('#submit').click(function(){
            $.ajax({
            type: "POST",

            url: 'send.php',
            data: "user=" + $('#user').val() + "&comment=" + $('#comment').val(),
            success: function(data){

            $('input[type=text]').val('')
            $('#status').html(data);

            }

            });
            });
            });
            </script>
                            </head>
                     <body>
                      <div>
        <input type="text" name="user" id="user">
        <input type="text" name="comment" id="comment">
        <input name="submit"  type="submit" id="submit">

        <div id="status"></diV>
        </div>
        <br>


        <?php
       $con=mysqli_connect("localhost","root","","user");
        // Check connection
        if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

        $result = mysqli_query($con,"SELECT * FROM comments");

    echo "<table  width='640' >
    <tr>

    </tr>";

    while($row = mysqli_fetch_array($result))
      {
      echo "<tr >";
      echo "<td  style='vertical-align:text-top'>" . $row['user'] . "</td>";

      echo "<td><br><br><br>" . wordwrap($row['comment'], 90, '<br>',true) . "</td>";
      echo "</tr>";
      }
    echo "</table>";

    mysqli_close($con);
    ?> 

And here is my send.php which submits the data to the table.

<?php
                $mysqli = new mysqli("localhost", "root", "", "user");
                    if ($mysqli->connect_errno) {
                    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
                }

                $username = $_POST["user"];
                $comment = nl2br($_POST['comment']);
                $stmt = $mysqli->prepare("INSERT INTO comments (user, comment) VALUES (?,?)");
                $stmt->bind_param('ss', $username, $comment);
                $stmt->execute();

                echo"comment posted successfully";

                ?>
2
  • jQuery load() Commented Jun 21, 2013 at 22:48
  • Check the "related" sidebar -- there are many ajax examples. Your best bet is to understand how they work so that you can apply the same principles to your project. Commented Jun 21, 2013 at 22:59

3 Answers 3

2

This is a brief example for fetching data from a mysql database using JQuery AJAX and php. JQuery AJAX allows us to update a page's content without reloading the page:

http://openenergymonitor.org/emon/node/107

http://viralpatel.net/blogs/jquery-ajax-tutorial-example-ajax-jquery-development/

Sign up to request clarification or add additional context in comments.

Comments

0

make send.php send you back the data you need to load the newly inserted row.

Let's say you inserted a comment that says "Hello world". If nothing goes wrong, send.php could, for instance, echo the content of the comment (Hello world in this case), and you could make use of that in the success function (from the data parameter).

look at the first answer in this question, might be useful.

1 Comment

fyi you can also just send back a string of html and just append it to the dom somewhere
0

You should append the new comment to the table with jquery in the success callback function of your ajax request.

To append:

$("table").append("<tr><td style='vertical-align:text-top'>"+$('#user').val()+"</td><td><br><br><br>"+$('#comment').val()+"</td></tr>");

Keep the thing you want to append in one line of code and make sure you don't clear the values of #user and #comment before you include them to the append string.

1 Comment

how? I have no idea about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.