0

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

2
Contributors
3
Replies
27
Views
2 Years
Discussion Span
Last Post by bnmng
0

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.