1

After following several examples from this website I think I'm about to do it but still can't.

I have this code:

HTML

...
<a href="javascript:void(0);" class="button red big" style="font-size:24px;" onclick="update_it(<?=$_REQUEST["yeah"]["id"];?>);">Pay</a>
...

AJAX

function update_it(n_id){
    $.ajax({
        type: 'POST',
        url: 'update_yes.php',
        data: {idd: n_id},
        success: function(output) 
        {
            alert('Updated, server says '+n_id);
        }, error: function()
        {
            alert('Wrong!');
        }
    });
}

PHP

<?php
    $link = mysqli_connect("localhost", "root", "****", "****"); 
    $sql = "DELETE FROM stuff WHERE id = " .$_POST["idd"];
    mysqli_query($link,$sql) or die(mysql_error()); 
?>

And everything works but the PHP (I think). I say this because I can see how the HTML works properly and how the AJAX function return the success message, but still nothing happens in the database.

I tried different structures in the data field of the AJAX function like data: 'idd': n_id, or data: 'idd=' n_id, but nothing seems to work.

What am I doing wrong? Any tip or advice? Thank you in advance.

Zariweya
  • 295
  • 3
  • 13
  • Have you tried var_dump($sql) after you build up your query in PHP code? It will help us to see what you are sending to the database. – Musa Haidari Sep 30 '15 at 09:38
  • Could you please var_dump $link and $sql and mysqli_query result so we can see where is the problem ? – Axel Lavielle Sep 30 '15 at 09:38
  • Apart from being prone to SQL injection, the code example looks fine, should work. Probably the PHP never executed. Check in the PHP file that execution reaches that point. – marekful Sep 30 '15 at 09:39
  • What you are trying to achieve from PHP page because you have not return or echo anything in PHP so how could it show something ? – Sunil Pachlangia Sep 30 '15 at 09:40
  • You can try to get mysql error by using mysql_error() php function. – Ranjana Sep 30 '15 at 09:47
  • @SunilPachlangia when using `=$variable?>` there is no need to echo. :) – Noman Sep 30 '15 at 09:48
  • @Noman I know very well.But can you show me where he have do this at update_yes.php page ? – Sunil Pachlangia Sep 30 '15 at 09:50
  • @Zariweya add `alert(n_id)` just before your `$.ajax()` to make sure what you are getting. or you can try check in network console. – Noman Sep 30 '15 at 09:50
  • @SunilPachlangia i dont know where he use this but this question required some debugin of code. i also see typo on anchor tag where `onclick` stated – Noman Sep 30 '15 at 09:51
  • Use single quotes at onclick="update_it('=$_REQUEST["yeah"]["id"];?>');" – Sunil Pachlangia Sep 30 '15 at 09:51
  • @Noman Then why are you saying me this – Sunil Pachlangia Sep 30 '15 at 09:52
  • Uoh, so many comments, thank you, guys. @Musa I have inserted two var_dump but it does not seem to send any answer. Any specific think I should do to get the output? Never used var_dump before. I can't see any echo neither but I suppose it is because, at the moment, the button just do the onClick function, it does not "go" to any other page. – Zariweya Sep 30 '15 at 09:53
  • @SunilPachlangia I can't understand your question. I'm trying to delete that field from my table. – Zariweya Sep 30 '15 at 09:59
  • @Noman alert(n_id) returns the right id. I think the problem is PHP, maybe it is not accessing the file properly. HTML works nice and the id reaches fhe AJAX function. – Zariweya Sep 30 '15 at 10:01

3 Answers3

1

I am using apache MySQL and php on Winodws. And I has the same issue. I have in my update-script which was called by AJAX

  1. an UPDATE SQL statement
  2. query it from DB MySQL
  3. try to update the record without changes, (I mean simple updating a row with the same data)

then... I got nothing to be updated because it is bug in AJAX query which call UPDATE statement in your server-side script. But it will work if you before any saving (updating) data make some changes to it.

I am using REPLACE statement instead of UPDATE.

I looked for this solution a lot of time, really too much. And I can't understand why it happens, because when I debug entire script with own params right on the server and all works fine, but when Ajax then only help REPLACE.

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Paul Burilichev
  • 406
  • 3
  • 10
0

you should add exit; statement at the end of php code. echo some success statement. Then You should alert the output variable in javascript. So that you can make sure that php code is called.

ahmad rabbani
  • 323
  • 2
  • 9
  • Ok, after adding the exit function and some "echo" and some javascript "alert" nothing happened, so I suppose I'm not reaching the file. What could it be? If I move the file or just change its name, I get the AJAX function error message. I don't know what to do. – Zariweya Sep 30 '15 at 10:07
0

Ok, finally I found a solution.

The code is pretty fine, it is an apache ownerships and rights problem.

This helped a lot.

Thank you guys for all you comments.

Zariweya
  • 295
  • 3
  • 13