Use radio buttons to disable dropdown using Foreach loop - php

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>

Related

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>

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

Access Value of While Loop Outside it

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 } ?>

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