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 7827443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:52:46+00:00 2026-06-02T09:52:46+00:00

I need to use AJAX to save user input comments instantly to the database.

  • 0

I need to use AJAX to save user input comments instantly to the database.
This is the comment box.

<div class="comment">
        <form action="comment.php">
        <textarea cols="35" name="comments"> Give us comment </textarea>
        <input type="button" value="comment" onclick="ajaxFunction()">
        </form>                 
 </div>

This is the php code

<?php

$link = mysql_connect("localhost","root","");
mysql_select_db("continental_tourism");

$comments = $_POST['comments'];

if($comments == "")
echo "Please type your comment";
else
{
$query = "insert into comment(comment) values('$comments') ";
mysql_query($query);
} 
return;
?>

I need to know how this should be changed.
Thank You

  • 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-02T09:52:47+00:00Added an answer on June 2, 2026 at 9:52 am

    I would chnage your HTML like this

    <textarea cols="35" id="comments"> Give us comment </textarea>
    <input type="button" value="comment" id="btnSave" />
    

    And the script

    $(function(){
     $("#btnSave").click(function(e){
      e.preventDefault();
      $.post("yourphpfile.php", { comments : $("#comments").val() } ,function(data){
         alert(data);
      });  
     }); 
    });
    

    The above script will send an ajax request to yourphpfile.php with the value present in the textarea with id comment. Then once it gets some data back from the server page. it justs alerts it ( you may show this in a seperate div if you want ). You should echo the response from your php file after saving the data to database.

    As bracketworks already mentioned, you should be careful about SQL injections while saving data which is being read from the querystring value. do proper sanitization before putting it in a query. Sorry i am not a php guy. so not sure how to do that.

    Don’t forget to include the jQuery library to your page.

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
    

    You may use firebug console for debugging your script error if you run into any.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to use ajax feature to load an external div element (an external
I need to use the $.ajax() call within jQuery to post a little bit
While debugging jQuery apps that use AJAX, I often have the need to see
I need to use YQL (Yahoo Query Language) to perform a cross domain ajax
I've made an application that makes full use of ajax, and what I need
So, I need use this event so I can navigate trought blog posts. I
I need to use this formula to calculate the upper limit and lower limit
i have code like this. which is actually a form .which lets user updates
I want to upload an image use something like <form ...> <input type=file id=upload></input>
I already search about this and I cannot use ajax in file uploading, but

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.