【问题标题】:jQuery, PHP: Calling a PHP script from jQuery, but it doesn't appear to be executingjQuery,PHP:从 jQuery 调用 PHP 脚本,但它似乎没有执行
【发布时间】:2011-01-21 21:25:34
【问题描述】:

我在这里使用本网站上的源代码:http://webtint.net/tutorials/5-star-rating-system-in-php-mysql-and-jquery/comment-page-1/#comment-2562

一个很棒的资源,我想你们都会同意的,但我的问题是我的 Javascript 似乎并没有真正执行 PHP 脚本...

当我在 Chrome 的调试器中为倒数第二行(在 } 之前)添加断点时; ):

$("[id^=rating_]").children("[class^=star_]").click(function() {

        var current_star = $(this).attr("class").split("_")[1];
        var rid = $(this).parent().attr("id").split("_")[1];

        $('#rating_'+rid).load('http://localhost:8888/fivestars/send.php', {rating: current_star, id: rid});


    });

如您所料,它成功地停在断点处。但是我无法让 send.php 真正做任何事情,它根本不会!正如您从代码中看到的那样,我使用了绝对 URL,但它不适用于相对 URL,或者只是 send.php 在那个地方。我在 send.php 中放置了将“成功”写入数据库的代码,这样我就可以测试它是否正在执行 - 但显然不是。

有人有什么想法吗?

谢谢!

杰克

编辑:刚刚尝试使用此代码代替我之前使用的代码:

$('#rating_'+rid).load('send.php', function () {alert('Load was performed.');});

对话框显示正确,所以我相信它可以正常工作。

send.php的PHP代码供参考:

<?php

$rating = (int)$_POST['rating'];
$id = (int)$_POST['id'];

$this->db->query("INSERT INTO code (rating) VALUE (8)");
$query = $this->db->query("SELECT * FROM code WHERE id = '".$id."'");

while($query->row()) {

    if($rating > 5 || $rating < 1) {
        echo"Rating can't be below 1 or more than 5";
    }

    elseif(isset($_COOKIE['rated'.$id])) {
        echo"<div class='highlight'>You've already voted, sorry!</div>";
    }
    else {
        setcookie("rated".$id, $id, time()+60*60*24*365);

//      $total_ratings = $row['total_ratings'];
        $total_ratings = $query->row('total_ratings');
        $total_rating = $query->row('total_rating');
        $current_rating = $query->row('rating');

        $new_total_rating = $total_rating + $rating;
        $new_total_ratings = $total_ratings + 1;
        $new_rating = $new_total_rating / $new_total_ratings;
        // Lets run the queries. 

        $this->db->query("UPDATE test SET total_rating = '".$new_total_rating."' WHERE id = '".$id."'");
        $this->db->query("UPDATE test SET rating = '".$new_rating."' WHERE id = '".$id."'");
        $this->db->query("UPDATE test SET total_ratings = '".$new_total_ratings."' WHERE id = '".$id."'");

        echo"<div class='highlight'>Thanks for your vote!</div>";

    }
}

?>

抱歉,这有点乱,但我一直在将默认 PHP 代码迁移到我的 CodeIgniter 库。

【问题讨论】:

  • 您正在处理您的页面从 (localhost:8888) 运行的同一服务器和端口?
  • 抱歉,您能详细说明一下吗?我不太明白...它正在我的Mac上通过MAMP运行,访问地址是localhost:8888 ...

标签: php javascript jquery mysql


【解决方案1】:

首先要检查来自网络服务器的访问和错误日​​志,以查看请求是否真正到达网络服务器。

【讨论】:

  • 记录救援! [27-Feb-2010 22:25:05] PHP 致命错误:在第 6 行的 /Users/Jack/Sites/howCode.com/fivestars/send.php 中不在对象上下文中使用 $this 谢谢 :)跨度>
猜你喜欢
  • 2013-02-04
  • 2011-01-16
  • 1970-01-01
  • 2017-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
相关资源
最近更新 更多