Hi everyone,

I'm using jquery in this piece of script and also trying to call the functions. But, they aren't working at ALL and the firefug is saying that there is an error in the script. The error is

missing ) in parenthetical
[Break On This Error]

$.post('ajax/like_add.php', (article_id:article_id), function(data){

and the firebug points to the colon.


This is the entire piece of script that has both functions.

function like_add(article_id){
	$.post('ajax/like_add.php', (article_id:article_id), function(data){
		if(data == 'success'){
			like_get(article_id);
		}else{
			alert(data);
		}
	});
}


function like_get(article_id){
	$.post('ajax/like_get.php',(article_id:article_id), function(data){
		$('#article_'+article_id+'_likes').text(data);
	});
}

Thanks, folks.

Dani AI

Generated

Quick diagnosis for : the Firebug "missing ) in parenthetical" error is a plain JavaScript syntax issue — the data argument to an AJAX call must be an object literal (curly braces), not a parenthetical with colons. was on the right track. Below is a safe, modern pattern that also shows basic error handling and returns JSON from the server so you can update the page in one step.

function addLike(articleId) {
  $.ajax({
    url: 'ajax/like_add.php',
    type: 'POST',
    data: { article_id: articleId },
    dataType: 'json'
  }).done(function(response) {
    if (response && response.status === 'success') {
      $('#likes-' + articleId).text(response.likes); // update count from server
    } else {
      alert(response && response.error ? response.error : 'Like failed');
    }
  }).fail(function(xhr, status, err) {
    console.error('AJAX error:', status, err);
  });
}

Minimal server-side behavior (PHP): read and sanitize $_POST['article_id'], update your DB with a prepared statement, and return a JSON object such as { "status":"success", "likes": 42 }. Always send Content-Type: application/json and never echo raw SQL errors to the client.

Practical checklist and extra tips:

  • answered correctly: the PHP file lives on your server in the ajax/ folder — verify the relative path from the page making the request.
  • Verify jQuery is loaded before your script runs and that articleId is defined when you call the function.
  • Use the browser Network tab to inspect the POST, status code, and response body.
  • Add server-side validation (intval, prepared statements), CSRF protection, and logic to prevent duplicate likes (session or DB check).
  • Disable the like button while the request is pending to avoid double-submits.

Start with the corrected data object and a simple JSON response. That alone should clear the Firebug error and make debugging the rest much simpler.

Recommended Answers

All 3 Replies

The second parameter of post(), the data, should use curly brackets, and not parenthesis.

where can i find like_add.php or is this it and it links to itself?

like_add.php is inside the folder ajax in your server. it will handle your ajax request, it is the backend of your ajax call.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.