1

Hi all I am fairly new to programming. I have been teaching myself php and css and is trying to combine that with some jQuery.

I have a site with a list of items that I want to allow the user to vote up. (www.blueskycouncil.com you can log in with stack/this)

currently I am updating the database by sending this:

<a href=\"idea_karma.php?pagetype=index&karmatype=ideaspace&id={$row['id']}&sort_id=$sort_id\">

to idea_karma.php, check if the user has already voted on it and if not update the database.

This works fine (noob code aside)

Now I want to do it with jQuery instead so that I only update the point rather than the entire page.

Two two of the variables are dynamic (id and sort_id), the specific value get's assigned in a loop.

My question is how to I approach this?

I have tried a couple of things but I can't seem to get them to work.

This is the sript

<script type="text/javascript"> 
function get() { 
$.get('idea_karma.php', {"pagetype": index, "karmatype": ideaspace, "id": id, "sort_id":   sortid},

function (output) {
    $('#karma').html(output).show();
    }};

}

This is where I do the call to the script

<div class=\"karma-btn\">
<a href=\"idea_karma.php?pagetype=index&karmatype=ideaspace&id= {$row['id']}&sort_id=$sort_id\" onClick=\"get();\">
<img src=\"images/btn_lrg_karma.png\" alt=\"Alternative text\">
<span class=\"voted\"><div id="karma">{$row['karma']</div>}</span></a>                                                      </div>
3
  • 1
    Show us what you tried. Post some code. Commented Oct 27, 2010 at 17:12
  • Bear in mind that using JavaScript to show the actions will still require a php (or other server-side) intermediary to effect the changes. Commented Oct 27, 2010 at 17:24
  • there are a few problems... for one, the variables id and sortid do not exist in your get() function. Also, index and ideaspace are strings, you the need quotes. See my example below... Commented Oct 27, 2010 at 17:31

4 Answers 4

4

What you need to do is change that link so that it will call a javascript function with the proper parameters when it is clicked. The javascript function will do the AJAX, and look something like this:

function updateKarma(id, sortId){
  $.post("idea_karma.php", 
  {pagetype: "index", karmatype: "ideaspace", id: id, sort_id: sortId}, 
  function(){
    //in here you can do you stuff when the call returns
    alert("karma updated");
  });
}

Then, your link code will look like:

<a href=\"javascript:void(0);\" onclick=\"updateKarma('{$row['id']}', '$sort_id')\">

I think this is what you're after?

Sign up to request clarification or add additional context in comments.

4 Comments

I became late in providing same solution as yours. :)
The slashes are because he had slashes in the example... I'm assuming that link is inside a php string enclosed with double quotes... so the literal double quotes need to be escaped.
@Alpesh it's amazing how quickly answers happen on SO. I saw this question with zero responses and thought I could answer it...while I'm typing my answer 3 other answers appear and the question is marked as solved before I finish typing. Amazing.
@Jonathan Mayhak: so true. But this is the actual advantage of stack overflow.
2

You can assign an onclick function to the anchor as below -

<a href="#nodo" onclick="update_vote('<?=$row['id']?>','<?=$sort_id?>')">vote</a>

Then you can write a function below in your js file -

function update_vote(id,sort_id){


//Now you can call the url you were calling through the of the jquery.

// You can update spection section in page in the `sucess` callback function of ajax.

}

Detailed documentation of ajax is available at - http://api.jquery.com/jQuery.ajax/

Comments

2

The following is the basic idea to get you going with ajax:

function doKarma(id, sort_id) {
    var keyValue = {
        'method' : 'doKarma',
        'pageType' : 'index',
        'karmaType' : 'ideaspace',
        'id' : id,
        'sort_id' : sort_id
    };    
    $.post('/idea_karma.php', keyValue, function(xml) {
        var stat = $(xml).find('rsp').attr('stat');
        // stat will represent whether your api call failed or was a success
        if (stat === 'ok') {
            // update your counter
            alert('success');
        }
        else {
            // user has already voted or something went wrong
            alert('failed');
        }
    },'xml');
}

This is an example of your idea_karma.php page

<?php

if ($_POST['method'] == 'doKarma') {
    //perform update of karma

    header("Content-type: text/xml");
    // if success
    echo '<rsp stat=\"ok\"></rsp>';
    // else echo '<rsp stat=\"error\"></rsp>';
}

?>

1 Comment

thnx. Someone beat you to the "correct" answer but all answers are much appreciated.
0

Take a look at the jQuery API for Ajax, Get, Post. If you need specific help with code you already wrote, I would recommend posting it and we can help you troubleshoot.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.