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)
...
Related
I have two submits in the same form. One to filter the mySQL db and generate a list of checkboxes (works fine) and the other to display the values of the selected items using PHP (no go). Later on, I'll store these values to built a sql query. Please visit https://wintoweb.com/sandbox/question.php
I've tried everything you can imagine. Going nuts...
PHP function called from the [Show selected] button :
<?php
function getNames() {
//echo 'GOT IT !';
if(isset($_GET['choices[]'])){
if(!empty($_GET['choices'])){
foreach($_GET['choices'] as $selected){
echo $selected."</br>";
}
}
}
}
?>
Code for the [Show selected] button :
EXPECTED : the list of selected values below the
ACTUAL OUTPUT : no list of selected values. The PHP function getNames() is never traversed.
I simplified your form.
<form name="dig" action="#" method="get">
<input type="search" name="l_name" value="Author's name (whole or part)" style="width:200px;" onfocus="this.value=''" onblur="(this.value=='')? this.value='Author's name (whole or part)':this.value;" />
<input type="submit" name="search" value="Search">
<input type="submit" name="display_all" value="Show selected">
<br /><br />
<table style="width:400px">
<tr>
<td width="100%"><input type="checkbox" name="choices[]" value="ABÉLARD, Pierre et HÉLOÏSE" onclick="">ABÉLARD, Pierre et HÉLOÏSE</td>
</tr>
<tr>
<td width="100%"><input type="checkbox" name="choices[]" value="ABOUT, Edmond" onclick="">ABOUT, Edmond</td>
</tr>
<tr>
<td width="100%"><input type="checkbox" name="choices[]" value="ABRANTÈS, Laure Junot" onclick="">ABRANTÈS, Laure Junot</td>
</tr>
</table>
</form>
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
if(isset($_GET['search'])){
echo 'Search</br>';
} elseif(isset($_GET['display_all'])) {
echo getNames();
}
function getNames() {
$rets = '';
if(isset($_GET['choices']) and !empty($_GET['choices'])){
foreach($_GET['choices'] as $selected){
$rets .= $selected.'</br>';
}
}
return $rets;
}
I have a code which retrieves data from db and show it to a table. It is working fine but I want to implement a checkbox as part of the header which would allow me to select/deselect all rows. The header is just being pulled from db comments so I have no idea on how to implement the checkbox-select/deselect all header.
Here is my code,
<div>
<form action="test4.php" method="post"><?php
if(is_array($result)){
echo '
<fieldset>
<legend>Assign</legend>
<div>Changes will affect selected rows only.</div>
<table width=auto cellpadding=1px cellspacing=0px border=1 align=center>
<thead>
<tr>';
// column comment from DB as column header
foreach($result[0] as $key => $val){
echo '<th>'.$colcomments[$key].'</th>';
}
echo '
</tr>
</thead>
<tbody>';
foreach($result as $row => $info){
echo '
<tr>';
foreach($info as $key => $val){
if($key=='id'){
echo '
<td title="'.$colcomments[$key].'">'.$val.'.
<input type="checkbox" name="'.$key.'['.$info['id'].']"
value="'.$val.'" id="rowid_'.$val.'" />
<label for="rowid_'.$val.'"></label></td>';
}
else {
echo '
<td title="'.$colcomments[$key].'">
<input type="text" name="'.$key.'['.$info['id'].']"
value="'.$val.'" />
</td>';
}
}
echo '
</tr>';
}
echo '
</tbody>
</table>
</fieldset>';
}
?>
<fieldset>
<legend>Select Date</legend>
<div>Select Date from and Date to</div>
<input type="date" name="from" id="from" value="<?=$date['from']; ?>" />
<input type="date" name="to" id="to" value="<?=$date['to']; ?>" />
<div><input type="submit" value="Submit" /></div>
</fieldset>
</form>
</div>
This is the my script for the checkbox, and I dont know where I can insert a code to create a checkbox header. This is different from the ones posted previously as my issue is how I can create a checkbox header, if i only used the commments section in my db to make it as the header.
<script>
jQuery(document).ready(function () {
jQuery("input[name=checkall]").click(function () {
jQuery('input[name=checkall]').prop('checked', this.checked);
jQuery('input[name=checkbox]').prop('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
jQuery("input[name=checkbox]").click(function(){
if(jQuery("input[name=checkbox]").length == jQuery("input[name=checkbox]:checked").length) {
jQuery("input[name=checkall]").prop("checked", true);
} else {
jQuery("input[name=checkall]").prop("checked", false);
}
});
});
</script>
I'm marking this as resolved. Easy fix is just to set the type to hidden.
Change from
<input type="checkbox" name="'.$key.'['.$info['id'].']"
To
<input type="hidden" name="'.$key.'['.$info['id'].']"
Checkbox is just a redundant since I need to update all rows anyways.
I wrote this code (which posts to a PHP page) so that if guests do not input their name, it would pop out an alert box. However, Even when I put something in the name textbox and submit it, the alert box still pop out and it would submit the $_POST['gname'] to the server.
Here’s the JavaScript:
<script type="text/javascript">
function gSubmit()
{
if (document.getElementById("gname").value.length === 0)
{
alert("For completing the form, it requires you to input a Name");
return false;
}
else
{
document.getElementById("guestbook").innerHTML=("<p><b><center>Thank you for submitting the Guestbook");
window.location = "guestbook.php";
return true;
}
}
</script>
Here’s the HTML:
<form id="guestbook" name="guestbook" method="post" action="guestbook.php">
<center>
<table border="0px">
<tr>
<td>
<p align="right">Name :</p></td><td> <input type="text" name="gname" id="gname" />
</td>
</tr>
<tr>
<td>
<p align="right">E-mail :</p></td><td><input type="text" name="gemail" />
</td>
</tr>
<tr>
<td>
<p align="right">Telephone No :</p>
</td>
<td>
<input type="text" name="gtpn" />
</td>
</tr>
<tr>
<td>
<p align="right">Product Order / Comment / Messages :</p>
</td>
<td>
<textarea name="gtxtfld" id="gtxtfld" cols="32"></textarea>
</td>
</tr>
</table>
<br /><br />
<input type="submit" value="submit" name="submit" onclick="gSubmit();" />
<input type="reset" value="reset" name="reset" />
</form>
Change your gSubmit() function to this:
if (document.getElementById("gname").value === "")
{
alert("For completing the form, it requires you to input a Name");
return false;
}
else
{
document.getElementById("guestbook").innerHTML=("<p><b><center>Thank you for submitting the Guestbook");
return true;
}
In your html, change onclick="gsubmit()" to onclick="return gsubmit()".
Note that your message in the else clause will not be displayed because the form would have submitted by then.
Also, you have to compare the value of gname field, not it's innerHTML. And it has to be compared to empty string (""), not space (" ").
To check if the textbox has a value in it, you can use something like this:
if (document.getElementById("gname").value.length > 0)
{
alert("For completing the form, it requires you to input a Name");
//by returning false, you stop the submission from happening
return false;
}
else
{
document.getElementById("guestbook").innerHTML=("<p><b><center>Thank you for submitting the Guestbook");
return true; //allow the submission to go through
}
And in you're onclick event:
onclick="return gSubmit();"
For example, I have a number of fields a user could fill out but I want the PHP code to spit out something different depending on which radio button they select.
<html>
<body>
<form action="citation.php" method="post">
<table width="100%" border="0">
<tr>
<td>Name</td>
<td><label for="name2"></label>
<input type="text" name="name" id="name2"></td>
</tr>
<tr>
<td>Number</td>
<td><label for="number"></label>
<input type="text" name="number" id="number"></td>
</tr>
<tr>
<td>Email</td>
<td><label for="email"></label>
<input type="text" name="email" id="email"></td>
</tr>
<tr>
<td>Address</td>
<td><label for="address"></label>
<input type="text" name="address" id="address"></td>
</tr>
<tr>
<td>web</td>
<td><label for="web"></label>
<input type="text" name="web" id="web"></td>
</tr>
<tr>
<td><p>
<label>
<input type="radio" name="RadioGroup1" value="ex1" id="RadioGroup1_0">
ex1</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="ex2" id="RadioGroup1_1">
ex2</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="ex3" id="RadioGroup1_2">
ex3</label>
<br>
</p></td>
<td><input type="submit" name="click me" id="click me" value="Submit"></td>
</tr>
</table>
<p> </p>
</form>
</body>
</html>
So, if they choose the first radio button and hit submit, that would spit out something different than if you chose the 2nd radio button.
it's quite basic PHP
<?php
if($_POST['RadioGroup1']=='ex1'){
?>
You hit the first radio button, now he's hurt!
<?
}elseif($_POST['RadioGroup1']=='ex2'){
?>
You hurted the feelings of the second radio btn
<?
}elseif($_POST['RadioGroup1']=='ex3'){
?>
The tird radio button was clicked by you
<?
}
?>
or even nicer:
<?php
switch($_POST['RadioGroup1'){
case 'ext1':
?>
You hit the first radio button, now he's hurt!
<?
break;
case 'ext2':
?>
You hurted the feelings of the second radio btn
<?
break;
case 'ext3':
?>
The tird radio button was clicked by you
<?
break;
default: die('wrong')
}
}
?>
instead of pasteing HTML in th the PHP, you could also include files:
<?php
switch($_POST['RadioGroup1'){
case 'ext1':
include('form/1clicked.php')
break;
....
}
}
?>
When you do standard form submission, the name field of form fields automatically are passed to php as POST params, i.e., in $_POST. If a field has name="test" and value="valueTest", then at the php layer, you will get that value as $_POST['test']. Below is an example of how to handle your particular case:
<?php
switch ($_POST['RadioGroup1']) {
case 'ex1':
// handle ex1 case
break;
case 'ex2':
// handle ex2 case
break;
case 'ex3':
// handle ex3 case
break;
default:
// handle cases here where RadioGroup1 has invalid value
break;
}
?>
if(isset($_POST['firstradio']))
{
echo "Content for First Radio";
}
elseif(isset($_POST['secondradio']))
{
echo "Content for Second Radio";
}
else
{
echo "Something";
}
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.