Access Value of While Loop Outside it - php

The code that contained elements fetched from array
<?php
$query = "SELECT * from room";
$query_run = #mysql_query($query);
?>
<?php
while($row=mysql_fetch_array($query_run)) {
$id = $row['id'];
?>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<td> <center></center></td>
<td><?php echo $row['name']; ?> </td>
<td><button type="button" name="delete" class="btn btn-danger" data-toggle="modal" data-target="#delete" data-backdrop="static" data-keyboard="false"><center><i class="glyphicon glyphicon-trash"></i>Remove</center></button></a></td>
</tr>
The Delete Confirmation Modal that Show upon click on above Remove button
<div class="modal fade" id="delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div>
<button type="button" class="btn btn-danger"><i class="glyphicon glyphicon-trash"></i>Remove</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
The Output is repeated as the last value fetched
dell_rooms.php?id=10
dell_rooms.php?id=10
dell_rooms.php?id=10
I want it to be like this :
dell_rooms.php?id=8
dell_rooms.php?id=9
dell_rooms.php?id=10
not only the last element fetched
Note that the id i used in Modal is outside while loop and if i put the modal inside loop it not work properly

You need to stop using mysql_* Functions because it's omitted on php 7.0 instead use mysqli_* or PDO.
I advise you to read through this. it will help you to migrate from mysql to mysqli Converting to MySQLi.
i found alot of bugs on your code
you didn't close the while loop and there was a double quote missing on the html maybe just a typo
<a href="<?php echo "dell_rooms.php?id=$id";?> >
<input type="checkbox" class="myCheckBox" name="remove[]" value="<?php echo $id; ?>"></a>
give this one a try it worked fine with me
<?php
$query = "SELECT * from room";
$query_run = #mysql_query($query);
while($row=mysql_fetch_array($query_run)) {
$id = $row['id'];
?>
<div>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<td>
<center>
</center>
</td>
<td><?php echo $row['name']; ?> </td>
<td><button type="button" name="delete" class="btn btn-danger" data-toggle="modal" data-target="#delete" data-backdrop="static" data-keyboard="false"><center><i class="glyphicon glyphicon-trash"></i>Remove</center></button></a></td>
</tr>
</div>
<?php } ?>

Related

How can I use checkboxes to delete rows with php and mysql

I created a table with database information and tried to create checkboxes to be able to delete lines more easily, but something is not working correctly.
I have a button with form:
<form action="delete-register.php" method="post">
<button type="button" class="btn btn-primary"><span class="fe fe-file-plus fe-12 mr-2"></span>New</button>
<button type="submit" name="delete" class="btn btn-secondary"><span class="fe fe-trash fe-12 mr-2"></span>Delete</button>
</form>
And I have rows with checkboxes:
<form action="delete-register.php" method="post">
<td>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="<?php echo $row['id']; ?>" name="selected[]" value="<?php echo $row['id']; ?>">
<label class="custom-control-label" for="<?php echo $row['id']; ?>"></label>
</div>
</td>
</form>
And there is delete-register.php:
if (isset($_POST['delete'])) {
if (isset($_POST['selected'])) {
foreach ($_POST['selected'] as $id) {
$query = "DELETE FROM registers WHERE id = $id";
mysqli_query($conn, $query);
}
header('Location: registers.php');
exit;
}
}
The problem is that "selected" is always null and so nothing is deleted from the database.
How can I solve this problem?
Please note that the data submitted will be within the scope of <form>....</form>
Since you have two forms, when you click the submit button in the 1st form, it will not send the data of the 2nd form to the server.
Hence, please change the 2nd form to:
<form action="delete-register.php" method="post">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="<?php echo $row['id']; ?>" name="selected[]" value="<?php echo $row['id']; ?>">
<label class="custom-control-label" for="<?php echo $row['id']; ?>"></label>
</div>
<input type=submit name=delete>
</form>
[Additional Remark]
If you want to stick to using the 1st form to trigger the delete action, then please:
in the 1st form, change the delete from "submit" to "button"
add an onclick event to this delete button so that it will trigger submission of the 2nd form
make sure you have a hidden field known as "delete" in the 2nd form since you specified to have this field in your PHP script
you may have noticed that I have added id=form2 in the 2nd form so as to facilitate triggering of the submission by form1
So this is the revised code:
<form method="post">
<button type="button" class="btn btn-primary"><span class="fe fe-file-plus fe-12 mr-2"></span>New</button>
<button type="button" name="delete" class="btn btn-secondary"
onclick='document.getElementById("form2").submit()';
><span class="fe fe-trash fe-12 mr-2"></span>Delete</button>
</form>
<form id=form2 action="delete-register.php" method="post">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="<?php echo $row['id']; ?>" name="selected[]" value="<?php echo $row['id']; ?>">
<label class="custom-control-label" for="<?php echo $row['id']; ?>"></label>
</div>
<input type=hidden name=delete value="delete">
</form>

How to select perticular class in jquery from a dynamic table

I am trying to create a system to put sequence number of lessons, and i tried to make it unique as u can see that i take a variable $i but no luck . i am new in jquery. dont know how to use (this) in this situation. i am going to use ajax to save the data.
<?php
$chapter_id = $_GET['id'];
$lesson = mysqli_query($conn_course, "SELECT * FROM `lesson_list` WHERE `chapter_id` = '$chapter_id' order by `sequence` asc");
$i=0;
while ($info1 = mysqli_fetch_array($lesson)) {
$lesson_id = $info1['lesson_id'];
$lesson_info = mysqli_query($conn_course, "SELECT * FROM `lesson` WHERE `lesson_id` = '$lesson_id'");
$info = mysqli_fetch_array($lesson_info);
$i++;
?>
<tr>
<td>
<form>
<input type="hidden" class="lesson_id<?=$i?>" value="<?=$lesson_id?>">
<input type="hidden" class="sno" value="<?=$i?>">
<input type="hidden" id="chapter_id" value="<?=$chapter_id?>">
<input class="form-control sequence_no" style="width:27%;" type="number" value="<?= $info1['sequence'] ?>">
</form>
</td>
<td>
<a href="index.php?p=lesson-page&id=<?= $info['lesson_id'] ?>">
<button type="button" class="btn btn-outline-light btn-sm">
<?= $info['lesson_name'] ?>
</button>
</a>
</td>
<td>
<?= strtoupper($info['code']) ?>
</td>
<td>
<?php if($info['video_url'] == 0){}else{ ?>
<a href="https://player.vimeo.com/video/<?= $info['video_url'] ?>" target="blank"><button type="button"
class="btn btn-outline-light btn-sm">Play Video</button></a>
<?php } ?>
</td>
<td>
<form class="" action="../edit/delete.php" method="get" style="float:left; margin-right:5px;">
<input type="hidden" name="chapter_id" value="<?=$_GET['id']?>">
<input type="hidden" name="lesson_id" value="<?=$lesson_id?>">
<input type="submit" class="btn btn-outline-light btn-sm" name="delete_lesson" onclick="return confirm('Do you want to delete it')" value="Delete">
</form>
<a href="index.php?p=lesson-create&task=edit&id=<?= $info['lesson_id'] ?>&page=chapter-page&chapter_id=<?=$chapter_id?>">
<button type="button" class="btn btn-outline-light btn-sm">Edit
</button>
</a>
</td>
</tr>
<?php } ?>
myjquery is
$(document).ready(function () {
$('.sequence_no').on("change", function () {
var sequence_no = $('.sequence_no').val();
var s_no = $('.sno').val();
var chapter_id = $('#chapter_id').val();
var lesson_id = $('.lesson_id' + s_no).val();
alert(s_no);
})
})
alert is showing only the first table row s_no. how to select the lesson id of particular tr
You can simply get closest tr using $(this).closest("tr") and then use .find to get required classes values .
Demo Code :
$('.sequence_no').on("change", function() {
var selector = $(this).closest("tr")
var sequence_no = $(this).val();
var s_no = selector.find('.sno').val();
var chapter_id = selector.find('.chapter_id').val();
var lesson_id = selector.find('input[class*=lesson_id]').val();
console.log(s_no, lesson_id);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<table>
<tr>
<td>
<form>
<!--added class-->
<input type="hidden" class="lesson_id<?=$i?>" value="22">
<input type="hidden" class="sno" value="1">
<input type="hidden" class="chapter_id" value="1">
<input class="form-control sequence_no" style="width:27%;" type="number" value="2">
</form>
</td>
<td>
<a href="index.php?p=lesson-page&id=<?= $info['lesson_id'] ?>">
<button type="button" class="btn btn-outline-light btn-sm">
Smething
</button>
</a>
</td>
<td>
Abc
</td>
<td>
<form class="" action="../edit/delete.php" method="get" style="float:left; margin-right:5px;">
<input type="hidden" name="chapter_id" value="<?=$_GET['id']?>">
<input type="hidden" name="lesson_id" value="1">
<input type="submit" class="btn btn-outline-light btn-sm" name="delete_lesson" onclick="return confirm('Do you want to delete it')" value="Delete">
</form>
<a href="index.php?p=lesson-create&task=edit&id=<?= $info['lesson_id'] ?>&page=chapter-page&chapter_id=<?=$chapter_id?>">
<button type="button" class="btn btn-outline-light btn-sm">Edit
</button>
</a>
</td>
</tr>
<tr>
<td>
<form>
<input type="hidden" class="lesson_id<?=$i?>" value="26">
<input type="hidden" class="sno" value="2">
<input type="hidden" class="chapter_id" value="2">
<input class="form-control sequence_no" style="width:27%;" type="number" value="2">
</form>
</td>
<td>
<a href="index.php?p=lesson-page&id=<?= $info['lesson_id'] ?>">
<button type="button" class="btn btn-outline-light btn-sm">
Smething
</button>
</a>
</td>
<td>
Abc2
</td>
<td>
<form class="" action="../edit/delete.php" method="get" style="float:left; margin-right:5px;">
<input type="hidden" name="chapter_id" value="<?=$_GET['id']?>">
<input type="hidden" name="lesson_id" value="2">
<input type="submit" class="btn btn-outline-light btn-sm" name="delete_lesson" onclick="return confirm('Do you want to delete it')" value="Delete">
</form>
<a href="index.php?p=lesson-create&task=edit&id=<?= $info['lesson_id'] ?>&page=chapter-page&chapter_id=<?=$chapter_id?>">
<button type="button" class="btn btn-outline-light btn-sm">Edit
</button>
</a>
</td>
</tr>
</table>

Use radio buttons to disable dropdown using Foreach loop

I'm working on an app/form which has a table populated from a database via a foreach loop with radio buttons, one for yes, one for no and a dropdown. I need it so that when the Yes radio button is selected, the dd is disabled, and when no is selected, it is enabled. I have it working, but it only works for the top row, and I can't get all of the Yes radio buttons to be pre-selected. So in short, I can't figure out how to pre-select all of the yes rd btns, which in turn would disable the dd's. Any help would be great. Thanks.
<?php
//Foreach loop iterates through each column of $getallrows function
foreach($allRows as $rowID => $rowInfo){ ?>
<tr>
<td><?php echo $rowInfo['fpID'];?></td>
<td><?php echo $rowInfo['shortTitle'];?></td>
<td><?php echo $rowInfo['PI'];?></td>
<td><?php echo $rowInfo['Department'];?></td>
<td><?php echo $rowInfo['Division'];?></td>
<td><?php echo $rowInfo['sponsorName'];?></td>
<td><?php echo $rowInfo['Date_Project_Start']->format('Y-m-d');?></td>
<td><?php echo $rowInfo['Date_Project_End']->format('Y-m-d');?></td>
<td><?php echo $rowInfo['fundingType'];?></td>
//Radio buttons
<td>Yes<input type="radio" name="rdGrant" value="Yes" id="rdYes" onclick="disable()" checked="checked"/><br />
No<input type="radio" name="rdGrant" value="No" id="rdNo" onclick="enable()"/></td>
<form action="classes/functions.class.php" method="POST">
<input type="hidden" name="id" value="<?php echo $fpID; ?>"/>
<td>
//Drop down
<div class="dropdown">
<button class="btn btn-secondary btn-sm dropdown-toggle" type="button" name="ddgrantGroup" id="ddgrantType" data-toggle="dropdown">Select Proper Funding Type
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="ddgrantType">
<li><a data-value="Corporate Sponsor">Corporate Sponsor</a></li>
<li><a data-value="Federal">Federal</a></li>
<li><a data-value="Foundation Selected">Foundation Selected</a></li>
<li><a data-value="Internally Funded">Internally Funded</a></li>
<li><a data-value="State/Local">State/Local</a></li>
</ul>
</div>
</td>
<td>
<div class="update">
<!-- Update and Save -->
<button type="submit" class="btn btn-success" name="Update">Update and Save</button>
</div>
</td>
<td>
<div class="comment">
<textarea class="form-control" aria-label="With textarea" id="grantComment" placeholder="Comments"></textarea>
<button type="submit" class="btn btn-success" name="updateComm">Add Comments</button>
<!-- Add Comment -->
</div>
</td>
</form>
</tr>
<?php } ?>
</tbody>
Function behind the radio buttons disable/enable of the dd.
function disable() {
document.getElementById("ddgrantType").disabled=true;
}function enable() {
document.getElementById("ddgrantType").disabled=false;
}
You have to use a dynamic selector (class/id) for your button instead of constant id #ddgrantType.
Try something like this-
<?php $dynamic_id = 'ddgrantType_'.$rowInfo['fpID'];?>
<td>Yes<input type="radio" name="rdGrant" value="Yes" id="rdYes" onclick="disable('<?php echo $dynamic_id;?>')" checked="checked"/><br />
No<input type="radio" name="rdGrant" value="No" id="rdNo" onclick="enable('<?php echo $dynamic_id;?>')"/></td>
<form action="classes/functions.class.php" method="POST">
<input type="hidden" name="id" value="<?php echo $fpID; ?>"/>
<td>
//Drop down
<div class="dropdown">
<button class="btn btn-secondary btn-sm dropdown-toggle" type="button" name="ddgrantGroup" id="<?php echo $dynamic_id;?>" data-toggle="dropdown">Select Proper Funding Type
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="ddgrantType">
<li><a data-value="Corporate Sponsor">Corporate Sponsor</a></li>
<li><a data-value="Federal">Federal</a></li>
<li><a data-value="Foundation Selected">Foundation Selected</a></li>
<li><a data-value="Internally Funded">Internally Funded</a></li>
<li><a data-value="State/Local">State/Local</a></li>
</ul>
</div>
</td>
<script>
function disable(btn_id) {
document.getElementById(btn_id).disabled=true;
}
function enable(btn_id) {
document.getElementById(btn_id).disabled=false;
}
</script>

How can I update all input fields with one button and make a button to delete items individually?

I've been making a shopping cart where I display the products with a delete button for each product and an update button to update all the quantity fields.
The problem is that when I have managed to make update button work the delete button delete the last product not the one that I wanted to.
I know is a problem with the form but i do not know how to resolve it.
$i = 0;
while($row = $result->fetch_assoc()){
//loop product info
echo'<form action="" method="POST">
<input class="inp" type="hidden" name="h_order" value="'.$row['order_id'].'"/>
<input class="inp" type="hidden" name="order[]" value="'.$row['order_id'].'"/>
<input class="inp" style="width:30px;" type="text" name="';
echo 'quantity[$row['order_id']]';
echo 'value="'.$row['quantity'].'" autocomplete="off"/>';
if($i == 0){
echo'<table>';
}
echo '<td>'.$total.'</td>
<td>
<center>
<button class="action red" type="submit" name="delete" title="Delete">
<i class="fa fa-trash-o fa-2x"></i>
</button>
</center>
</td>
</tr>';
$i++;
}
echo '<tr>
<td colspan="6">
<button class="button action blue" style="float:right;" type="submit" name="update" title="Update quantity">
<i class="fa fa-refresh fa-lg"></i>
<a>Update cart</a>
</button>
</td>
</tr>
</table>
</form>';

PHP : How to put and pass variable in modal URL

So, I have a button to give a direct link to modal in same page.
this is the button and url
<a data-toggle="modal"
href="main_user.php?user_id=<?php echo $user['user_id']; ?>#myModal"
class="btn btn-warning">
( I try to echo the $user_id on before #modal ) is it right ?
and after I click the button, the modal will appear.
This is the modal with the form.
<form class="form-login" action="action/doEditUserStatus.php" method="post">
<div class="login-wrap">
<div class="changestatus">
<p>Banning or Activing User Status</p></div>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-success active">
<input type="radio" name="options" value="1" autocomplete="off" checked> Actived
</label>
<label class="btn btn-primary">
<input type="radio" name="options" value="2" autocomplete="off"> Banned
</label>
</div>
</div>
<div class="modal-footer">
<button data-dismiss="modal" class="btn btn-default" type="button">Cancel</button>
<button class="btn btn-theme" name="modalSubmit" type="submit">Submit</button>
</div>
</form>
And then I try to submit the modal form, but the action cannot read the $user_id which I put before #modal.
UPDATE :
Table code :
So this is my table :
<tr class="success">
<th class="numeric">ID</th>
<th class="numeric">E-mail</th>
<th class="numeric">Name</th>
<th class="numeric">Phone</th>
<th class="numeric">Picture</th>
<th class="numeric">Status</th>
<th colspan="2" class="numeric">Action</th>
</tr>
<tr>
<td class="numeric"><?php echo $user['user_id']; ?></td>
<td class="numeric"><?php echo $user['email']; ?></td>
<td class="numeric"><?php echo $user['name']; ?></td>
<td class="numeric"><?php echo $user['phone']; ?></td>
<td class="numeric"><?php echo $user['picture']; ?></td>
<td class="numeric">
<a data-toggle="modal" href="main_user.php?user_id=<?php echo $user['user_id']; ?>#myModal" class="btn btn-warning">
<span class="glyphicon glyphicon-edit" aria-hidden="true"></span> Change Status
</a>
</td>
</tr>
The main problem is :
When I click the button, then the modal will appear but it can't get the $user_id from that button?
Put a hidden input field with the user_id into your form:
<input type="hidden" name="user_id" value="<?php echo $user['user_id']; ?>">
EDIT: If you mean Bootstrap try this:
Link/Button:
<a data-toggle="modal" data-userid="<?php echo $user['user_id']; ?>"
href="main_user.php#myModal"
class="btn btn-warning">
Hidden Form Field:
<input type="hidden" name="user_id" value="">
Javascript:
$('#myModal').on('show.bs.modal', function(e) {
var userid = $(e.relatedTarget).data('userid');
$(e.currentTarget).find('input[name="user_id"]').val(userid);
});
See also http://getbootstrap.com/javascript/#modals-related-target and Passing data to a bootstrap modal
You should have mentioned Bootstrap in your question. PHP is not the appropriate tag.
This will work if your link will do a redirection :
You will need to add an hidden input within your form with the $_GET['user_id'] variable (that contains the value of the parameter in the url) :
<input type="hidden" name="user_id" value="<?php echo (int)$_GET['user_id'] ?>" />
but, if I'm right, this link is handle by twitter bootstrap that will prevent its default behaviour. So there is no actual redirection. What you can do, is use a custom data attribute in the link :
<a data-toggle="modal"
data-userid="<?php echo $user['user_id']; ?>"
href="#myModal"
class="btn btn-warning">
And handle this with a bit of javascript, to add the hidden input field to the form :
$(document).ready(function(){
$('[data-userid]').click(function(){
var userid = $(this).data('userid');
var form = $('form.form-login');
var input = $('<input type="hidden" name="user_id" value="'+userid+'" />');
if (form.find('input[name="user_id"]').length) {
form.find('input[name="user_id"]').val(userid);
} else {
form.prepend(input);
}
});
});
how about this add the id to the data-target
click me
now in your modal id add this
myModal-<?php echo $user['user_id'];?>
and you can load the data dynamically base on that id

Categories