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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:38:51+00:00 2026-05-21T06:38:51+00:00

I have barely created a script (as I’m just a beginner in ajax), in

  • 0

I have barely created a script (as I’m just a beginner in ajax), in fact I have copied some part (ajax script) of it from somewhere. But when I click on the the link (vote up / vote down) nothing happens not even the value in Mysql database changes, but on click the submit button I got a change in my MySql Database! Here is my code —

-:::- HTML PART (test.php) -:::-

<html>
       <title>
               TEST
       </title>
       <head>
              <script type="text/javascript" src="jquery.1.4.4.js"></script>
              <script type="text/javascript">
                        function vote(type)
                        {
                          $.ajax({
                              'url': 'test.func.php',
                              'type': 'POST',
                              'dataType': 'json', 
                              'data': {type: type},
                              'success': function(data)
                               {
                                   if(data.status)
                                   {
                                       if(data.voted)
                                        {
                                             $(document).ready(function () {
                                               $("span#status"+type).attr("innerHTML","You have voted up!");
                                             });
                                        }
                                        else
                                        {
                                             $(document).ready(function () {
                                               $("span#status"+type).attr("innerHTML","You have voted Down!");
                                             });
                                        }
                                    }
                               },
                               beforeSend: function()
                                 {
                                      $(document).ready(function () {
                                       $("span#status"+type).attr("innerHTML","Voting....");
                                      });
                                 },
                                  'error': function(data)
                                  {
                                    $(document).ready(function () {
                                      $("span#status"+type).attr("innerHTML","An error occureed");
                                    });
                                  }
                                });
                          }
                </script>
       </head>
       <body>
              <a href="#" onclick="vote('up')" > Vote Up </a>
              <span id="status_up" ></span>
              <br>
              OR
              </br>
              <a href="#" onclick="vote('down')" > Vote Down </a>
              <span id="status_down" ></span>
       </body>
</html>

-:::- PHP PART (test.func.php) -:::-

<?php

    function db_connect($i)
    {
         if(isset($i))
         {
              if(mysql_connect('localhost', 'root', 'root'))
              {
                   if(mysql_select_db($i))
                   {
                        return;
                   }
                   else
                   {
                        echo 'ERROR';
                   }
              }
              else
              {
                  echo 'ERROR';
              }
         }
         else
         {
             echo 'ERROR';
         }
    }
if($_POST)
{
 db_connect('tests');
 $vote_type=$_POST['type'];
 $post_id = '123';
 $query = mysql_query("SELECT * FROM test WHERE post_id='$post_id'");
 $cur_vote_get = mysql_fetch_array($query);
 $vote_up = $cur_vote_get['votes']+1;
 $vote_down = $cur_vote_get['votes']-1;
 if($vote_type=='up')
 {
   mysql_query("UPDATE test SET votes='$vote_up' WHERE post_id='$post_id'");
   return json_encode(array("status" => true, "voted" => true));
 }
 elseif($vote_type=='down')
 {
   mysql_query("UPDATE test SET votes='$vote_down' WHERE post_id='$post_id'");
   return json_encode(array("status" => true, "voted" => false));
 }
}

?>

JavaScript Error solved!

Everything Solved!

As I’m very new to Ajax so I’m not able to find any solution to 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-21T06:38:52+00:00Added an answer on May 21, 2026 at 6:38 am

    You’re not echoing the json, you’re echoing random text (‘Vote up’), which will break the ajax json parser.

    Remove the echo 'Voted!'; and try echo json_encode(array("status" => true, "voted" => false));.

    Also you can skip the SELECT query and do : UPDATE test SET votes=votes+1 WHERE post_id='$post_id'

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

Sidebar

Related Questions

I have a script that appends some rows to a table. One of the
i have a input tag which is non editable, but some times i need
Source RegexOptions.IgnoreCase is more expensive than I would have thought (eg, should be barely
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have had to write my first proper multithreaded coded recently, and realised just how
I noticed that they have a list of plugins here: https://wiki.mozilla.org/Bugzilla:Addons#Integration_with_Source_Code_Management_programs ...But most of
I have a new web app that is packaged as a WAR as part
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution

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.