0

I just made up some code with the help of some nice Stackers. However, it does not seem to send the data at all. I should receive 'theid' at the page, where a query should run, however the query doesn't even get executed because I think there is no data submitted to the page, or am I getting the data incorrectly?

$(document).ready(function() {    
    $(".myButton").on("click", function() {
        var id = $(this).data("id");
        $.ajax({
            type: "POST",
            url: "index.php?url=seenot",
            async: true,
            data: {"theid": id },
                success: console.log("Een Notificatie is als gelezen gemarkeerd | ID: " + id)
            });
        });
    }); 

The file seenot

$id = $_POST['theid'];
$setseen = mysql_query("UPDATE cms_notifications SET userstatus = 'seen' WHERE id = '".$id."'")or die(mysql_error());

I'm aware that MySQL is deprecated. I'm sorry. Thanks in advance.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Pascal Boschma
  • 187
  • 1
  • 14
  • 2
    Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there an errors reported? Are you running this on a web-server? – Jay Blanchard Apr 14 '16 at 21:23
  • @JayBlanchard I'm not that super experienced in jQuery. Is it possible to show the eventual result of the page, which runs the Query in the Console Log? – Pascal Boschma Apr 14 '16 at 21:26
  • Yes, the returned results - including errors in PHP, will show in the console under the network tab. – Jay Blanchard Apr 14 '16 at 21:27
  • Given your previous related question, I think you should be using `prop()`, not `data()` to get the id: `var id = $(this).prop("id");` - or even just `var id = this.id;` – Rory McCrossan Apr 14 '16 at 21:27
  • 1
    Also you should ***never*** use `async: false`. It locks the UI while the request is processing which appears to the user as though the browser has crashed or frozen. – Rory McCrossan Apr 14 '16 at 21:29
  • @JayBlanchard Nothing returns. I Think that it just doesn't send the ID. Rory, Does this work for TRUE too? And related to prop(), I Need to get that ID from a button click. – Pascal Boschma Apr 14 '16 at 21:29
  • You should be able to check if it sends the ID at the bottom of the "Headers" tab (assuming you're using Chrome). – Chris Apr 14 '16 at 21:31
  • If you look in the request you should be able to see what is sent. – Jay Blanchard Apr 14 '16 at 21:31
  • Can you share the markup of `.myButton`? – Jay Blanchard Apr 14 '16 at 21:32
  • When I get it correctly, you mean that this should return? `success: console.log("Een Notificatie is als gelezen gemarkeerd | ID: " + id)` Because, that does return, however the Query isn't runned. – Pascal Boschma Apr 14 '16 at 21:32
  • can you try this change: var id = $(this).data("id"); to var id = $(this).attr("id"); – Aljay Apr 14 '16 at 21:32
  • What is the actual attribute on the HTML entity called @PascalBoschma? Is it "data-id" or "id"? – Chris Apr 14 '16 at 21:34
  • Also, I should point out that just because you are using the deprecated `mysql` module, doesn't mean that you have to leave yourself vulnerable to SQL injection. You can still use [`mysql_real_escape_string`](http://php.net/manual/en/function.mysql-real-escape-string.php) to escape your variables. – Chris Apr 14 '16 at 21:35
  • *Like a pack of dogs to a bone.....* ¯\\_(ツ)_/¯ – Jay Blanchard Apr 14 '16 at 21:35
  • Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe @Chris – Jay Blanchard Apr 14 '16 at 21:35
  • @JayBlanchard It's not ideal, but in this situation it's sufficient. – Chris Apr 14 '16 at 21:36
  • data-id, and in the Console it does return the correct ID. To talk about the class .myButton, it's not stylized. It's just for the use of jQuery. Jay, I Need to get the ID which the variable returns, but I tried it. It returns A undefined, since it uses data-id and not id (I Think) – Pascal Boschma Apr 14 '16 at 21:36
  • Show us the markup for the button. – Jay Blanchard Apr 14 '16 at 21:36
  • i guest this is the markup of his button – Aljay Apr 14 '16 at 21:37
  • Why would you guess that @jay – Jay Blanchard Apr 14 '16 at 21:37
  • And when you look at the source of the rendered page, what does your button markup look like? – Jay Blanchard Apr 14 '16 at 21:38
  • Base on his question here http://stackoverflow.com/questions/36633616/jquery-click-not-running – Aljay Apr 14 '16 at 21:38
  • After rendering, the markup looks like this: `` – Pascal Boschma Apr 14 '16 at 21:40
  • OK - just wanted to check to make sure `var id` was getting set. Do you echo anything out from your PHP file? – Jay Blanchard Apr 14 '16 at 21:41
  • For testing purposes I echo out `$id` – Pascal Boschma Apr 14 '16 at 21:41
  • Is the code you're showing for the PHP all that is in the file? If so, there is no connection to the database and there is nothing that would be returned. We need to see the whole PHP file. – Jay Blanchard Apr 14 '16 at 21:42
  • It's the whole file, and the connection does exist. It's a kind of TPL system. When I echo the user id, it will show it. The original URL to the file would be `index.php?url=seenot` – Pascal Boschma Apr 14 '16 at 21:43
  • The PHP you show has no access to the database connection, therefore the query doesn't run. – Jay Blanchard Apr 14 '16 at 21:46
  • I feel like a bit of an idiot for not picking up on this earlier, but shouldn't `success` be a function? – Chris Apr 14 '16 at 21:47
  • And @JayBlanchard, I think he means that's the whole file (excluding the open PHP tag I hope), but it's included in another file that establishes the database connection. – Chris Apr 14 '16 at 21:47
  • @Jay it has a connection, but since it's load by TPL it's all included in one. – Pascal Boschma Apr 14 '16 at 21:48
  • I guest the query is the problem or mysql connection – Aljay Apr 14 '16 at 21:49
  • Could that be the problem? That pages are loaded over one file? `index.php?url=seenot` – Pascal Boschma Apr 14 '16 at 21:49
  • @jay The Connection exists, otherwise I couldn't echo my User ID on the page? I Could take another look at the Query, but it seems that nothing is really wrong with it. – Pascal Boschma Apr 14 '16 at 21:49
  • @PascalBoschma Try changing the value of success to an anonymous function that calls `console.log` rather than the result of `console.log` (which I think is probably undefined). – Chris Apr 14 '16 at 21:49
  • You could echo the ID all day long without a database connection. – Jay Blanchard Apr 14 '16 at 21:50
  • Also you are just logging the local value of `id` in JavaScript. It doesn't even matter if the request succeeds or fails it will still log the value because the `console.log` statement is processed when you make the request. – Chris Apr 14 '16 at 21:51
  • Jay, I'm talking about the User ID, Which I get from the database in another Query, and that's right. The Console log does show a Result, THE ID which got posted http://i.imgur.com/6Yj2egG.png – Pascal Boschma Apr 14 '16 at 21:52
  • i tried the jquery code its working find with me. and i was able to echo the theid in my php code. – Aljay Apr 14 '16 at 21:53
  • Chris, what should I log then? RESULT or RESPONSE? – Pascal Boschma Apr 14 '16 at 21:53
  • try to change the success like this. success : function(response) { console.log(response); } – Aljay Apr 14 '16 at 21:55
  • You are logging the request data. As I said, the `success` parameter takes a **function** as a value. You have provided the result of a call to a function, which is in this case undefined. You are effectively calling your log statement regardless of whether or not your request succeeded. – Chris Apr 14 '16 at 21:56
  • Dear Stackers, thanks for your very good help. I Just fixed it. In the Connector file, I Forgot a Semicolon. This bugged the whole seenot file. – Pascal Boschma Apr 14 '16 at 22:04

1 Answers1

0
 $(document).ready(function() {    
   $(".myButton").on("click", function() {
     var id = $(this).data("id");
       $.post('index.php?url=seenot', {theid: id},
          function(output){
           console.log("Een Notificatie is als gelezen gemarkeerd - ID" + output)
       });
    });
  });

You can use the JQuery shorthand Ajax function $.Post() to send data to PHP scripts. The callback function with the output argument is the data being returned after it is manipulated and returned by the PHP script. Please see Documentation and example JQuery Ajax Function.