how to check all check boxes which shown throgh for loop? - php

I want to check all the check boxes which are in div tag
My code is as below.
<div id="cBoxes">
<li><input class="radio" name="list" id="list" type="checkbox"
value="<?php echo $resultPages['id'];?>"></li>
</div>
I have put this whole code in for loop with php tag.
Is there any way to accomplish this task.

Set the checked attribute of checkbox. w3.org has complete documentation.

See the checked attribute:
<div id="cBoxes">
<ul>
<li><input class="radio" name="list" id="list" type="checkbox"
value="<?php echo $resultPages['id'];?>" checked="checked"/></li>
</ul>
</div>

Related

Using PHP to set checked option on a radio button based on data from database. BUT it only works on some data

Here is the code snippet, which is inside a form:
<fieldset>
<!-- for each radio button, PHP code used to check IF $id==value
to determine what should be checked. -->
<div class="newrow">
<label>Status:</label>
<div class="radio-group">
<input type="radio" id="active" value="active" <?php echo ($status_id==1)?'checked':'' ?> name="status">
<label for="active">Active</label>
<input type="radio" id="on-leave" value="leave" <?php echo ($status_id==2)?'checked':'' ?>name="status">
<label for="on-leave">On Leave</label>
<input type="radio" id="terminated" value="terminated" <?php echo ($status_id==3)?'checked':'' ?>name="status">
<label for="terminated">Terminated</label>
</div>
</div>
<div class="newrow">
<label>Eligible for rehire?</label>
<div class="radio-group">
<input type="radio" id="yes" value="yes" <?php echo ($rehire_id==1)?'checked':'' ?>name="rehire">
<label for="yes" class="radio_label">Yes</label>
<input type="radio" id="no" value="no" <?php echo ($rehire_id==2)?'checked':'' ?> name="rehire">
<label for="no" class="radio_label">No</label>
</div>
</div>
</fieldset>
The first thing I want to point out is that in the first row, I decide what to check based on $status_id. For this row if $status_id == 1, the first radio button is checked. BUT if it equals 2 or 3, nothing gets checked.
But in the second row, I decide what to check based on $rehire_id. If $rehire == 2, the second button is checked, BUT NOTHING happens if it equals 1.
WHAT is going on here? Any ideas?
You have no space between checked and the name tag in the cases where it's not working.
When $status_id==2 is true
value="leave" <?php echo ($status_id==2)?'checked':'' ?>name="status">
will output
value="leave" checkedname="status">
instead of what you expected
value="leave" checked name="status">
Add a space before each name=

Issues with 'checked' attribute in 'input' form tags within PHP while loops

I'm using a html form to edit sql db, thus I would want to have the form display the value currently in the database. I'm Using a while loop to display all the sql rows. I simple radio button allowing user to chose between 'Listings' (shows as '0' in sql) or 'Recent Transactions" (shown as '1' in sql).
The radio buttons are not pre-filling the value from sqlas checked or not
<form name="edit listing" action="edit_list.php" method="post" id="edit_form" >
<ul>
<?php while ($data=mysqli_fetch_assoc($result)):
$transaction = $data['transaction'];
$chkvalue='checked="checked"'; ?>
<li>
<fieldset>
<legend>Designation <h6>Required</h6></legend>
<input name="transaction" type="radio" tabindex="11" value="0" <?php if ($transaction == '0') echo $chkvalue; ?> />
<label for="listings">Listings</label>
<input name="transaction" type="radio" tabindex="12" value="1" <?php if ($transaction == '1') echo $chkvalue; ?> />
<label for="recent_transactions">Recent Transactions</label>
</fieldset>
<input type="submit" formaction="edit_list.php" value="Submit Changes" />
</form>
</li>
<?php endwhile;mysqli_close($con);?>
</ul>
The short php insert is working, a little. My source code is looking something like this:
<input name="transaction" type="radio" tabindex="11" value="0" />
<label for="listings">Listings</label>
<input name="transaction" type="radio" tabindex="12" value="1" checked="checked" />
<label for="recent_transactions">Recent Transactions</label>
But the radio button is not pre-filling.
I've been poking at this all day, any suggestion here where problem could be would be extremely helpful
You only have to write checked not check="checked"!
So change it to this:
$chkvalue='checked'; ?>
instead of this:
$chkvalue='checked="checked"'; ?>

radio button multiple selection

I have many radio buttons with different name attribute and I am facing one problem. Every time I click on each radio button, I can select all. I am using the radio buttons for redirecting to other pages.
I don't want to have multiple selection just only one. I am using twitter bootstrap tabs for showing multiple content on one page just by switching the tabs if that has something to do with the prob. Can someone help me out?
php
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['cleat']) && !empty($_POST['cleat'])) {
header("location: sporting_goods");
exit();
}
if (isset($_POST['cell']) && !empty($_POST['cell'])) {
header("location: cellphones");
exit();
}
}
?>
css
.radto{vertical-align:top;}
.sell_ali{display:inline-block;}
html
<form action="" method="post">
<div class="tabbable tabs-left" style="margin-bottom: 18px;">
<ul class="nav nav-tabs">
<li class="active">Electronics</li>
<li>Sporting Goods</li>
</ul>
<div class="tab-content" style="padding-bottom: 9px; border-bottom: 1px solid #ddd;">
<div class="tab-pane" id="tab2">
<div class="sell-ali">
<li><input type="radio" class="radto" name="camera"/> Cameras</li>
<li><input type="radio" class="radto" name="cell"/> Cell Phones</li>
<li><input type="radio" class="radto" name="cell_cover"/> Cell Phone Covers</li>
</div>
</div>
<div class="tab-pane" id="tab2">
<div class="sell-ali">
<li><input type="radio" class="radto" name="ball"> Balls</li>
<li><input type="radio" class="radto" name="cleat"> Cleats</li>
<li><input type="radio" class="radto" name="jersey"> Jersey</li>
</div>
</div>
</div>
<input type="submit" value="next" name="submit">
</form>
Give then the same name. That is how you create a radio button group.
Use the value for the piece of data that the user is picking from that group.
If you want to allow just one choice you should give them same name buddy. then set the value you want to send to server for each one.

Not returning selected pciture name

I have a database with pictures that i can delete from or use them in different parts of the the website by choosing one of the radio buttons in the popup.Problem is everytime I click I click on a picture the name of the first picture in the database comes up not the one from the picture i clicked . What's wrong? I used PHP 5.3 and HeidiSQL
<?php
// ...
$result=mysql_query("SELECT * FROM imagini WHERE menu_id=6");
while($data=mysql_fetch_row($result)){ ?>
<div class="tag">
<div id='container_poze_originale'>
Alege alt rol
<div id="my_popup" style="display:none;border:1px dotted gray;padding:.3em;background-color:white;position:absolute;width:auto;left:100px;top:100px">
close
<form action="popup.php" method="post" >
<input type="text" name="alt-rol" value="<?php echo $data[1];?>" /> //always returns the name of the first picture in the database not the one i select
<input type="submit" value="Adauga imagine" class="buton_imagine" />
<div class="radio">
<input type="radio" name="tip_imagine" value="0"/><label for="tip_imagine" class="radio2">Logo</label>
<input type="radio" name="tip_imagine" value="1"/><label for="tip_imagine" class="radio2">Slider</label>
<input type="radio" name="tip_imagine" value="2"/><label for="tip_imagine" class="radio2">Hot destinations</label>
<input type="radio" name="tip_imagine" value="3"/><label for="tip_imagine" class="radio2">Pachete</label>
<input type="radio" name="tip_imagine" value="4"/><label for="tip_imagine" class="radio2">Reclama</label>
<input type="radio" name="tip_imagine" value="5"/><label for="tip_imagine" class="radio2">Background</label>
</div>
</form>
</div>
<div class="imagine_originala">
<img src= "../upload/original/<?php echo $data[1];?>" /></a>
</div>
<div class="Btag" style="display:none;">
<div id="buton_slide4" >
Sterge</td>
</div>
</div>
</div>
</div>
<?php
}
?>
I think I understand your question. You are clicking on
Alege alt rol
and you are surprised that the same <div> always appears?
Well, it is simply because an id should be unique. And if you cycle through your images you create a new <div> each time with the same id: my_popup. When clicking on the link (no matter which one), JavaScript will then just take the first <div> it finds with the id my_popup. So you should just assign an unique id to each div so your JavaScript knows which one to open. You could do this by appending your image id to the div id (assuming you have a unique image id in $data[1]).
So change it to (shortened):
<? while($data=mysql_fetch_row($result)) { ?>
<div class="tag">
<div id="container_poze_originale">
Alege alt rol
<div id="my_popup<?=$data[1]?>" style="display:none;border:1px dotted gray;padding:.3em;background-color:white;position:absolute;width:auto;left:100px;top:100px">
...
</div>
</div>
</div>
<? } ?>
Oh and it might be good to put all those huge inline CSS attributes into an external stylesheet, so it will be easier to maintain/change your webpage later on. And it makes things a bit more readable... :-)
I think your form is ok when you click on the radio but not when you click on the label.
The for attribute of the label need an ID as reference like this :
<input type="radio" name="tip_imagine" value="0" id="tip0" /><label for="tip0" class="radio2">Logo</label>
<input type="radio" name="tip_imagine" value="1" id="tip1"/><label for="tip1" class="radio2">Slider</label>
<input type="radio" name="tip_imagine" value="2" id="tip2"/><label for="tip2" class="radio2">Hot destinations</label>
<input type="radio" name="tip_imagine" value="3" id="tip3"/><label for="tip3" class="radio2">Pachete</label>
<input type="radio" name="tip_imagine" value="4" id="tip4"/><label for="tip4" class="radio2">Reclama</label>
<input type="radio" name="tip_imagine" value="5" id="tip5"/><label for="tip5" class="radio2">Background</label>
With your actual code, the FOR is always the same so you get always the first.

manipulating radio box selection with javascript

i'm trying to do a poll but i designed it without any radio box in it. So i decided to make the selection being highlighted with a different background color, all is done with jquery.
I set the display of the radio box to none so that it wouldn't show, gave each a unique ID. Here's the script.
<form action="v_poll.php" method="post">
<ul class="voting">
<li class="voting votetext"><input type="radio" name="voting" value="a1" style="display:none;" id="a1"><a onClick="vote('a1')"Answer 1</a></li>
<li class="voting votetext"><input type="radio" name="voting" value="a2" style="display:none;" id="a2"><a onClick="vote('a2')">Answer 2</a></li>
<li class="voting votetext"><input type="radio" name="voting" value="a3" style="display:none;" id="a3"><a onClick="vote('a3')">Answer 3</a></li>
<input type="hidden" value="1" name="id" />
<input type="submit" value="submit">
</ul>
</form>
<script type="text/javascript">
function vote(TheValue) {
GetElementById(TheValue).checked=true;
}
</script>
But when i checked the value of the radio box with $_POST['voting'], it is blank. Not the value assigned to the radio box. Anything i'm doing wrong?
Please help. Thanks.
You can do this without javascript as well, using a <label> with for="id" instead of <a>, like this:
<li class="voting votetext">
<input type="radio" name="voting" value="a1" style="display:none;" id="a1">
<label for="a1">Answer 1</label>
</li>
This is nothing new/HTML5 or anything, any browser IE6+, and probably older will support this.
GetElementById(TheValue).checked=true;
should instead be:
document.getElementById(TheValue).checked=true;

Categories