Enable/Disable Submit Button based on radio buttons - php

I have a form layed out like this:
<form action="join-head-2-head.php" method="POST" enctype="application/x-www-form-urlencoded">
<table border="0" cellspacing="4" cellpadding="4">
<tr>
<td colspan="2"><input name="player1rules" type="radio" id="tandcy" value="y" />
<label for="tandcy">I Have Reviewed The Rules And The Terms & Conditions And Agree To Abide By Them</label></td>
</tr>
<tr>
<td colspan="2"><input name="player1rules" type="radio" id="tandcn" value="n" checked="checked" /><label for="tandcn">I Do Not Agree To The Terms And Condtions And/Or Have Not Read The Rules</label></td>
</tr>
<tr>
<td width="100"><input name="player1" type="hidden" value="<? $session->username; ?>" /></td>
<td><input type="submit" name="join" id="join" value="Take Available Slot" /></td>
</tr>
</table>
</form>
What I am hoping to do is disable the submit button if id="tandcn" is selected, and enable it when id="tandcy". Is there an easy way to do that using javascript?

example http://jsfiddle.net/sWLDf/
$(function () {
var $join = $("input[name=join]");
var processJoin = function (element) {
if(element.id == "tandcn") {
$join.attr("disabled", "disabled");
}
else {
$join.removeAttr("disabled")
}
};
$(":radio[name=player1rules]").click(function () {
processJoin(this);
}).filter(":checked").each(function () {
processJoin(this);
});
});

$(":radio[name='player1rules']").click(function() {
var value = $(this).val();
if (value === "n") {
$("#join").attr("disabled", "disabled");
return;
}
$("#join").removeAttr("disabled");
});
Example on jsfiddle

Lots answers based on jquery (which I recommended to use). Here your form with javascript
<script type="text/javascript">
function disable(id){
document.getElementById(id).disabled = 'disabled';
}
function enable(id){
document.getElementById(id).disabled = '';
}
</script>
<form action="join-head-2-head.php" method="POST" enctype="application/x-www-form-urlencoded">
<table border="0" cellspacing="4" cellpadding="4">
<tr>
<td colspan="2"><input name="player1rules" type="radio" id="tandcy" value="y" onclick='enable("join")' />
<label for="tandcy">I Have Reviewed The Rules And The Terms & Conditions And Agree To Abide By Them</label></td>
</tr>
<tr>
<td colspan="2"><input name="player1rules" onclick='disable("join")' type="radio" id="tandcn" value="n" checked="checked" /><label for="tandcn">I Do Not Agree To The Terms And Condtions And/Or Have Not Read The Rules</label></td>
</tr>
<tr>
<td width="100"><input name="player1" type="hidden" value="<? $session->username; ?>" /></td>
<td><input type="submit" DISABLED name="join" id="join" value="Take Available Slot" /></td>
</tr>
</table>
</form>
However more elegant way is use jquery.

$(document).ready(function(){
if($('#tandcy').is(':checked')){
$('#join').attr('disabled','disabled');
}
if($('#tandcn').is(':checked')){
$('#join').removeAttr('disabled','disabled');
}
$('#tandcn').click(function(){
$('#join').attr('disabled','disabled');
});
$('#tandcy').click(function(){
$('#join').removeAttr('disabled','disabled');
})
});
Try this....
you need jquery for this,....

$("#tandcn #tandcy").livequery('change', function(event){
if ($('#tandcn').is(":checked"))
{
$('#join').hide();
//or
$('#join').attr("disabled", false);
}
else($('#tandcy').is(":checked"))
{
$('#join').show();
//or
$('#join').attr("disabled", false);
}
});

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

Jquery empty field add message beside the field

Jquery:
$(document).ready(function(){
$("#login_form").submit(function(event){
var username = $.trim($("#username").val());
var password = $.trim($("#password").val());
if(username.length < 1){
if(!$("#user_error").length){
$("#username_error").append("<p id='user_error'>Required</p>");
}
}
if(username.length < 1|| password.length < 1){
$("#username_error").append("<p id='user_error'>Required</p>");
$("#password_error").append("<p id='pass_error'>Required</p>");
event.preventDefault();
}
});
});
View:
<? $attr = array( "id" => "login_form"); echo form_open("KGLogin/verify_login",$attr); ?>
<table id="loginform">
<tr>
<th>Log-in</th>
</tr>
<tr>
<td>
<input type="text" name="username" id='username' placeholder="Username" />
</td>
<td id="username_error"></td>
</tr>
<tr>
<td>
<input type="password" name="password" id='password' placeholder="Password" />
</td>
<td id="password_error"></td>
</tr>
<tr>
<td>
<input type="submit" name="login" value="Login" />
</td>
</tr>
</table>
<? echo form_close(); ?>
Is there a simpler way to do this? IF i submit it twice without inputting anything, it will continue to append. Im a newbie to jquery, Cant seem to find a simple way to do things, all search results in google directs me to complicated jqueries.
How do i keep the function from stacking the error message.
Ill just put this as an answer, so no one would suggest.
<?if(form_error('username'))echo "required";;?>
Put the following code in autoload file.
$autoload['helper'] = array('url','array','html','form');
Then try this in view file
<?php if (form_error('your field name'))
{
echo "required";
}
;?>

Check Box AutoSum with Javascript & PHP

I wanted to put a checkbox autosum on my website so that anyone who check a box and submit, the values (Points) of those check box are automatically added to my total sum (on home page actually)
I have found this code so far for java script.
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td> <input name="sum_m_1" value="25" type="checkbox" id="sum_m_1" onclick="UpdateCost()"> </td>
<td> ResponseOption_1 </td>
</tr>
<tr>
<td> <input name="sum_m_2" value="15" type="checkbox" id="sum_m_2" onclick="UpdateCost()"> </td>
<td> ResponseOption_2 </td>
</tr>
</table>
<input type="text" id="totalcost" value="">
With JAVASCRIPT:
<script type="text/javascript">
function UpdateCost() {
var sum = 0;
var gn, elem;
for (i=1; i<3; i++) {
gn = 'sum_m_'+i;
elem = document.getElementById(gn);
if (elem.checked == true) { sum += Number(elem.value); }
}
document.getElementById('totalcost' ).value = sum.toFixed(2);
}
window.onload=UpdateCost
</script>
This will add up the values but i wanted to add by checking check boxes --> Submit --> collect the data --> add to my main total.
Any helps would be much appreciated.
Simple changes:
Add a button "calculate cost".
Removed the onclick events from the checkboxes and added the onclick event to the button instead.
Note that "window.onload=UpdateCost" is still there because what you want ideally is for the total cost to be displayed properly as 0.00 when the page loads.
HTML
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td> <input name="sum_m_1" value="25" type="checkbox" id="sum_m_1"> </td>
<td> ResponseOption_1 </td>
</tr>
<tr>
<td> <input name="sum_m_2" value="15" type="checkbox" id="sum_m_2"> </td>
<td> ResponseOption_2 </td>
</tr>
</table>
<input type="text" id="totalcost" value="">
<input type="button" value="update cost" onclick="UpdateCost();">
JavaScript
<script type="text/javascript">
function UpdateCost() {
var sum = 0;
var gn, elem;
for (i=1; i<3; i++) {
gn = 'sum_m_'+i;
elem = document.getElementById(gn);
if (elem. checked == true) { sum += Number(elem. value); }
}
document.getElementById('totalcost' ).value = sum.toFixed(2);
}
window.onload=UpdateCost
</script>
Hope that's the kind of thing you wanted, let me me know what you think.

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