Ajax Posting

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Pezmc
Forum Commoner
Posts: 53
Joined: Mon Nov 06, 2006 2:15 pm

Ajax Posting

Post by Pezmc »

I can currently using starbox and an ajax event to save a users rating on a video they have seen. However I also need to post the id of the video to the back end php.
How can I do this? What is the safest way to do this as well so I can prevent the user from modifying the js?

Code: Select all

<html>
<link rel="stylesheet" type="text/css" href="css/starbox.css" />
<script src='js/prototype.js' type='text/javascript'></script>
<script src='js/scriptaculous/scriptaculous.js?load=effects' type='text/javascript'></script>
<script src='js/starbox.js' type='text/javascript'></script>
<script type="text/javascript">
document.observe('starbox:rated', saveStar);
function saveStar(event) {
  new Ajax.Request('savestar.php', {
    parameters: event.memo.rating + '&id=1',
    onComplete: function() {}
  });
}
</script>
    <div class='star'>
            <div id=star><img src='images/loading.gif' alt=''/></div>
            <script language='javascript' type='text/javascript'>
            new Starbox('star', 5.6011, { overlay: 'pointy.png', className: 'pointy', identity: 'star', max: 8, buttons: 8, stars: 8, total: 5, indicator: '#{average} average after #{total} votes'} );
            </script>
    </div>
</html>
 
I have tried this but I don't know why it doesn't work. I can pick up the id variable but not the rated variable.

Code: Select all

parameters: event.memo.rating + '&id=1',
I hope you can help.
Pezmc
Forum Commoner
Posts: 53
Joined: Mon Nov 06, 2006 2:15 pm

Re: Ajax Posting

Post by Pezmc »

Bump.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Ajax Posting

Post by kaszu »

What's the value of the 'event.memo.rating' ? It should be "some_key=some_value", is it like that?
Pezmc
Forum Commoner
Posts: 53
Joined: Mon Nov 06, 2006 2:15 pm

Re: Ajax Posting

Post by Pezmc »

Yes it is it contains 5 things all formatted as ?name=whatever&rating=8&average=5.24 etc... I thought adding + '&id=7' would work but it just makes the previous not work so you only get the id.
I have put in a tempory solution atm that means the ajax posts to sendscore.php?id=6 but I would prefer it all to be sent as a post rather than have the id as a get.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Ajax Posting

Post by kaszu »

I guess event.memo.rating is an object, not a string. That could be the reason for your problem.
Pezmc
Forum Commoner
Posts: 53
Joined: Mon Nov 06, 2006 2:15 pm

Re: Ajax Posting

Post by Pezmc »

If that is the problem how can I send the id as well?
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Ajax Posting

Post by kaszu »

If it's an object, then you can do it like this:

Code: Select all

var params = Object.clone(event.memo.rating);
params.id = 7;
new Ajax.Request('savestar.php', {
    parameters: params,
    onComplete: function() {}
});
 
Pezmc
Forum Commoner
Posts: 53
Joined: Mon Nov 06, 2006 2:15 pm

Re: Ajax Posting

Post by Pezmc »

kaszu wrote:If it's an object, then you can do it like this:

Code: Select all

var params = Object.clone(event.memo.rating);
params.id = 7;
new Ajax.Request('savestar.php', {
    parameters: params,
    onComplete: function() {}
});
 
Thank you. As you gathered I am not very good with javascript ajax :P
Just basic things liek alert boxes :-)
Post Reply