show number of radio button checked instantaneous - javascript

I have radio buttons with name as 2d array in a while loop.I want to show the number of radio buttons checked when its clicked.
my radio buttons
$n=0;
while($row= mysqli_fetch_row($rs)){?>
<form name="myfm" id="myfm" method="post" action="Quizn.php">
<table width=100%> <tr> <td width=30><td></td></td></tr> <table border=0>
<?php $n=$n+1; ?>
<tr><td>Question <?php echo $n.") "; echo $row[2]; ?></td></tr>
<tr><td class=style8>A. <input type="radio" name="ques['<?php echo $n; ?>'][]" value=1><?php echo $row[3]; ?></td></tr>
<tr><td class=style8>B. <input type="radio" name="ques['<?php echo $n; ?>'][]" value=2><?php echo $row[4];?></td></tr>
<tr><td class=style8>C. <input type="radio" name="ques['<?php echo $n; ?>'][]" value=3><?php echo $row[5];?></td></tr>
<tr><td class=style8>D. <input type="radio" name="ques['<?php echo $n; ?>'][]" value=4><?php echo $row[6];?></td></tr>
<input type="hidden" name="ques['<?php echo $n; ?>'][]" value="0" />
<input type="hidden" value="<?php echo $row[0]; ?>" name="qid[]" />
<?php }
echo "<tr><td><input type=submit name=submit id='result' value='Get Result'></form>";
?>
</table></table>
</form>
<p>Clicked:</p> <p id="clicked">0</p>
javascript
$("input:radio").click(function () {
var totalRd = $('table').find(':not(.pend) > input:radio:checked').length;
$("#totalRd .rd-count").html(totalRd);
});
Please help me if you know the solution.
Thankyou

With some assumption, try like this
<?php
if(isset($_POST['submit'])){
echo "<pre>";
print_r($_POST);
echo "</pre>";
}
?>
<form method="post" action="">
<?php for ($i=0; $i < 3 ; $i++) { ?>
<br>
A<input type="radio" name="ques[<?php echo $i;?>][]" value=1>
B<input type="radio" name="ques[<?php echo $i;?>][]" value=2>
C<input type="radio" name="ques[<?php echo $i;?>][]" value=3>
D<input type="radio" name="ques[<?php echo $i;?>][]" value=4>
<?php } ?>
<br>
<input type="submit" name="submit" value="submit">
</form>
http://screencast.com/t/HEVSPR7yVR
http://screencast.com/t/rQSpXiJAMg
I suggest don't use array here name="ques[<?php echo $i;?>][]" just use name="ques[<?php echo $i;?>]" for clean and clear

Try this
$(":radio").on( 'click', function() {
count = $(':radio:checked').length;
alert(count);
});

Related

Get the unique total scores of students

I have a form on my school web application where teachers are able to add scores for students and at the end of each row, the total, which is the sum of input values is automatically calculated per student.
The problem is that it output the total of all the students instead of the total of individual students on all the rows even ones with no scores added.
How do I get the unique total value for each row?
Below is my code
<?php if($class_id >= 1 && $class_id <= 4 && $student['is_secmid'] == 1){?>
<form method="post" action="<?php echo site_url('admin/examgroup/entrymarks') ?>" id="assign_form1111">
<input type="hidden" id="max_mark" value="<?php echo $subject_detail->max_marks; ?>">
<?php
if (isset($resultlist) && !empty($resultlist)) {
?>
<div class="row">
<div class="col-md-12">
<input type="hidden" name="exam_group_class_batch_exam_subject_id" value="<?php echo $exam_group_class_batch_exam_subject_id; ?>">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>C.A 1 (10.0)</th>
<th>C.A 2 (10.0)</th>
</tr>
</thead>
<tbody>
<?php if (empty($resultlist)) {
?>
<tr>
<td colspan="7" class="text-danger text-center"><?php echo $this->lang->line('no_record_found'); ?></td>
</tr>
<?php
} else {
foreach ($resultlist as $student) {
?>
<tr class="std_adm_<?php echo $student['admission_no']; ?>">
<input type="hidden" name="prev_id[<?php echo $student['exam_group_class_batch_exam_students_id'] ?>]" value="<?php echo $student['exam_group_exam_result_id'] ?>">
<input type="hidden" name="exam_group_student_id[]" value="<?php echo $student['exam_group_class_batch_exam_students_id'] ?>">
<td><?php echo $student['admission_no']; ?></td>
<td style="white-space: nowrap;"><?php echo $student['lastname'] . " " . $student['firstname']; ?></td>
<td> <input type="number" class="marksssss2 form-control" min="0" max="10" name="exam_group_student_ca1_<?php echo $student['exam_group_class_batch_exam_students_id']; ?>" value="<?php echo $student['exam_group_exam_result_get_ca1']; ?>" step="any"></td>
<td> <input type="number" class="marksssss3 form-control" min="0" max="10" name="exam_group_student_ca2_<?php echo $student['exam_group_class_batch_exam_students_id']; ?>" value="<?php echo $student['exam_group_exam_result_get_ca2']; ?>" step="any"></td>
<td> <output class="result"></output></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
<?php if ($this->rbac->hasPrivilege('exam_marks', 'can_edit')) { ?>
<button type="submit" class="allot-fees btn btn-primary btn-sm pull-right" id="load" data-loading-text="<i class='fa fa-spinner fa-spin '></i> Please Wait.."><?php echo $this->lang->line('save'); ?>
</button>
<?php } ?>
</div>
</div>
<?php } ?>
<script>
const $inputs = $('input[type="number"]')
$inputs.change(function() {
var total = 0;
var parent = $(this).closest('.row');
parent.find('input[type="number"]').each(function() {
if ($(this).val() != '') {
total += parseInt($(this).val());
}
});
parent.find('.result').html(total);
});
</script>
you missed class row on <tr> and you are trying to find it on closest div from javascript, and you are getting this (<div class="row">), it have all the (<input type="number").
you forget to add class to TR. Add class row on <tr> so you can get all (<input type="number") inside the tr.
because of not added class on TR you are facing issue that all total have same numbers.

When I click the update button, it won't change/update (CodeIgniter, Update Multiple Rows)

This is my controller:
function update_agenda() {
$id = $this->input->post['did'];
$this->load->model('agenda_model');
$data = array(
'nama' => $this->input->post('did'),
'keterangan' => $this->input->post('dketer')
);
($this->agenda_model->update($id, $data))
}
When I click button Save, it won't change anything. How can I fix this problem?
<td>
<input type ="checkbox" id="haha" class=checkbox1 name="checklist" value=<?php echo $agenda->id; ?>> <?php echo $agenda->id; ?>
</td>
<td>
<input type="text" id="hide" name="did" value="<?php echo $agenda->id; ?>">
</td>
<td>
<input type="text" name="dnama" id="nama_" value="<?php echo $agenda->nama; ?>" disabled />
</td>
<td>
<input type="text" name="dketer" id="ket_" value="<?php echo $agenda->keterangan; ?>" disabled>
</td>
<?php }?>
</td>
</tr>
</table>
<button onclick="saveUpdate()"> Save </button>
This is my model:
function update ($id,$data){
$this->db->where('id', $id);
$this->db->update('agenda', $data);
}
Here is my saveUpdate function
i hope this helpyou
view.php
<td>
<input type ="checkbox" id="haha" class=checkbox1 name="checklist" value=<?php echo $agenda->id; ?>> <?php echo $agenda->id; ?>
</td>
<td>
<input type="text" id="hide" name="did" value="<?php echo $agenda->id; ?>">
</td>
<td>
<input type="text" name="dnama" id="nama_" value="<?php echo $agenda->nama; ?>" disabled />
</td>
<td>
<input type="text" name="dketer" id="ket_" value="<?php echo $agenda->keterangan; ?>" disabled>
</td>
<?php }?>
</td>
</tr>
</table>
<button onclick="saveUpdate()"> Save </button>
controller.php
function update_agenda() {
$this->load->model('agenda_model');
$this->form_validation->set_rules('did', 'Did', 'trim|required|xss_clean');
$this->form_validation->set_rules('dketer', 'Dketer', 'trim|required|xss_clean');
if ($this->form_validation->run() === FALSE){
echo 'ERROR';
}
else{
$id = $this->input->post['did'];
$data = array(
'nama' => $this->input->post('did'),
'keterangan' => $this->input->post('dketer')
);
$this->agenda_model->update($id, $data);
$data['agenda'] = $this->agenda_model->get_data($id);
$this->load->view('formsuccess', $data);
}
}
model.php
function update($id, $data){
$this->db->where('id', $id);
$this->db->update('table', $data);
}
function get_data($id){
$this->db->where('id', $id);
return $this->db->get('table')->row();
}

How to check if all the radio button gropus in a division are checked or not?

I am weighting a code where user will select their answer for questions they are asked.
There would be multiple questions per page. And I want all those questions to be answered.
Below is the code of the questions form where questions are being called dynamically from database in a loop.
<form id="radio-demo" class="radio-demo" action="<?php echo get_site_url(); ?>/register-response" method="POST">
<?php
/* print_r($categories); */
/*loop to get questions for each categories*/
foreach($cat as $categories){
$software = $wpdb->get_results("SELECT * FROM ". $wpdb->prefix ."statement where statement_category_id =".$categories->statement_category_id);
$j++;
$table = $software;
$c_id = $categories->statement_category_id;
$i = 0;
?>
<div class="custom_hide" id="page_<?php echo $j;?>">
<?php
$cat_name = $categories->statement_category;
/*loop for the number of questions per category */
foreach($table as $software) {
$i++;
?>
<div class="question_one">
<p><?php echo $i.". ".$software->statement;?></p>
<label class="radio-inline">
<input type="radio" name="<?php echo $c_id.'_'.$i;?>" id="first-choice-<?php echo $i;?>" value="<?php echo 1*$software->weight;?>" required = "required"/>
<label for="first-choice">1</label>
</label>
<label class="radio-inline">
<input type="radio" name="<?php echo $c_id.'_'.$i;?>" id="second-choice-<?php echo $i;?>" value="<?php echo 2*$software->weight;?>" required = "required"/>
<label for="second-choice">2</label>
</label>
<label class="radio-inline">
<input type="radio" name="<?php echo $c_id.'_'.$i;?>" id="third-choice-<?php echo $i;?>" value="<?php echo 3*$software->weight;?>" required = "required"/>
<label for="third-choice">3</label>
</label>
<label class="radio-inline">
<input type="radio" name="<?php echo $c_id.'_'.$i;?>" id="four-choice-<?php echo $i;?>" value="<?php echo 4*$software->weight;?>" required = "required"/>
<label for="four-choice">4</label>
</label>
<label class="radio-inline">
<input type="radio" name="<?php echo $c_id.'_'.$i;?>" id="five-choice-<?php echo $i;?>" value="<?php echo 5*$software->weight;?>" required = "required"/>
<label for="five-choice">5</label>
</label>
<label class="radio-inline">
<input type="radio" name="<?php echo $c_id.'_'.$i;?>" id="six-choice-<?php echo $i;?>" value="<?php echo 6*$software->weight;?>" required = "required"/>
<label for="six-choice">6</label>
</label>
<label class="radio-inline">
<input type="radio" name="<?php echo $c_id.'_'.$i;?>" id="seven-choice-<?php echo $i;?>" value="<?php echo 7*$software->weight;?>" required = "required"/>
<label for="seven-choice">7</label>
</label>
<label class="radio-inline">
<input type="radio" name="<?php echo $c_id.'_'.$i;?>" id="eight-choice-<?php echo $i;?>" value="<?php echo 8*$software->weight;?>" required = "required"/>
<label for="eight-choice">8</label>
</label>
<label class="radio-inline">
<input type="radio" name="<?php echo $c_id.'_'.$i;?>" id="nine-choice-<?php echo $i;?>" value="<?php echo 9*$software->weight;?>" required = "required"/>
<label for="nine-choice">9</label>
</label>
<label class="radio-inline">
<input type="radio" name="<?php echo $c_id.'_'.$i;?>" id="ten-choice-<?php echo $i;?>" value="<?php echo 10*$software->weight;?>" required = "required"/>
<label for="ten-choice">10</label>
</label>
<label class="radio-inline">
<input type="radio" name="<?php echo $c_id.'_'.$i;?>" id="eleven-choice-<?php echo $i;?>" value="<?php echo 0*$software->weight;?>" required = "required"/>
<label for="eleven-choice">No Opinion</label>
</label>
<!-- <input type="hidden" name="currentID" value="<?php echo $c_id; ?>" />-->
</div> <?php }?>
<div class="bottum_btn">
<div class="">
<?php if($j > 1){?>
<div class="back_btn">
<a onclick="custom_back(<?php echo $j-1;?>)"><i class="fa fa-chevron-circle-left">Back</i> </a>
</div>
<?php
}
if($j == $catcount){?>
<div class="next_btn">
<input name="submit" type="submit" id="submit_btn" value="submit""><i class="fa fa-chevron-circle-right"></i></input>
</div>
<!--<a name="submit" id="submit_btn" onClick="PopUp()">submit<i class="fa fa-chevron-circle-right"></i>
</a>-->
<?php } else
{
?>
<div class="next_btn">
<a onclick="custom_next(<?php echo $j+1;?>)">Next<i class="fa fa-chevron-circle-right"></i> </a>
</div><?php
}?>
</div>
</div>
</div>
<?php }?>
The script I am trying is as below.
function custom_next(val) {
var val1 = "#page_" + val;
var val2 = "#page_" + (val - 1);
var questions = jQuery(".question_one");
var maindiv = jQuery(val2);
if (jQuery(val2+':not(:has(:radio:checked))').length > 1) {
console.log(jQuery(val2+':not(:has(:radio:checked))').length);
alert("At least one group is blank");
}else{
console.log(jQuery(val2+':not(:has(:radio:checked))').length);
jQuery(val1).removeClass("custom_hide");
jQuery(val1).addClass("custom_show");
jQuery(val2).removeClass("custom_show");
jQuery(val2).addClass("custom_hide");
}}
But this script is checking for "if any of the radio button group is checked or not" where I want all of the radio button groups to be checked.
I have analyzed your code and I have developed this code for your script. This scripts checks if any of your question options is checked or not.
function custom_next(val) {
var val1 = "#page_" + val;
var val2 = "#page_" + (val - 1);
var i = 0;
var j = 0;
jQuery('div.custom_show input[type=radio]').each(function(){
var status = jQuery(this).is(':checked');
//alert(status);
i=i+1;
if(status == true){
j=j+1;
}
});
i= i/11;
if(i == j){
jQuery(val1).removeClass("custom_hide");
jQuery(val1).addClass("custom_show");
jQuery(val2).removeClass("custom_show");
jQuery(val2).addClass("custom_hide");
}else{
alert("All Questions are mandetory!!");
}
}
First find all the radio button groups inside your form
var radio_groups = [];
$("#radio-demo :radio").each(function(){
radio_groups.push(this.name);
});
Now iterate through all of them to see if any one is not selected
var isAnyUnselected = radio_groups.filter( function(vallue){
return $(":radio[name='"+group+"']:checked").length == 0
} ).length > 0;
check if isAnyUnselected is true (some are unselected) or false (all of them are selected)
But this script is checking for "if any of the radio button group is checked or not" where I want all of the radio button groups to be checked.
What you need to do is check if there is at least one radio box that is not checked. Small example:
<input class="ans" type="radio"/>
<input class="ans" type="radio"/>
if ($('.ans').not(':checked').length)
{
console.log('At least one not checked, please check all');
}
else
{
console.log('All checked');
}
If you donĀ“t want to change the html you can use the selector jQuery("input[type='radio']:not(:checked)") if the .length of this selector is greater than 1 there are some input to check. Simple example here se the console logs.
Otherwise you could use the required attribute link1 link2.

"if" condition variable in PHP

I am building a quiz app, working fine now decided to add "boosters" (help tools for the player), I've already posted a question here about adding extra time to the timer, I am still digging to find a solution, but no luck til now.. Anyway, this is not my question now.
I started to work on the second booster: Successfully skip a question (without losing points)
So the original code (before adding the boost part) was like this:
<div id='question<?php echo $i;?>' class='cont'>
<p class='questions' id="qname<?php echo $i;?>">
<?php echo $i?>.<?php echo $result['question_name'];?></p>
<input type="radio" value="1" id='radio1_<?php echo $result['id'];?>'
name='<?php echo $result['id'];?>'/>
<?php echo $result['answer1'];?><br/>
<input type="radio" value="2" id='radio1_<?php echo $result['id'];?>'
name='<?php echo $result['id'];?>'/>
<?php echo $result['answer2'];?><br/>
<input type="radio" value="3" id='radio1_<?php echo $result['id'];?>'
name='<?php echo $result['id'];?>'/>
<?php echo $result['answer3'];?><br/>
<input type="radio" value="4" id='radio1_<?php echo $result['id'];?>'
name='<?php echo $result['id'];?>'/>
<?php echo $result['answer4'];?><br/>
<input type="radio" checked='checked' style='display:none' value="5"
id='radio1_<?php echo $result['id'];?>'
name='<?php echo $result['id'];?>'/><br/>
<button id='<?php echo $i;?>' class='next btn btn-success' type='button' >Next</button>
And this is how the points are count:
if(isset($_POST[1])){
$keys=array_keys($_POST);
$order=join(",",$keys);
//$query="select * from questions id IN($order) ORDER BY FIELD(id,$order)";
// echo $query;exit;
$response=mysql_query("select id,answer from questions where id IN($order) ORDER BY FIELD(id,$order)") or die(mysql_error());
$right_answer=0;
$wrong_answer=0;
$unanswered=0;
while($result=mysql_fetch_array($response)){
if($result['answer']==$_POST[$result['id']]){
$right_answer++;
}else if($_POST[$result['id']]==5){
$unanswered++;
}
else{
$wrong_answer++;
}
}
So my idea was about adding a button called "Success skipe"
<button id='<?php echo $success;?>' class='skip btn btn-success' type='button'>Success Skip</button>
And while counting the right answers I tried to make a condition that once this button is clicked the answer is considered correct so I added this:
while($result=mysql_fetch_array($response)){
if($success){ //Just added
$right_answer++; //this "if" condition
}
But it didn't work, what's wrong?

Jquery disable child element of the same parent

I don't want to copy any of the questions asked before, but I have hard time to solve my problem.
I have a form with few checkbox-es, some of them are hidden to trigger output when checkbox is unchecked. At the end of the page I have JQuery code, that should disable those hidden checkboxes if I enable general boxes, but this code doesn't work. Whenever I click to any checkbox, EVERY other checkboxes become unchecked! I think it happens because I cant aim to the only one needed element.
Maybe you didn't understand, but if you look at the code, you will find out exactly what is wrong!
<form action="" method="GET" name="<?php echo $listFilter['forn_name']; ?>" id="<?php echo $listFilter['forn_name']; ?>">
<span>
<input type="checkbox" value="1" name="word" id="word_checkbox" class="checkbox" <?php if($listFilter['word_status'] == 1){ ?> checked <?php } ?>/>
<input type='hidden' value='0' name='word' class="checkbox_hidden" <?php if($listFilter['word_status'] == 0){ ?> checked <?php } ?>/>
<label class="label" for="word_checkbox">Words</label>
</span>
<span>
<input type="checkbox" value="1" name="pron" id="pron_checkbox" class="checkbox" <?php if($listFilter['pronunciation_status'] == 1){ ?> checked <?php } ?> />
<input type='hidden' value='0' name='pron' class="checkbox_hidden" <?php if($listFilter['pronunciation_status'] == 0){ ?> checked <?php } ?>/>
<label class="label" for="pron_checkbox">Pronunciation</label>
</span>
<span>
<input type="checkbox" value="1" name="pos" id="pos_checkbox" class="checkbox" <?php if($listFilter['pos_status'] == 1){ ?> checked <?php } ?> />
<input type='hidden' value='0' name='pos' class="checkbox_hidden" <?php if($listFilter['pos_status'] == 0){ ?> checked <?php } ?>/>
<label class="label" for="pos_checkbox">Part of speech</label>
</span>
<span>
<input type="checkbox" value="1" name="meaning" id="meaning_checkbox" class="checkbox" <?php if($listFilter['meaning_status'] == 1){ ?> checked <?php } ?> />
<input type='hidden' value='0' name='meaning' class="checkbox_hidden" <?php if($listFilter['meaning_status'] == 0){ ?> checked <?php } ?>/>
<label class="label" for="meaning_checkbox">Meaning</label>
</span>
<span>
<input type="checkbox" value="1" name="examples" id="examples_checkbox" class="checkbox" <?php if($listFilter['examples_status'] == 1){ ?> checked <?php } ?> />
<input type='hidden' value='0' name='examples' class="checkbox_hidden" <?php if($listFilter['examples_status'] == 0){ ?> checked <?php } ?>/>
<label class="label" for="examples_checkbox">Examples</label>
</span>
<script>
$(document).ready(function(){
$("#<?php echo $listFilter['forn_name']; ?> .checkbox").change(function() {
/* I reckon those codes bellow are causing problem */
var parent = $(this).closest('span');
if(this.checked){$(this).parent("span").children(".checkbox_hidden").prop('disabled', true);}
document.forms.<?php echo $listFilter['forn_name']; ?>.submit();
});
});
</script>
</form>
Thanks to Alexander, there is a sample code on JSFiddle. http://jsfiddle.net/TH2hh/

Categories