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

  • SEARCH
  • Home
  • 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 8574427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:28:20+00:00 2026-06-11T19:28:20+00:00

I made a simple sample on how to insert using AJAX and retrieving it

  • 0

I made a simple sample on how to insert using AJAX and retrieving it then append it in a <div> after getting it. But I am having trouble on getting all the content of the table, it’s returning a null values.

<div id="wrap-body">
  <form action method="post">
    <input type="text" name="username" id="username">
    <input type="text" name="msg" id="msg">
    <input type="button" id="submit" value="Send">
  </form>
  <div id="info">
  </div>
</div>

jQuery:

<script>
    $(document).ready(function (){
        $('#submit').click(function (){
            var username = $('#username').val();
            var msg = $('#msg').val();

            $.ajax({
                type: 'POST',
                url: 'get.php',
                dataType: 'json',
                data:'username='+username+'&msg='+msg,
                success: function (data){
                    $('#info').append("<p> you are:"+data.username+"</p> <p> your message  is:"+data.mesg);
                }
            });
        });
    });
</script>

PHP:

<?php 
$host='localhost';
$username='root';
$password='12345';
$db = 'feeds';

$connect = mysql_connect($host,$username,$password) or die("cant connect");
mysql_select_db($db) or die("cant select the".$db);

$username = $_POST['username'];
$msg = $_POST['msg'];

$insert = "INSERT INTO info(user_name,message) VALUES('$username','$msg')";

if(@!mysql_query($insert)){
    die('error insertion'.mysql_error());
}

$get = "SELECT * FROM info ";

$result=mysql_query($get)or die(mysql_error());

while ($row = mysql_fetch_array($result))
{
    $return  = $row['user_name'];
    $return = $row['message'];
}
echo json_encode($return);
?>
  • 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-06-11T19:28:21+00:00Added an answer on June 11, 2026 at 7:28 pm

    Your while should create array and then do json_encode

    Try below code

    $data=array();
    while ($row = mysql_fetch_array($result))
    {
    $data[] = array(
       'username'=>$row['user_name'],
       'mesg'=>$row['message']
    );
    }
    
    echo json_encode($data);
    exit
    

    Now write your javascript success handler as below

    $.ajax({
                    type: 'POST',
                    url: 'get.php',
                    dataType: 'json',
                    data:'username='+username+'&msg='+msg,
                    success: function (data){
                       $.each(data, function(i, item) {
                         $('#info').append("<p> you are:"+data[i].username+"</p> <p> your message  is:"+data[i].mesg);
                       });​
    
                    }
                });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably stupid simple, but for some reason I'm having trouble getting it
I made simple email in wordpress but it would only send email to the
Visual Studio 2008 (vb.net) I made simple anivirus but when I make Full scan
OK, I have made simple rectangular using OpenGL, and it looks pretty simple glNormal3f(0.0f,
On Windows, testing different OSes is made simple using VMs. Is there a simple
I made a sample application using windows azure dedicated caching ( preview ). The
I made a simple clock signal using goroutines: func clockloop(ch chan byte) { count
I made a simple news system with comments using PHP and MySQL, and it
I have made a very simple page using google maps API with several fields
I just made a very simple test app using documentation from MSDN. All I

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.