I am having no luck with this code but for some reason i can get this element to work but not the other one. here is my code.

<td><a onclick="javascript:approve_account_function(<?php echo $row['id'];?>, <?php echo $rownumber;?>)" class="btn btn-success" name="yes_account" id="yes_account">Approve</a></td>
                         <td><a onclick="javascript:decline_account_function(<?php echo $row['id'];?>, <?php echo $rownumber;?>)" class="btn btn-danger" name="no_account" id="no_account">Decline</a></td>
                         <input type="hidden" name="sub_id<?php echo $rownumber;?>" id="sub_id<?php echo $rownumber;?>" value="<?php echo $row['id']; ?>"/>
                      </tr>
                        <?php 
                      } ?>
              </tbody>
      </table>
      <input type="hidden" name="row_total" id="row_total" value="<?php echo $rownumber; ?>"/>

one thing to rememebr is that my hidden elements are being generated by a while loop so they will be different per user. This way i can pass these elements to my ajax and then through ajax pass them to the server through php, my row_total element works but my other one does not.

Here is the ajax call, there are two but they are nearly identical in function

<script type="text/javascript">

function approve_account_function(subscriber_id, rownum){
  var xhr;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
    else {
        throw new Error("Ajax is not supported by this browser");
    }

    var note_text = $(this).attr("sub_id"+subscriber_id);
    var cnotes = $("#row_total").val(); //will be the value of loggedIn

    // var note_text=$('#row_id'+rownum).val();
    // var cnotes=$('#row_total').val();
     var lock_data = "sub_id="+note_text+"&row_total="+cnotes;

  //var lock_data = $("form#approval_form").serialize(); // gets all data from your form
  $.ajax({
    url : "http://localhost/xampp/approve_subscriber/approve_account.php",
    type: "POST",
    data: lock_data,
      success: function(data, textStatus, jqXHR)
      {
      //data - response from server
      alert(data);

      // reloads page after user clicks ok on the response
      location.reload(true);
    },
  });
}

function decline_account_function(subscriber_id, rownum){
  var xhr;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
    else {
        throw new Error("Ajax is not supported by this browser");
    }

    var note_text=$('#sub_id'+rownum).val();
    var cnotes = $("#row_total").val(); //will be the value of loggedIn

    // var note_text=$('#row_id'+rownum).val();
    // var cnotes=$('#row_total').val();
     var lock_data = "sub_id="+note_text+"&row_total="+cnotes;

  //var lock_data = $("#newtable").serialize(); // gets all data from your form
  $.ajax({
    url : "http://localhost/xampp/approve_subscriber/decline_account.php",
    type: "POST",
    data: lock_data,
      success: function(data, textStatus, jqXHR)
      {
      //data - response from server
      alert(data);

      // reloads page after user clicks ok on the response
      location.reload(true);
    },
  });
}

</script>

here is one of the php files, they are both identical except the sql statements so if one gets fixed it should be applyable to the other.

<?php
include_once '../includes/db_connect.php';

$total = $_POST['row_total'];

for ($i=0; $i < $total; $i++) { 

    $id = $_POST['sub_id'.$i];

    $add_sql = "INSERT INTO approved_users SELECT * FROM temp_users WHERE id='".$id."'";
    $delete_sql = "DELETE FROM temp_users WHERE id='".$id."'";

    if (mysqli_query($con, $add_sql)) {
        mysqli_query($con, $delete_sql);
        echo "Successfully Approved User";
        // table modified and updated
    }

}

//echo "Successfully Approved User";

?>

im not sure how to fix this issue mainly because i dont see it, i have done this before or so i thought in another snippet of code and i have tried applying the same method but to no success

any and all help is as always greatly appreciated

Dani AI

Generated

Short summary: the symptom turned out to be an indexing mismatch in the loop (an off‑by‑one), which made the AJAX call send the wrong hidden input name — fixed this by moving the row counter so the generated sub_idN matched what the server expected. ’s note about using a relative path for AJAX is also a good general tip when you might hit cross‑domain restrictions.

How this usually happens and how to avoid it:

  • When you generate inputs in a loop, be explicit about whether you increment the counter before or after output; a one‑step difference will make the client send sub_id1 while PHP reads sub_id0.
  • Avoid relying on fragile name patterns like sub_id0, sub_id1 unless you control both sides strictly. Prefer sending a single id per AJAX request, or use name="sub_id[]" so PHP receives an array in $_POST['sub_id'].
  • Don’t assume this inside a standalone global function refers to the clicked element — either pass the element explicitly or bind a handler with jQuery/event delegation. Use browser devtools (Elements + Network) and console.log() to verify the DOM id and the actual POST payload before debugging server code.

Example approach (different pattern than the original):

<tr data-row="0" data-sid="123">
<td><button class="approve-btn">Approve</button></td>
</tr>

$(document).on('click', '.approve-btn', function(e){
  e.preventDefault();
  var $tr = $(this).closest('tr');
  var sid = $tr.data('sid');               // single id to send
  var total = $('#row_total').val();
  $.post('approve_account.php', { sub_id: sid, row_total: total })
    .done(function(response){ alert(response); location.reload(true); })
    .fail(function(xhr){ console.error('AJAX error', xhr.status); });
});

Server-side notes: accept a single sub_id (or sub_id[]), cast to int, and use prepared statements and a transaction for the move/delete so the database stays consistent. If you must send many ids at once, post sub_id[] and iterate $_POST['sub_id'] server‑side rather than expecting numbered keys.

Recommended Answers

All 3 Replies

It might be a cross-domain security measure. Instead of using a fully qualified domain for your URL, try using a relative path. If your form is on a different server and you have to use the full domain, then look into cross domain ajax posting.

lol i figured it out, the issue was on my end, i was not passing my form values correctly in my loop, my rownumber reference was always 1 ahead of the function so essentially i was passing sub_id1 to a php file listening for sub_id0. i moved my rownumber counter and now it works like a charm, im sorry for the confusion, thanks for the help bnmng

Cool. Glad you figured it out. And thanks for responding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.