1

I have this "Read More" link:

echo '<p>'.$readNewsResult['content'].'<a class="test" href="#fancybox'.$readNewsResult['id_news'].'">Read More</a></p>';

When I click in this link, my goal is to update views column of my news table.

So I have a jQuery where Im passing id of my news, and it is working fine, when I click on "Read more" link I get an alert message saying: "action=update&update=311", where 311 is id of my clicked news.

My jQuery until now:

$(function(){
   var read = $('.news');
   read.on('click','.test',function(){
       var updateid = $(this).attr("id");
       var updatedata = "action=update&update="+updateid;
       alert(updatedata);
       $.ajax({
         data:  updatedata,
         beforesend: '',
         error: '',
         success: function(updateR)
         {
             alert(updateR);
         }   
       });
   });
});

But now with php, Im trying to get update action and id, and do update on my news table, but its not working, because it seems that I never enter in my switch condition.

I tried to give some "echos" inside my case, and when I click on my "Read more" link my echo never appears.

Do you see where might be the problem??

$action = $_POST['action'];
switch($action)
{
    case 'update':
      $id = $_POST['id'];
      $updateViews = $pdo->prepare("UPDATE news SET views=:views WHERE id=:id"); 
      $updateViews->bindValue(':views', '1');
      $updateViews->bindValue(':id', $id); 
      $updateViews->execute();
    break;
}
2
  • You miss a semicolon ' after the echo code: echo **'**<p>'.$readNewsResult['content'].'<a class="test" href="#fancybox'.$readNewsResult['id_news'].'">Read More</a></p>'; Commented May 24, 2014 at 3:52
  • Thanks, I have that correct on my example, but here I put it wrong thanks for notice. I update now correctly! Commented May 24, 2014 at 15:00

3 Answers 3

1

At the very least you are trying to work with $_POST['id'] in your PHP but you are actually creating an URL parameter called update which contains the ID in your JavaScript.

The actual issue is that you are lacking an URL argument to your $.ajax call.

You are also naming your var udpdatedata on this line:

   var udpdatedata = "action=update&update="+updateid;

but are referencing updatedata in the $.ajax call:

     data:  updatedata,

As such your query parameters are never added to the non-existent URL.

An extra one:

     sucess: function(updateR)

Is actually spelled success, note the double c.

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

1 Comment

Thanks ikanobori. Your corrections were really helpful! I didnt notice that errors. But my update still dont work! And when I click on "Read more" link, I get an alert with my php.file code!
1

Where is your ajax controller url, I mean url and also type of call type,

$.ajax({
     type: 'POST',
     url: '/url/myphpfunction',
     data:  updatedata,
     beforesend: '',
     error: '',
     sucess: function(updateR)
     {
         alert(updateR);
     }   
   });

Here is the tutorial http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php

2 Comments

Thank you dhana. I read your answer, whole tutorial and made some changes, but Im not seeing why, it isnt work yet!
@OzzC sorry I am not php developer. But some thing goes wrong in your backend logic. For more bug tracing use chrome developer tool bar.
1

You had a few things wrong with your ajax function. The first was that you should be passing through an object of values instead of a string. Then you need to specify a method of getting to your script. Then you need to set the URL to your script. See the comments below:

$(function(){
    var read = $('.news');
    read.on('click','.test',function(){
        var updateid = $(this).attr("id");
        // pass data as a JS object
        var udpdatedata = {action:'update', update:updateid};
        alert(udpdatedata);
        $.ajax({
            // set the method to post
            type: "POST",
            // the URL to your PHP script
            url: "pathtoscript/script.php"
            data:  updatedata,
            beforesend: '',
            error: '',
            success: function(updateR)
            {
                alert(updateR);
            }   
        });
    });
});

Your PHP also had an error, you're passing through 'update', not 'id':

$action = $_POST['action'];
switch($action)
{
    case 'update':
        // you're passing through 'update', not 'id'
        $id = $_POST['update'];
        $updateViews = $pdo->prepare("UPDATE news SET views=:views WHERE id=:id"); 
        $updateViews->bindValue(':views', '1');
        $updateViews->bindValue(':id', $id); 
        $updateViews->execute();
    break;
}

3 Comments

Thanks for your answer Samsquanch, now I understood better how to do this. But its not working yet, when I click in my "Read more" Link Im getting an alert saying: [object Object]
So now, it seems that Im not also getting id of clicked news.
You're getting [object Object] because what is being returned is some sort of object. Without the rest of your PHP I can't say what it is you're trying to return vs what you're getting. Try using console.log(updateR) instead of alert, you might have better luck.

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.