how to show textbox if radio button value is checked from database? - php

I have 3 radio buttons
<input type="radio" name="preferedpaymt" value="CheckMailed" id="CheckMailed1" <?php if($paymnt=="CheckMail") {?> checked="checked" <?php }?> />
(a)Check Mailed
<input type="radio" name="preferedpaymt" value="PayPal" <?php if($paymnt=="PayPal") {?> checked="checked" <?php }?> />
(b)By PayPal
<input type="radio" name="preferedpaymt" value="WireTransfer" id="WireTransfer" <?php if($paymnt=="wireTransfer") {?> checked="checked" <?php }?> />
(c)Wire Transfer To Bank A/c$12
But the value of radiobutton is checked from database . With these 2 tables are associated
if Radiobutton 2 is checked table1 1 should populate id tb1
here the problem is when a value of radio button is checked from database the table does not get populate if you only click it gets populated i want it automatis how can i do it?
jquery code, which I am using
<script type="text/javascript">
jQuery(function () {
jQuery("input[name=preferedpaymt]").change(function () {
if ($(this).val() == "PayPal") {
jQuery("#ppl").slideDown()
} else {
jQuery("#ppl").slideUp();
}
});
});
</script>

Or you culd use:
var chk_val = jQuery("input[name='preferedpaymt']:checked").val();
alert(chk_val);
EDIT: how to use it!
Your inputs:
<input type="radio" name="preferedpaymt" value="CheckMailed" id="CheckMailed1" <?php if($paymnt=="CheckMail") {?> checked="checked" <?php }?> />
(a)Check Mailed
<input type="radio" name="preferedpaymt" value="PayPal" <?php if($paymnt=="PayPal") {?> checked="checked" <?php }?> />
(b)By PayPal
<input type="radio" name="preferedpaymt" value="WireTransfer" id="WireTransfer" <?php if($paymnt=="wireTransfer") {?> checked="checked" <?php }?> />
(c)Wire Transfer To Bank A/c$12
Then lets say you got one tables for each payment choise. ex:
NOTE All tables has the same classname this is because i want to hide all of them by default. The id attribute is set to "table_paymentmethod".
<table class="payment_table" id="table_CheckMailed" height="200" bgcolor="#00FF33" width="100px;" >
<tr>
<td>
You checked Checked mail
</td>
</tr>
</table>
<table class="payment_table" id="table_PayPal" height="200" bgcolor="#FF0000" width="100px;" >
<tr>
<td>
You checked Paypal
</td>
</tr>
</table>
<table class="payment_table" id="table_WireTransfer" height="200" bgcolor="#CCC" width="100px;" >
<tr>
<td>
You checked wiretransfer
</td>
</tr>
</table>
Now to the part where jquery comes in handy.
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('.payment_table').hide(); // Hide all tables by default
var chk_val = jQuery("input[name='preferedpaymt']:checked").val(); // Grab the value of the selected radio
jQuery('#table_'+chk_val).slideDown(1000); // Slides down the right table on page load.
});
</script>
Well there you go, should do the trick for you. Im not the best at explaining but it should be pretty straightforward.

Related

Why my checkbox doesn't work? I'm working with codeigniter and here is my view

I want make checkbox for checking all sub checkboxes.
My checkbox:
<table width="30%" class="table striped hovered cell-hovered border bordered">
<tr valign="middle">
<td><b>IDPEL</b></td>
<td><b>No. Baris</b></td>
<td><b><input type="checkbox" id="pilihsemua"/> Pilih Semua</b></td>
</tr>
<?php
foreach ($panel_error as $key) {
echo"<tr><td>".$key->errpanel."</td>";
echo"<td>".$key->nomorBaris."</td>";
echo"<td>";
echo form_checkbox('chk_boxes1[]',$key->errpanel);
echo"</td></tr>";
}
?>
</table>
and here is my script:
<script type="text/javascript">
$(document).ready(function () {
$('.chk_boxes').click(function(){
$('.chk_boxes1').attr('checked',checked)
})
$('#table1').dataTable();
$('#table2').dataTable();
//checkbox
$("#pilihsemua").click(function () { // If #pilihsemua checked, all checkbox will be checked.
$('.chk_boxes1[]').attr('checked', checked);
});
// if all sub checkboxes are being checked, #pilihsemua will automatically checked.
$(".chk_boxes1[]").click(function(){
if($(".chk_boxes1[]").length == $(".chk_boxes1[]:checked").length) {
$("#pilihsemua").attr("checked", "checked");
} else {
$("#pilihsemua").removeAttr("checked");
}
});
//end of checkbox
});
</script>
But still, I don't know why, it can't be work. I try to check #pilihsemua but all the sub classes doesn't be checked. Or if I checked all the sub classes, the #pilihsemuadoesn't be checked too.
Maybe Its close to this:
But this script has some flaws.. Just try to figure out whats wrong..
Example:
HTML CODE
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<table width="30%" cellspacing="1" cellpadding="1" border="collapse">
<tr>
<td><b><input type="checkbox" class="group" chk="g1"/> Pilih Semua</b></td>
<td><b><input type="checkbox" class="group" chk="g2"/> Pilih Semua</b></td>
<td><b><input type="checkbox" class="group" chk="g3"/> Pilih Semua</b></td>
</tr>
<tr>
<td>
<input type="checkbox" class="g1"/>
<input type="checkbox" class="g1"/>
<input type="checkbox" class="g1"/>
</td>
<td>
<input type="checkbox" class="g2"/>
<input type="checkbox" class="g2"/>
<input type="checkbox" class="g2"/>
</td>
<td>
<input type="checkbox" class="g3"/>
<input type="checkbox" class="g3"/>
<input type="checkbox" class="g3"/>
</td>
</tr>
</table>
the script: Jquery
$(function(){
$(".group").click(function(){
var click = $(this).attr('chk');
var current = $("."+click).attr('checked');
if(current){
$("."+click).removeAttr('checked');
}else{
$("."+click).attr('checked','checked');
}
})
})
check this out real simulation

How to hide data using radio button after page onload in php or jquery?

In my program have radio buttons for selecting specific event or all events. this my code is
index.php:
<tr>
<td width="15%" align="left">View entry statistics<span class="mandetory_text">*</span></td>
<td width="2%" align="left" valign="middle">:</td>
<td width="1%"><input name="e_access" id="e_access_<?php echo $edit_row['id']; ?>" type="radio" value="yes" <?php if($edit_row['access_status']=='yes') {?> checked="checked" <?php }?> /></td>
<td width="11%">Permitted</td>
<td width="1%"><input name="e_access" id="e_access_<?php echo $edit_row['id']; ?>" type="radio" value="true" <?php if($edit_row['access_status']=='true') {?> checked="checked" <?php }?> /></td>
<td width="13%">Not permitted</td>
<td width="60%"></td>
</tr>
this code is using for creating and editing an event. So when i am creating event default all events radio button is checked. And also for specific radio button click display one table need chance for selecting some events up to here it is fine but when i am edit this same event all the data displayed in this page here i want is if create with all events radio button no need to display table as the same way create with specific events(radio button) need to show table what are the events are selected and tick mark(select) radio button. for this i am write a code in script
<script>
$(document).ready(function() {
var selected = $("input[type='radio']:checked").val();
if(selected == 'all')
{
$(".edit_veri_table").hide();
}
});
</script>
but in edit page radio buttons(all events and specific events) are showing properly when the page is load display the table(if already selected in specific event button).
when specific button click display table and all events button click hide the table how can i do it please..
when specific button click display table
If you simply want to show on a specific button and hide on rest then you can try this:(demo consists a static dataset)
$(document).ready(function(){
$("button").on("click",function(){
if(this.id == "show"){
$(".table").show();
}else{
$(".table").hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
</script>
<table class='table'>
<tr>
<td width="15%" align="left">View entry statistics<span class="mandetory_text">*</span></td>
<td width="2%" align="left" valign="middle">:</td>
<td width="1%"><input name="e_access" id="e_access_1" type="radio" value="yes" checked="checked" /></td>
<td width="11%">Permitted</td>
<td width="1%"><input name="e_access" id="e_access_2; ?>" type="radio" value="true" /></td>
<td width="13%">Not permitted</td>
<td width="60%"></td>
</tr>
</table>
<button id="show">SHOW</button> <button id="hide">HIDE!!</button> <button id="hide1">HIDE!</button>
NOTE: You should have always unique id's on the page.So:
<td width="1%"><input name="e_access" id="e_access_<?php echo $edit_row['id']; ?>" type="radio" value="yes" <?php if($edit_row['access_status']=='yes') {?> checked="checked" <?php }?> /></td>
<td width="1%"><input name="e_access" id="e_access_<?php echo $edit_row['id']; ?>" type="radio" value="true" <?php if($edit_row['access_status']=='true') {?> checked="checked" <?php }?> /></td>
are not valid since they both will have same ids.either use class or make sure to create unique ids.

How do I make my radio buttons checked

I have a table, that generates rows dynamically. I have used radio buttons for each row. I want the radio button to be checked even after I click my submit button.
This is body of my table:
<form action="" method="POST">
<table>
<thead>
<tr>
<td></td>
<td><h3>Date<h3></td>
<td><h3>Status<h3></td>
</tr>
</thead>
<tbody>
<?php for($i=$start;$i<$end;$i++)
{
$add=$ARRAY[$i]['source_address'];
$Status=$ARRAY[$i]['Status'];
$total=$add.$Status;
?>
<tr>
<td><input type="radio" name="ID[]" value="<?php echo $total; ?>"/></td>
<?php
echo'<td>'.$ARRAY[$i]['escl_date'].'</td>';
echo'<td>'.$ARRAY[$i]['escl_status'].'</td>';
?>
</tr>
<?php }?>
</tbody>
</table>
<input type="submit" name="details" value="details" />
How Can I make the radio button selected even after clicking on submit button? Please help me.
You can try this, You need a condition to check with reference value and add checked attribute.
Here your reference value is your POST of ID value. Add this into your radio tag <?php echo $_POST['ID'][0]==$total ? 'checked':'';?>
<input type="radio" name="ID[]" value="<?php echo $total; ?>" <?php echo $_POST['ID'][0]==$total ? 'checked':'';?> />
The correct HTML attribute you are looking for is checked.
Note to other answer: readonly is not necessary, nor is it what OP is asking for.
Since you're doing it in PHP, let's say you're passing something with an HTML markup name of foobar, as follows:
<form><input type="radio" name="foobar"></form>
To make sure that the value is retained even after it is submitted, you could add a code like this (get or post depending on your purpose):
<form><input type="radio" name="foobar" <?php if(isset($_GET[foobar])){ echo "checked"; ?>></form>
This is to make sure that it would be checked if the name was passed in the form.
Note that this is just an example.
Try this, it works:
<input type="radio" checked>

Check if the radio button is pushed

I have a form that has a foreach loop and inside the loop I have an input type radio, so every time it has a new value like the codes below:
<?php
$aircrafts = AircraftPermit::getaircraft();
foreach($aircrafts as $aircraft)
{
$pacas = AircraftPermit::getrouteaircraft($aircraft->name ,$pilotid);
if($pacas)
{
?>
<tr>
<td>
<input type="radio" id="ac" value="" disabled="disabled"><?php echo $aircraft->name ;?><br />
</td>
<td align="center">
<input name="submit" title="Give permission to selected aircraft" type="submit" value="Give Permit" disabled="disabled">
</td>
<td align="center">
<font color="green">Granted</font>
</td>
</tr>
<?php
}
else
{
?>
<tr>
<td>
<input type="radio" id="ac" value="<?php echo $aircraft->name ;?>"><?php echo $aircraft->name ;?><br />
</td>
<td align="center">
<input name="id" type="hidden" value="<?php echo $pilotid ;?>">
<input name="submit" title="Give permission to selected aircraft" type="submit" value="Give Permit">
</td>
<td align="center">
<font color="red">Restricted</font>
</td>
</tr>
<?php
}
}
?>
Now at the end I have a script to check if the radio button is selected like below:
<script type="text/javascript">
function radio_is_on() {
var elementId = document.getElementById('ac');
if (elementId.checked==false)
{
alert('Please select the aircraft first!');
return false;
}
if (elementId.value)
{
return true;
}
}
</script>
When the radio button is not pushed the message pops up okay but when it's pushed it returns no value and the form send null value. Please tell me where I'm wrong.
Thanks
You cannot have multiple id tags, which is probably why it's not working. Try changing it to a class instead. See below for example in jquery.
HTML:
<input type="radio" class="ac" value="<?php echo $aircraft->name ;?>"><?php echo $aircraft->name ;?><br />
JAVASCRIPT:
function radio_is_on() {
if ($('.ac:checked').val()) {
return true;
}
else {
alert('Please select the aircraft first!');
return false;
}
}
You have same id for all radio buttons - that is not correct.
And only one button is considered all the time.
You can number them, eg. ac1, ac2 and so on.
as far as I can remember a radio button is "pushed" when it has the checked attribute, and it is not pushed when it doesn't have it.
...
if (elementId.hasOwnProperty('checked'))
...
instead of
...
if (elementId.checked==false)
...

Jquery and PHP sessions

I'm learning jquery as I code and I need to do something with jquery related to sessions. Code goes like this for html:
<table class="match_table">
<tr id="10"><td class="timez">20:45</td><td>Valencia</td><td>:</td><td>Fulham</td></tr>
<tr id="11"><td class="timez">20:45</td><td>Crvena Zvezda </td><td>:</td><td>Rad</td></tr>
<tr id="12"><td class="timez">20:45</td><td>Vojvodina </td><td>:</td><td>Elche</td></tr>
<tr id="13"><td class="timez">20:45</td><td>Chelsea </td><td>:</td><td>Tottenham</td></tr>
</table>
<div id="triping">
<form id="triping_form">
<table class="popz">
<tr>
<td>1</td><td><input type="radio" name="opklada" value="1" /></td>
<td>X</td><td><input type="radio" name="opklada" value="2" /></td>
<td>2</td><td><input type="radio" name="opklada" value="3" /></td>
</tr>
<tr>
<td>0-2</td><td><input type="radio" name="opklada" value="4" /></td>
<td>3+</td><td><input type="radio" name="opklada" value="5" /></td>
<td>4+</td><td><input type="radio" name="opklada" value="6" /></td>
</tr>
</table>
<span id="toje" style="display:none"><img src="success.png" /></span>
<input type="hidden" name="match_id" id="hidin" />
<input type="submit" class="button-class" id="grauzame" />
</form>
</div>
This is jquery:
<script type="text/javascript">
var current_id;
$('.match_table tr').on('click', function() {
$("#triping").slideUp(300);
$("#triping").slideDown(300);
current_id = $(this).attr('id');
$("#hidin").val(current_id);
$("#toje").hide();
$("#grauzame").show();
});
$("form#triping_form").on("submit", function() {
$.post("ajax.php", { match_id: $("#hidin").val() }, function(rez) {
if(rez == 'ok') {
$("#grauzame").hide();
$("#toje").fadeIn(500);
}
});
return false;
});
</script>
Now, I will now just simulate user login with opening sessions:
session_start();
$_SESSION['user_id'] = 12;
$_SESSION['username'] = 'eston';
I know jquery is not nice, but it works for now. I will improve it later. But the point is, logged in user can click on submit button and he will get confirmation image.
I need to prevent send message to guests, like 'You must be logged in to submit' when he clicks on submit. How to make this work together with sessions and what's the best way? I would probably figure it out but it's better to see what are the best practices. But for now any solution is welcome :)
Simply check whether $_SESSION['user_id'] exists or not. If it's an AJAX request, I usually return json with at least one field named result. It could be anything your jquery code can use, for example 'success' and 'fail'.

Categories