-1

So i'm trying to use a table that generates multiple submit buttons per row. I'm doing this with a php while loop. Each button has a value of the corresponding "id" from the database. The table populates with everything from the database that has a status of "Ordered". On each row I can click a button that will change it to "Received" or "Cancelled". This is why I assigned a value of "id" to each button, so it only affects the status of the row that's being clicked. So far, all this is working fine but I would like to be able to do this using ajax instead of refreshing the page each time.

Currently I have this for my ajax:

     $('#cancel').click(function(e) {
         e.preventdefault();
//set value of cancel button to a variable
          var value = $("#cancel").val();
                $.ajax({
                  method: "POST",
                  url: "updatetable.php",
                  data: {cancel: value},

                });
        });

and this for my PHP:

//updatetable.php
     if($_POST['cancel']) {

     echo $_POST['cancel'];
     }

the reason for the "if" statement is because I also need to click received but if yall help me figure this part out, i can go the rest of the way on my own.

Right now, I can't even get this to connect with a simple echo of the POST variable.

Just for reference, here is my html markup for the buttons:

<button type='submit' class='btn btn-danger' name = 'cancel' id = 'cancel' value='".$row['order_id']."'>Cancel</button>
 <button type='submit' class='btn btn-success' name = 'received' id= 'received' value='".$row['order_id']."'>Received</button>

(the buttons are output by a PHP echo statement-hence the concats in the value setting)

I've tried to follow several tutorials but I can't figure out why this doesn't connect the right way. Perhaps I need to change it to an input with type "button" instead of button type submit? But then the actual value of the "value" would appear as the text instead of the word "cancel". Any help is appreciated.

maximus1127
  • 936
  • 11
  • 30
  • 1
    Id's have to be unique so you cannot have multiple `#cancel`, etc. buttons. – jeroen Jan 17 '18 at 15:19
  • but it works fine without ajax. because each row has it's database id assigned to it and that value assigned to the value of the buttons, I can run a query that basically says "update orders set status = "cancelled" where id = $row['id']". i'm more concerned with the ajax side of this. I have it working perfectly as regular submit style – maximus1127 Jan 17 '18 at 15:21
  • Read my comment. What do you think the value of `$("#cancel").val()` will be. Probably not what you think... – jeroen Jan 17 '18 at 15:24
  • if i set an alert, it alerts the correct value of each row when clicked. my question is with ajax. Why will the "updatetable.php" file not echo the $_POST['cancel']? What am i doing wrong so that it's not connecting the ajax call with the corresponding php file? – maximus1127 Jan 17 '18 at 15:26
  • What do you see in the console? – nicolascolman Jan 17 '18 at 15:41
  • i haven't checked the console and i'm not entirely sure how to. I'm not incredibly skilled with javascript/jquery. when i do "include("updatetable.php")" at the top of my dashboard page, it will echo the correct value of the cancel id. so i know that the value is being set correctly. but if i remove the "include" statement, the ajax by itself is not working. nothing gets echoed. – maximus1127 Jan 17 '18 at 15:44
  • `F12` will pull up the console (most likely). Since you are not getting a return, it may not show you much though – NappingRabbit Jan 17 '18 at 16:24

1 Answers1

0

you are going to want to put a unique identifier on your IDs. Here is what I would do (instead):

function cancelClick(btn){    
    e.preventdefault();  // I dont think this is needed
    var value = $(btn).val();
    $.ajax({
        method: "POST",
        url: "updatetable.php",
        data: {cancel: value},
    });
});

actually I would do this but that isnt what you used:

function cancelClick(btn){    
    var value = $(btn).val();
    $.post("updatetable.php",{cancel: value});
});

then your UI like:

<button type='button' 
        class='btn btn-danger' 
        value='<?=$row['order_id'] ?>' 
        onClick="cancelClick(this)">Cancel
</button>
<button type='button' 
        class='btn btn-success' 
        value='<?=$row['order_id'] ?>'
        onClick="otherFnName(this)>Received
</button>

edit: to perform a task on return you do something like this:

function cancelClick(btn){    
    var value = $(btn).val();
    $.post("updatetable.php",{
      cancel: value
      }, function (d){
        // here is where you will do stuff on return.
        // I suggest first console.log(d); and see what is returning
        // return (echo) a value to at least identify the row
        // you are wanting to delete, if that is what you are attempting
        // be sure the element you are attempting to manipulate has
        // a unique identifier (id), and then do whatever.
        // you can call a function from here also, to make it neater
        // this area will `happen` when your ajax is complete.
      });
});
NappingRabbit
  • 1,888
  • 1
  • 13
  • 18
  • you were right about the preventDefault...i removed it. now my console is throwing an error saying "$.ajax is not a function". I'm using the correct jquery files in my library. – maximus1127 Jan 17 '18 at 16:29
  • you have jQuery being called? – NappingRabbit Jan 17 '18 at 16:31
  • yes ok. so i swapped out my jquery library with the hosted google one. Now i'm getting the correct id logged to the console. So for now, everything is connected and working between the button and the javascript section. but my php file is still not executing the code for that $_POST variable. – maximus1127 Jan 17 '18 at 16:33
  • ok....update...i got it working with the file. so that's all connected now. If i can trouble you for one more question...is there a way to get the display table on the page to automatically reflect the change to the status? right now i still have to manually refresh the page to get that line to go away once it's "cancelled" – maximus1127 Jan 17 '18 at 16:36
  • yeh you can, I will edit my answer and add that. well the `bones` of it. one sec – NappingRabbit Jan 17 '18 at 16:37
  • @maximus1127 ok I added a `$.post()` example that you can probably follow pretty easily. I would need more specifics about the `html` you will manipulate to assist further I think. – NappingRabbit Jan 17 '18 at 16:44
  • do you have a cloud 9 account? i can just share the whole project with you – maximus1127 Jan 17 '18 at 17:00
  • @maximus1127 I dont and actually at work I can barely access the internet at all lol. – NappingRabbit Jan 17 '18 at 17:05
  • ok, no worries. just to help point my research in a profitable direction...what should i look for? in the function (d) section of your update...should i write to refresh the page there? if i have to refresh it...it's not the end of the world...but things like facebook don't need to refresh to update a comment section or something similar. I'm kinda fishing for that seemless UI concept. This is my first time using ajax so i'm a total noob – maximus1127 Jan 17 '18 at 17:07
  • no no that is the point of ajax (`$.post()` is just a shorter version of ajax type - post) on return you can do whatever, and very have to refresh the page. like if you have a `
    ` you can do in the return place something like, `$("#testDiv").html(d)` and that will put the value into the div.
    – NappingRabbit Jan 17 '18 at 17:10
  • ok so let me condense a few details to give you a better idea. When the index page loads, it runs a query and pulls all the rows from the database with a status of "ordered". For every row it pulls, a "while" loop creates an html row with the buttons we've been talking about. If i'm following you correctly, then something I could do is run that initial query and populate the first table, then perhaps delete that table and echo out a new table in the ajax function? – maximus1127 Jan 17 '18 at 17:13
  • you can. or you can delete a single row if you give the `` an id. that is likely better than recreating the whole table; although perhaps more confusing to implement. I would add an `id` to the `tr` as they are produced by your while loop. here is alink to a question that may assist you https://stackoverflow.com/questions/170997/what-is-the-best-way-to-remove-a-table-row-with-jquery – NappingRabbit Jan 17 '18 at 17:16
  • oh wait!! one more thing...i already implemented the id and that works fine...This is starting a whole separate topic but maybe you can give me a quick answer that i can look into. my index page has two tables. the top one for the status of "ordered" and the bottom one for the status of "received". How can i update the bottom table once the status is changed? should i do some sort of setInterval with a query inside it or something? (the tables use the same concept...query for a status and out put rows with a while loop) – maximus1127 Jan 17 '18 at 17:26
  • you can update both on return. @maximus1127 perhaps make a function to do that which you call on return – NappingRabbit Jan 17 '18 at 17:40