Janaky Murthy
Janaky Murthy

Reputation: 60

PHP file not able to read multiple parameters from Jquery ajax

I have read multiple solutions to this problem, yet none of them seem to work for me. I have two files manageadmin.php and rejectaction.php . The manageadmin.php files provides a button to the user to reject a admin. when a person clicks on reject button admin status must be set to zero.

Here is my initial code

manageadmin.php

<script type="text/javascript">
$(document).ready(function()
    {
       /* reject */
       $(".reject_btn").click(function(){

         var element = $(this);
         var reject_id = element.attr('reject_id');
         $.ajax({
            type: "POST",
            url: "rejectaction.php",
            data: {reject_id:reject_id},
            success: function(){
                alert("Reject Successful");
                location.reload();
            }
         });

       });
    });
</script>

rejectaction.php

    $reject_id=$_POST['reject_id'];
    $reject_query="UPDATE tbl_admin set admin_status=0 where admin_id='$reject_id'";
    mysql_query($reject_query);

This works fine but then I realized that I needed this reject code for multiple pages. So I thought of passing the table name , column to be updated and column to be checked in where condition as parameters. This is the modified code:

<script type="text/javascript">
$(document).ready(function()
    {
       /* reject */
       $(".reject_btn").click(function(){

         var element = $(this);
         var reject_id = element.attr('reject_id');
         var tbl_name = "tbl_admin";
         var column_reject = "admin_status";
         var column_cond = "admin_id";
         $.ajax({
            type: "POST",
            url: "rejectaction.php",
            data: {reject_id:reject_id, tbl_name:tbl_name, 
                column_cond:column_cond,
                column_reject:column_reject},
            success: function(){
                alert("Reject");
                location.reload();
            }
         });

       });
    });
</script>

rejectaction.php

    $reject_id=$_POST['reject_id'];

    $tbl_name = $_POST['tbl_name'];
    $column_cond = $_POST['column_cond'];
    $column_reject = $_POST['column_reject'];

    $reject_query="UPDATE '$tbl_name' set '$column_reject' = 0 where '$column_cond'='$reject_id'";


// $reject_query="UPDATE tbl_admin set admin_status=0 where admin_id='$reject_id'";
    mysql_query($reject_query);

This code is not working (that is the status is not getting updated) . But the alert("Reject Successful") is getting executed. I am a beginner. Could anyone point out the mistake I have made? Thanks

Upvotes: 0

Views: 66

Answers (1)

cFreed
cFreed

Reputation: 4474

Apart from the (good) advices you received in comments [also look under this answer], your main issue here is probaly the fact that you wrapped $table_name, $column_reject, and $column_cond between quotes.

You should write:

$reject_query =
    "UPDATE $tbl_name set $column_reject = 0 where $column_cond='$reject_id'";

Upvotes: 1

Related Questions