Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7066149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:02:59+00:00 2026-05-28T05:02:59+00:00

I am trying to display data after being submitted with ajax. The ajax works

  • 0

I am trying to display data after being submitted with ajax. The ajax works so far when submitting, but I have to refresh to see it.

Here’s the jquery:

$('#submit-quote').live("submit", function(){
    var formdata = $(this).serialize();
    $.post("add.php", formdata, function(data) {
        console.log("success"); 
    });
    return false;
});

The php in add.php:

require('includes/connect.php');

$quote = $_POST['quote'];
$quotes = mysql_real_escape_string($quote);

echo $quotes . "Added to database";

mysql_query("INSERT INTO entries (quote) VALUES('$quotes')")
or die(mysql_error());

Here’s the HTML/PHP that I use to fetch the data and display it:

<?php 
    require("includes/connect.php");

    $result = mysql_query("SELECT * FROM entries", $link);
    while($row = mysql_fetch_array($result)){ ?>
        <div class="quote-wrap group">
            <span>Like</span>
            <div class="quote">
                <p>
                <?php echo htmlentities($row['quote']); ?>
                </p>
            </div><!-- /.quote -->
        </div><!-- /.quote-wrap -->

 <?php } ?>

If needed, here’s the form:

<form id="submit-quote" method="post" >
     <h2> Submit A Quote </h2>
     <textarea name="quote">
     </textarea>
     <input type="submit" name="submit" value="Submit!">
</form>

Ajax works when submitting, but I need to display it after being sent also, how can I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:03:00+00:00Added an answer on May 28, 2026 at 5:03 am

    The data variable in your success callback function stores the server response. So to add the server response to the DOM:

    $(document).delegate("'#submit-quote'", "submit", function(){
        $.post("add.php", $(this).serialize(), function(data) {
            $('.inner').append('<div class="quote-wrap group"><span>Like</span><div class="quote"><p>' + data + '</p></div></div>');
        });
        return false;
    });
    

    If you need to use event delegate (e.g. the form isn’t always present in the DOM) then use .delegate() instead of .live() as the latter has been depreciated as of jQuery 1.7.

    Also you don’t really need to cache $(this).serialize() in a variable since it is only being used once (creating the variable is unnecessary overhead).

    Since your PHP code is outputting echo $quotes . "Added to database";, the server response will be the quote with “Added to database` appended to the string, which will be added to your list of quotes.

    UPDATE

    $(document).delegate("'#submit-quote'", "submit", function(){
        var quoteVal = $(this).find('[name="quote"]').val();
        $.post("add.php", $(this).serialize(), function() {
            $('.inner').append('<div class="quote-wrap group"><span>Like</span><div class="quote"><p>' + quoteVal+ '</p></div></div>');
        });
        return false;
    });
    

    Notice that I am no longer referencing the server response (in fact I removed the data variable all together). Instead I am saving the value of the name="quote" element within the form being submitted and using it after the AJAX request comes back (this way the quote is added to the database before being added to the DOM). You could move the .append() code outside the success callback to run it right as the form is submitted (in the submit event handler).

    UPDATE

    If you want to create a DOM element to append rather than concocting a string:

    $(document).delegate("'#submit-quote'", "submit", function(){
        var quoteVal = $(this).find('[name="quote"]').val();
        $.post("add.php", $(this).serialize(), function() {
    
            //create parent div and add classes to it
            $('<div />').addClass('quote-wrap group').append(
    
                //append the "like" span to the parent div
                $('<span />').text('Like');
            ).append(
    
                //also append the .quote div to the parent div
                $('<div />').addClass('quote').append(
    
                    //then finally append the paragraph tag with the quote text to the .quote div
                    $('<p />').text(quoteVal)
                )
    
            //then after we're done making our DOM elements, we append them all to the .inner element
            ).appendTo('.inner');
        });
        return false;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to have a tree display custom data that looks like this.
Here is my current code. I'm trying to display data from three separate queries
I'm trying to display data from a database and I want to know what
I'm trying to display some data from a list sharepoint and then using jquery
I am having issues trying to display the data in the console.. i just
Hi i am new to jsp & was trying to display some data from
Problem: The data I'm trying to display is essentially a collection of collections of
I am trying to display a table containing data from my db in a
I'm trying to work with a YII CGridview to display some data. This is
I'm trying to inherit QSqlTableModel to make data im my table display in way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.