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

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>

Related

Cannot display edit form when clicked on edit button and the form doesn't parse error and displays blank page

EDIT: My edit button refuse to work.
I'm trying to edit this page, but it seems to be breaking when I click the edit button. it is retrieving data from the database and displaying on faculty.php successfully while if I try to edit it shows a blank page. what is wrong with my code, please.
faculty.php: a separate page where i have my edit button
<td>
<form action="facultyedit.php" method="POST">
<input type="hidden" name="edit_id" value="<?php echo $row['id']; ?>" >
<button type="submit" name="edit_data_btn" class="btn btn-success">EDIT</button>
</form>
</td>
facultyedit page: where i receive the edit button
<?php
if (isset($_POST['edit_data_btn']))
{
$id = $_POST['edit_id'];
$query = " SELECT * FROM register WHERE id='$id' ";
$query_run = mysqli_query($connection, $query);
foreach ($query_run as $row ) {
?>
<form action="" method="POST" >
<input type="text" name="edit_id" value="<?php echo $row['id'] ?>" >
<div class="form-group">
<label>Name:</label>
<input type="text" name="edit_name" value="<?php echo $row['name']; ?>" class="form-control" >
</div>
<div class="form-group">
<label>Designation:</label>
<input type="text" name="edit_designation" value="<?php echo $row['design']; ?>" class="form-control" >
</div>
<div>
<label>Description:</label>
<input type="text" name="edit_description" value="<?php echo $row['descrip']; ?>" class="form-control" >
</div>
<div class="form-group">
<label>Upload Image:</label>
<input type="file" name="edit_faculty_image" id="faculty_image" value="<?php echo $row['image']; ?>" class="form-control" >
</div>
<div class="">
Cancel
<button type="update" name="update_aboutusbtn" class="btn btn-primary">Update</button>
</div>
</form>
<?php
}
}
?
I have fixed the issue:
The problem was that I was selecting a register table from the database instead of the faculty table.

How get the value from repetition form

I try to make a multiple choice for quiz, and the question is from the database and also the answer, so i code the program like this:
<form role="form" method="POST" action="test/hitung">
<div class="tab-content">
<?php
$no = 0;
foreach ($data_pilihan as $row){
$no++;
?>
<?php
if($no == 1){
$status = "active";
}else{
$status = "";
}
?>
<div class="tab-pane <?php echo $status;?>" role="tabpanel" id="<?php echo "step".$no?>">
<h3>Pertanyaan</h3>
<p>This is step 1</p>
<form action="">
<input type="radio" name="<?php echo "pertanyaan".$no?>" value="<?php echo $row->simbol_a?>"><?php echo $row->pernyataan_a?><br>
<input type="radio" name="<?php echo "pertanyaan".$no?>" value="<?php echo $row->simbol_b?>"><?php echo $row->pernyataan_b?><br>
<input type="radio" name="<?php echo "pertanyaan".$no?>" value="<?php echo $row->simbol_c?>"><?php echo $row->pernyataan_c?><br>
<input type="radio" name="<?php echo "pertanyaan".$no?>" value="<?php echo $row->simbol_d?>"><?php echo $row->pernyataan_d?>
</form>
<ul class="list-inline" align="center">
<?php if($no != 1): ?>
<li><button type="button" class="btn btn-default prev-step">Previous</button></li>
<?php endif?>
<?php if($no != $Jumlah_Data):?>
<li><button type="button" class="btn btn-primary next-step">continue</button></li>
<?php endif?>
<?php if($no == $Jumlah_Data):?>
<li><button type="submit" class="btn btn-primary next-step">finish</button></li>
<?php endif?>
</ul>
</div>
<?php }
?>
<div class="clearfix"></div>
</div>
</form>
but when I try to get a data from the form value of the radio button and then I print, it just show the first value. How can i print or get another or rest of the value?
if all the name of the radio buttons are same , you will get only one value
As the Definition is a graphical control element that allows the user to choose only one of a predefined set of mutually exclusive options
For eg: In this case user can select only one out of three options
<form action="" method="post">
<input type="radio" name="gender" value="male"> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
</form>
From PHP you can access as - here u get only one selected value
<?PHP
$selected_radio = $_POST['gender'];
print $selected_radio;
?>
if you need multiple selection you can make use of checkbox
if your case if you need one options out of 4 need to be selected give a common name to radio button
please add if anything is missing

How to get values of selected dropdown box display in url

I'm setting up an eCommerce website and I need to select product quantity user enters in drop down. I have tried this but it gives me last quantity not the selected. I'm still in the process of creating the addtocart.php file but before that I need to pass the values so that I can use them in a later stage.
I have implemented the following code
<div class="product-cartq">
<div class="product-quantity pull-left">
<span>Quantity:</span>
<select name="quantity_option">
<?php for($qty = 1; $qty < 11; $qty++){
echo '<option value="'.$qty.'">'.$qty.'</option>';
}?>
</select>
</div>
<div class="btn_addtocart">
<form method="GET" action="cart.php" enctype="multipart/form-data">
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>">
<input type="hidden" name="quantity_option" value="<?php echo $qty; ?>">
<input class="btn btn-primary btn-submit" type="submit" name="add_to_cart" value="add to cart"/>
</form>
</div>
</div>
I can't proceed as I'm unable to get the value. Any suggestions or improvements on this?
Do this:
<form method="GET" action="cart.php" enctype="multipart/form-data">
<div class="product-cartq">
<div class="product-quantity pull-left">
<span>Quantity:</span>
<select name="quantity_option">
<?php for($qty = 1; $qty < 11; $qty++){
echo '<option value="'.$qty.'">'.$qty.'</option>';
}?>
</select>
</div>
<div class="btn_addtocart">
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>">
<input type="hidden" name="quantity_option" value="<?php echo $qty; ?>">
<input class="btn btn-primary btn-submit" type="submit" name="add_to_cart" value="add to cart"/>
</div>
</div>
</form>
The select control is outside form tag.

Can't retrieve values from other page (Stored As Label)

I am new to PHP and I am trying to get information from a form to another page, but the data won't transfer over when I hit submit. What am I doing wrong? Should I be trying to use GET instead of POST? What is the best way to debug something like this?
The path to information.php is definitely correct.
<form action="information.php" method="post" type="post">
<div class="row" style="padding-bottom: 20px;">
<label name="tempID"><?php echo $number; ?></label>
<button class="btn" name="submit" type="submit">More Details</button>
</div>
</form>
This file is in a different page (information.php)
if (isset($_POST["tempID"]))
{
$infoID = $_POST['tempID'];
}
echo $infoID;
<input type="hidden" name="tempID" value="<?php echo $number; ?>" />
Add this next to your original echo, post variables can't be stored in a label. Also remove the name value from the label
You need a submit input instead of a button with the name submit. Change the button's html to:
<input type='submit' value='More Details'>
Change Label in input
<form action="information.php" method="post">
<div class="row" style="padding-bottom: 20px;">
<input name="tempID" value="<?php echo htmlentities($number); ?>"/>
<button class="btn" name="submit" type="submit">More Details</button>
</div>
</form>
labels are only to display information. they are not submitted during form submission.
<form action="information.php" method="post" type="post">
<div class="row" style="padding-bottom: 20px;">
<label>Number:</label>
<input name="tempID" value="<?php echo $number; ?>"/>
<input class="btn" id="submit" name="submit" type="submit" value="More Detail" />
</div>
</form>

cannot save/retrieve value using html input in a foreach with php

For some probably obvious to anyone else reason, I cannot save the input value of my radio buttons and retrieve them with php. $_POST['answerToQuestion'] is not empty and will print the $key of each, but the value is empty. Can anyone readily see my mistake?
html:
<form action="answerQuestion.php" method="post">
<?php foreach($questions as $k => $q):
if(!$q['is_subquestion']):?>
<div class="questionAnswer">
<?php echo $q['body']; ?><br/>
<div class="btn-group" data-toggle="buttons-radio">
<button type="button" class="btn" id="answer_yes" name="answerToQuestion[<?php echo $k; ?>]" value= 1>Yes</button>
<button type="button" class="btn" id="answer_no" name="answerToQuestion[<?php echo $k; ?>]" value= 0>No</button>
<button type="button" class="btn" id="answer_na" name="answerToQuestion[<?php echo $k; ?>]" value= 2>N/A</button>
</div>
<input type="hidden" id="hidden_2" name="answerToQuestion[<?php echo $k; ?>]" value="">
</div>
<?php endif;?>
<?php endforeach; ?>
<input type="submit" value="Next" name="submit-form" />
</form>
php:
foreach($_POST['answerToQuestion'] as $key=>$value)
{
echo ' '.$value.'<br/>';
}
Basing this answer on the question which clearly states "my radio buttons".
You don't need to echo the $k variable and you should use actual radio buttons:
<form action="answerQuestion.php" method="post">
<div class="btn-group" data-toggle="buttons-radio">
<label><input type="radio" name="answerToQuestion" value="1"> Yes</label>
<label><input type="radio" name="answerToQuestion" value="0"> No</label>
<label><input type="radio" name="answerToQuestion" value="2"> N/A</label>
</div>
<input type="submit" value="Submit">
</form>
Also, remove the spaces value= 0 (from your example), I use quotes as well.
$_POST['answerToQuestion'];
That should work.

Categories