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
mysql_*functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really pretty easy.print_r($_POST)orvar_dump($_POST)in the PHP page which receives the form submission. Fill out your form, submit and look closely at the data printed to the screen. Familiarize yourself with how form data is posted to scripts, including what gets passed and what doesn't.