0

I don't understand. I have searched all internet forums but found nothing helpful. I am trying to update the numberOfLikes field on my postsTable in MySql when the user clicks on the like button. I know this is done through ajax but I am only familiar with prototype ajax and none internet forums state anything about it.

Here's the flow chart

1. On "seeForums.php" user clicks on the "like" link.
2. The like link has an id that triggers the function which updates numberOfLikes on my postsTable.

Thats it. Thats all I need. But I need it in a prototype ajax format, something like this.

   function processLikes()
   {  
      new Ajax.Request(theUrl,
      { 
     contentType:"text/HTML",
     onSuccess:updateLikesMySql,
     onFailure:error
     onException:error,

    });
  }

Helps are appreciated :)

2 Answers 2

2

You can't do this with Javascript alone as it is client side only, you'll need to get a server side language (e.g. PHP) involved as well.

The idea is that you send an AJAX request to your PHP file along with the data that you want to update, and your PHP file will handle inserting values into the database. That PHP file would then print an output (e.g. success or failure) which would be received in your Javascript so you can act accordingly.

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

2 Comments

So can I use simple javascript instead and work my way through the further using php? I mean in the end I can do something like $_REQUEST["numberOLikes"] and update my database. Does that sound good?
That's exactly right, just make sure you use mysql_real_escape_string() on parameters passed on so you're protected from SQL injection attacks.
0

You should know that the nature of HTTP (web) makes it Request/Response like. So your backend code runs on the server, And javascript and all frontend code run on the client.

Client has no access to your database, So you can't do anything to your database with Javascript.

Best thing you can do is ask your serve with javascript to update the database,

Make a server-side script available at some URL,

Then use ajax to call that URL.

In the server-side script, do the code which updates the database.

Comments

Your Answer

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