0

I'm having an issue with php and ajax. I have built a personal workout routine website (so I can view whilst at the gym). I have built the ability to add, edit and delete routines however I am trying to build it so that the edit and delete work through ajax so that it updates on the page rather than having the need to refresh the page.

My main issue is trying to get the routine id to pass through ajax as I can't figure out how to do it dynamically! I have manually added in the ajax that id = 15 and the top routine data changes (although need to refresh page at the moment for data to change) but as I said, can't figure out how to dynamically pull the routine id that is been updated.

View the website here http://www.swishwebs.co.uk/workout/view_workout.php

Let me know what I need to do get that routine id.

Thanks

CarlTaylor1989
  • 209
  • 1
  • 4
  • 12

3 Answers3

0

in your anchor tag have some custom attribute lik data-id

<a href="##" data-id="{dynamic id through looping results}" class="button>Edit</a>

In jquery, you can retrieve it as

$(this).data("id");

In your code, you can have it as

$(".button").click(function() {
        var id = $(this).data("id"); 
        var dy = $("#day").val();
        var bp = $("#body_part").val();
        var sw = $("#start_weight").val();
        var cw = $("#current_weight").val();
        var ex = $("#exercise").val();
        var st = $("#sets").val();
        var rp = $("#reps").val();

            //other code

});

Based on your comment, I created a sample table with two rows like below:

<table border="0" bordercolor="#000000" cellspacing="0" cellpadding="0">
            <tbody>
                <tr style="border-top: 1px solid #ffffff;">
                    <Td><a href="##" data-id="5" class="button">Edit</a></td>
                    <Td>125</td>  
                </tr>


               <tr style="border-top: 1px solid #ffffff;">
                    <Td><a href="##" data-id="6" class="button">Edit</a></td>
                    <Td>135</td>  
                </tr>
          </tbody>
</table>​

and jquery code for the edit link click is as below:

   $(".button").click(function(){
      var id = $(this).data("id");
      var startweight = $(this).parent().next().text();
      alert(id + ", " + startweight);
   });

When the anchor tag is clicked, '$(this)' refers to the element that is clicked, in your case, it in anchor tag. So when I say $(this).data("id"), it only searches in anchor tag, but when you want to get other information from adjacent columns, you will need to learn about more DOM manipulation. I just gave an example on how to get information of adjacent columns. try for your code, and post here if you have any questions

Once your data is updated via ajax, you can do the same. Post some sample html of where you want to update the data, then porbably we can fix it.

DG3
  • 5,070
  • 17
  • 49
  • 61
0
$(".button").click(function() {
    var id = $(this).data("id");
    var dy = $(this).parent().siblings().find(".day").val();
    var bp = $(this).parent().siblings().find(".body_part").val();
    var sw = $(this).parent().siblings().find(".start_weight").val();
    var cw = $(this).parent().siblings().find(".current_weight").val();
    var ex = $(this).parent().siblings().find(".exercise").val();
    var st = $(this).parent().siblings().find(".sets").val();
    var rp = $(this).parent().siblings().find(".reps").val();

    if(dy == "" || bp == "" || sw == "" || cw == "" || ex == "" || st == "" || rp == ""){
        alert("error");
    } else {
        var id_num = "id"+id;
        var day = "day"+dy;
        var body_part = "body_part"+bp;
        var start_weight = "start_weight"+sw;
        var current_weight = "current_weight"+cw;
        var exercise = "exercise"+ex;
        var sets = "sets"+st;
        var reps = "reps"+rp;

        $.ajax({
            type: "POST",
            url: "new_edit_workout.php",
            data: "id=" + id + "&day=" + dy + "&body_part=" + bp + "&start_weight=" + sw + "&current_weight=" + cw + "&exercise=" + ex + "&sets=" + st + "&reps=" + rp,
            dataType: "text/html",
            success: function(response){
                if(response !== undefined){

                    $(this).closest("tr").remove();

                    $(this).closest("tr").before("<tr class=\"row-refresh"+id+"><td class=\"bg-cell-td\">"+dy+"</td><td class=\"bg-cell-td\">"+bp+"</td><td class=\"bg-cell-td\">"+sw+"kg</td><td class=\"bg-cell-td\">"+cw+"kg</td><td class=\"bg-cell-td\">"+ex+"</td><td class=\"bg-cell-td\">"+st+"</td><td class=\"bg-cell-td\">"+rp+"</td><td class=\"bg-cell-td\"><a data-id="+id+" class=\"click-edit\" style=\"cursor: pointer;\">Edit</a> | <a href=\"delete_workout.php?id="+id+" rel=\"ibox&width=350\">Delete</a></td></tr>");  
                    $(".updated").slideDown(800).delay(2000).slideUp(800);

                } else {
                    alert("It failed!");
                    $(".edit-box").css("background-color", "#000000");
                }
            }
    });
}

}); });

CarlTaylor1989
  • 209
  • 1
  • 4
  • 12
0
        print "<h1 style=\"margin: 0; text-shadow: 0 0 5px #B9C9FE; font-size: 16px;\">MONDAY</h1>
        <table border=\"0\" bordercolor=\"#000000\" cellspacing=\"0\" cellpadding=\"0\" id=\"refresh\">
            <tr style=\"border-top: 1px solid #ffffff;\">
                <th class=\"bg-cell\">Body Part</td>
                <th class=\"bg-cell\">Start Weight</td>
                <th class=\"bg-cell\">Current Weight</td>
                <th class=\"bg-cell\">Exercise</td>
                <th class=\"bg-cell\">Sets</td>
                <th class=\"bg-cell\">Reps</td>
                <th class=\"bg-cell\">Customise</td>
            </tr>";
    } 
} else {
    print "";   
}

if($r = mysql_query($query_monday)){
    while($row = mysql_fetch_array($r)){
        print "
            <tr class=\"row-refresh{$row['id']}\" id=\"{$row['id']}\">
                <td class=\"bg-cell-td\">{$row['body_part']}</td>
                <td class=\"bg-cell-td\">{$row['start_weight']}kg</td>
                <td class=\"bg-cell-td\">{$row['current_weight']}kg</td>
                <td class=\"bg-cell-td\">{$row['exercise']}</td>
                <td class=\"bg-cell-td\">{$row['sets']}</td>
                <td class=\"bg-cell-td\">{$row['reps']}</td>
                <td class=\"bg-cell-td\"><a data-id=\"{$row['id']}\" class=\"click-edit\" style=\"cursor: pointer;\">Edit</a> | <a href=\"delete_workout.php?id={$row['id']}\" rel=\"ibox&width=350\">Delete</a></td>
            </tr> 
CarlTaylor1989
  • 209
  • 1
  • 4
  • 12