最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Run php file through jquery link click - Stack Overflow

programmeradmin4浏览0评论

I cannot get this to work, the page already has other jquery functions running pletely fine so i know jquery is working (jplayer and lazyload work fine)

here is my javascript function:

<script type="text/javascript">
function upone() {
    $.get("upone.php");
    return false;
}
</script>

Here is my link

<a href="#" onclick="upone();">Like</a>

and my php file "upone.php is :

<?php
include("include/config.php");
include("include/functions/import.php");

if(isset($_SESSION['USERID'])&&$_SESSION['USERID']>=0&&is_numeric($_SESSION['USERID']))
{
    $USERID=$_SESSION['USERID'];

    $query="INSERT INTO audio_like SET USERID='".mysql_real_escape_string($USERID)."'";
    $result=$conn->execute($query);
}

?>

the php file runs fine if i manually link to it, clicking on the link also results in the page refreshing with the # added to the page. Any ideas?

I cannot get this to work, the page already has other jquery functions running pletely fine so i know jquery is working (jplayer and lazyload work fine)

here is my javascript function:

<script type="text/javascript">
function upone() {
    $.get("upone.php");
    return false;
}
</script>

Here is my link

<a href="#" onclick="upone();">Like</a>

and my php file "upone.php is :

<?php
include("include/config.php");
include("include/functions/import.php");

if(isset($_SESSION['USERID'])&&$_SESSION['USERID']>=0&&is_numeric($_SESSION['USERID']))
{
    $USERID=$_SESSION['USERID'];

    $query="INSERT INTO audio_like SET USERID='".mysql_real_escape_string($USERID)."'";
    $result=$conn->execute($query);
}

?>

the php file runs fine if i manually link to it, clicking on the link also results in the page refreshing with the # added to the page. Any ideas?

Share Improve this question edited Feb 13, 2012 at 18:10 CBusBus 2,3592 gold badges18 silver badges28 bronze badges asked Feb 13, 2012 at 18:03 JimmyBanksJimmyBanks 4,7189 gold badges48 silver badges73 bronze badges 2
  • What do you see in firebug or your JavaScript console? – Michael Berkowski Commented Feb 13, 2012 at 18:10
  • The only thing i see in the javascript console is a constant error caused by the google+ button, but removing this doesnt change anything – JimmyBanks Commented Feb 13, 2012 at 18:17
Add a ment  | 

2 Answers 2

Reset to default 6

EDIT: Misunderstood the question

Using the code below will get rid of the issue where the page refreshes after you click the link. The reason it was happening previously is that the onclick attribute wasn't returning false, in other words this would have been the right code: <a href="#" onclick="return upone();">Like</a>. The code below does the same thing, but structured a little better.

HTML:

<a id="like" href="#" >Like</a>

JS:

$("#like").click(function(){
   $.get("upone.php");
   return false;
});

Rather than using inline javascript, try doing something like this.

Give the "Like" link a class, example:

<a href='#' class='likeLink'>Like</a>

An instead of your current section, use something like this:

<script>
    $(function() {
        $('.likeLink').click(function() {
            $.get('upone.php', function(data) {
                alert("Server Returned: " + data);
            });
            return false;
        });
    });
</script>

If the alert message returns what you expect from the PHP page, you can just ment it out for production.

By the way, I typed this out a long time ago today and forgot I was working on it. Sorry if it's already solved.

发布评论

评论列表(0)

  1. 暂无评论