my database update with jQuery Ajax it isnt´t working
I have a link "Read more", when I click in it, I want to update my news table using php but also jQuery Ajax:
- echo '<p>'.$readNewsResult['content'].'<a class="test" href="#fancybox'.$readNewsResult['id_news'].'">Read More</a></p>';
When I click in this link Im getting: "update&update=311", 311 is id of my clicked news, so it is working fine.
But then my php its not working, and it seems that I never enter in my switch condition.
Do you see where might be my error?
This is my jQuery:
- $(function(){
- var read = $('.news');
- read.on('click','.test',function(){
- var updateid = $(this).attr("id");
- var updatedata = "action=update&update="+updateid;
- alert(updatedata);
- $.ajax({
- type: "POST",
- url: "index.php",
- data: updatedata,
- beforesend: '',
- error: '',
- success: function(updateR)
- {
- alert(updateR);
- }
- });
- });
- });
And then, my php
- $action = $_POST['action'];
- switch($action)
- {
- case 'update':
- $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;
- }