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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:52:02+00:00 2026-05-21T04:52:02+00:00

I am trying to increment a number and update the database and then display

  • 0

I am trying to increment a number and update the database and then display the number without refreshing the page. I am just wondering, is there a way to read/update MySQL using JavaScript?

I tried searching around, and it seems you have to call another program that changes the values for you. Right now I have

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

echo "connected!<br>";

$extract = mysql_query("SELECT * FROM articles")or die ("Not working");

//$numrows = mysql_num_row($extract);

while ($row = mysql_fetch_assoc($extract)) {

    $id = $row['id'];
    $article = $row['article'];
    $thumbsup= $row['thumbs_up'];
    $thumbsdown = $row['thumbs_down'];
    $date = $row['date_time'];
    $username = $row['username'];
    mysql_query("UPDATE articles SET thumbsup = 1");
}
?>

So I am wondering, once this page updates the database, how do you get the updated value into the JavaScript function? Right now I am just reading it from a file

xmlhttp.open("GET", "http://localhost/ajax/folder5/includes/a.txt", true);

Thanks a lot for your help!

  • 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-21T04:52:03+00:00Added an answer on May 21, 2026 at 4:52 am

    Since you already have a server-side script that fetches data all you need to do is return it as a response to your XMLHttpRequest and handle it by using a callback function. Assuming you want to return $id, $article, $thumbsup ..etc you can put it in an array and encode it, as suggested you can use json_encode(). In your PHP script you can do something like:

    $Response = array(
         'Id' => $id,
         'Article' => $article',
         'ThumbsUp' => $thumbsup
    )
    
    echo json_encode($Response);
    exit;
    

    And in your Javascript you may need to add a response handler, like:

    xmlHttp.onreadystatechange = function() {
    
     if (syndLinkRequest.readyState != 4)
        return;
    
      var result = xmlHttp.responseText;
    
      /* if you've returned javascript instead of xml or text, 
        you can eval(result) to access the javascript variables returned.
      */
    
      // do your thing
      }
    

    Of couse you need to change the way you request data instead of using GET to text file, in this case you may want to request directly to the PHP script and return the result:

    xmlhttp.open("GET", "/ajax/update.php", true); // this will return the response
    

    Essentially, that’s all you need.
    For more brief example you can refer here.

    Also check jQuery’s ajax functions as it may help you a lot.

    **EDIT**

    Added additional info.
    Since we’re returning a result which is JSON (by using json_encode), you need to add a header for the content type, add this at the top most part of your script just below the <?php part:

    header('Content-type: application/json');
    

    And based on the example above assuming we have this as the response:

    $Response = array( 'Id' => $id,  'Article' => $article,'ThumbsUp' => $thumbsup);
    echo json_encode($Response);
    exit;
    

    Once you receive this in your callback function this will be translated to something like (with sample values):

    {"Id":1,"Article":20,"ThumbsUp":30}
    

    which is given to the result variable:

    var result = xmlHttp.responseText;
    

    To be sure we get the response evaluated we will use eval() to the result string so you can get values off of it by accessing properties. Take this for example:

    var result = eval(xmlHttp.responseText);
    

    Once that’s done you can access properties from the result variable like result.ThumbsUp which gives you 30 based on the example. You can then assign that value wherever you need it to be like: document.getElementById('c').innerHTML = result.ThumbsUp; I hope this gives you a clearer idea.

    A note to using eval(), read this.

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

Sidebar

Related Questions

I am trying to update the error message for a CustomValidator that uses a
just trying to tie up a few loose odds and ends here. I have
I'm trying to do a simple test php script for sessions. Basically it increments
Trying to setup an SSH server on Windows Server 2003. What are some good
Trying to get my css / C# functions to look like this: body {
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to make a make generic select control that I can dynamically add elements
Trying to keep all the presentation stuff in the xhtml on this project and
Trying to make a MySQL-based application support MS SQL, I ran into the following
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation

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.