I am trying to automate the "scheduled" column. Right now I have set it where it displays "YES" if 1 and "NO" if 0. Currently I have a modal popup where I could enter 1 or 0, then the scheduled turns to "YES" or "NO". How do I get the $_POST to automatically check if the column is 1 or 0 and then change it to the other one if I click on "Update" in the modal?
GOAL:
- Click the "YES" or "NO" >> Modal to confirm >> Update to the other.
The database is already retrieved via connection.php and is being outputted using the variable $table.
I am not sure where to place the checking for the "scheduled" value if it is in the modal or in "connection.php".
I have also tried:
$scheduled = mysqli_query($connection , "SELECT scheduled FROM user WHERE id ='$id'");
if ($scheduled = 1){
$changesched = 0;
} else if ($scheduled = 0) {
$changesched = 1;
}
$result = mysqli_query($connection , "UPDATE user SET scheduled = '$changesched' WHERE id='$id'");
This did not work though, tried to add a variable to the 1 and 0 before the SQL UPDATE.
Any help would be appreciated and would like to see and example even if it is in pseudocode.
Database Name: ajax_test
Table Name: user
index.php
<?php
include 'connection.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<table class="table">
<thead>
<tr>
<th>Email</th>
<th>Scheduled</th>
</tr>
</thead>
<tbody>
<?php
$table = mysqli_query($connection ,'SELECT * FROM user');
while($row = mysqli_fetch_array($table)){ ?>
<tr id="<?php echo $row['id']; ?>">
<td data-target="email"><?php echo $row['email']; ?></td>
<td data-target="scheduled">
<?php
if ($row['scheduled'] == 1) {
?>
<a href="#" data-role="update" data-id="<?php echo $row['id'] ;?>">YES</a>
<?php
} else if ($row['scheduled'] == 0) {
?>
<a href="#" data-role="update" data-id="<?php echo $row['id'] ;?>">NO</a>
<?php
}
?>
</td>
</tr>
<?php }
?>
</tbody>
</table>
</div>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>UPDATE SCHEDULED?</label>
<input type="text" id="scheduled" class="form-control">
</div>
<input type="hidden" id="userId" class="form-control">
</div>
<div class="modal-footer">
<a href="#" id="save" class="btn btn-primary pull-right">Update</a>
<button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</body>
<script>
$(document).ready(function(){
// append values in input fields
$(document).on('click','a[data-role=update]',function(){
var id = $(this).data('id');
var scheduled = $('#'+id).children('td[data-target=scheduled]').text();
$('#scheduled').val(scheduled);
$('#userId').val(id);
$('#myModal').modal('toggle');
});
// now create event to get data from fields and update in database
$('#save').click(function(){
var id = $('#userId').val();
var scheduled = $('#scheduled').val();
var email = $('#email').val();
$.ajax({
url : 'connection.php',
method : 'post',
data : {scheduled: scheduled , id: id},
success : function(response){
// now update user record in table
$('#'+id).children('td[data-target=scheduled]').text(scheduled);
$('#myModal').modal('toggle');
}
});
});
});
</script>
</html>
connection.php
<?php
$connection = mysqli_connect('localhost' , 'root' ,'' ,'ajax_test');
if(isset($_POST['id'])){
$id = $_POST['id'];
$scheduled = $_POST['scheduled'];
$result = mysqli_query($connection , "UPDATE user SET scheduled = '$scheduled' WHERE id='$id'");
}
?>