为什么我的 Ajax 发布脚本不起作用?

发布于 2024-10-31 17:42:39 字数 4349 浏览 1 评论 0原文

我几乎没有创建一个脚本(因为我只是 ajax 的初学者),事实上我已经从某个地方复制了它的某些部分(ajax 脚本)。但是,当我单击链接(投票赞成/投票反对)时,没有任何反应,甚至 Mysql 数据库中的值也没有更改,但是单击提交按钮时,我的 MySql 数据库中发生了更改!这是我的代码 --

-:::- 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 错误已解决!

一切都已解决!

由于我对 Ajax 很陌生,所以我无法找到任何解决方案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小苏打饼 2024-11-07 17:42:39

如果这完全是您的代码(我的意思是,如果您只是从真实的 .php 文件中复制/粘贴它),则第一个 .php 文档的第二个脚本块中存在拼写错误。

javasript-> javasCript

尝试一下。

If that's STRICTLY your code, (I mean, if you've just literally copy/pasted it from your real .php file), there's a typo error in the second script block of the first .php document.

javasript -> javasCript

Give it a try.

倚栏听风 2024-11-07 17:42:39

您不是回显 json,而是回显随机文本(“投票”),这会破坏 ajax json 解析器。

删除 echo 'Voted!'; 并尝试 echo json_encode(array("status" => true, "voted" => false));

您也可以跳过SELECT查询并执行:UPDATE test SET votes=votes+1 WHERE post_id='$post_id'

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'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文