how to send multiple radio button values via ajax to php - php

I have multiple radio buttons differentiated by their name, here is my script
<?php
if(mysqli_num_rows($result)>0){
while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['fullname'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['class'];?></td>
<td><input type="radio" value="present" name="<?php echo($row['id']); ?>" checked></td>
<td><input type="radio" value="absent" name="<?php echo($row['id']); ?>"></td>
</tr>
<?php }
}
?>
I want to pass their value to a php script, how I can pass each radio group value, Say if mysql query returned 10 rows then there are 10 radio button groups and I need to send all values of radio buttons.
And my ajax call is
$("#submitAttendance").click(function(){
$.ajax({
url: 'teacher.php',
method: 'post',
data:
});
});

You can use $(form_selector).serialize() that serializes all the values of the form and send to your server as an intended input.
$("#submitAttendance").click(function(){
$.ajax({
url: 'teacher.php',
method: 'post',
data: $(form_selector).serialize()
});
});
Also, make sure you write this code in:
$(form_selector).submit()
Rather than attaching an event handler to the submit button.
So, a typical output of your form, say like this:
<form action="">
<div>
<label><input type="radio" name="student-1" id="" value="present"> Present</label>
<label><input type="radio" name="student-1" id="" value="absent"> Absent</label>
</div>
<div>
<label><input type="radio" name="student-2" id="" value="present"> Present</label>
<label><input type="radio" name="student-2" id="" value="absent"> Absent</label>
</div>
<div>
<label><input type="radio" name="student-3" id="" value="present"> Present</label>
<label><input type="radio" name="student-3" id="" value="absent"> Absent</label>
</div>
<div>
<label><input type="radio" name="student-4" id="" value="present"> Present</label>
<label><input type="radio" name="student-4" id="" value="absent"> Absent</label>
</div>
<div>
<label><input type="radio" name="student-5" id="" value="present"> Present</label>
<label><input type="radio" name="student-5" id="" value="absent"> Absent</label>
</div>
</form>
Would be:
"student-1=absent&student-2=present&student-3=absent&student-4=present&student-5=absent"
Is this what you are looking for? And you can get this using PHP using:
$_POST["student-1"] // absent
$_POST["student-2"] // present
$_POST["student-3"] // absent
$_POST["student-4"] // present
$_POST["student-5"] // absent

Related

Send Multiple Radio Button Value to Controller Codeigniter

I have survey page containing multiple question and radio button for each question
<table class="table">
<form action="What should i write here" method="post">
<?php $numbering = 1;
foreach ($dataQuestion as $key => $value) : ?>
<tr>
<td width="1%"><?= $numbering++; ?></td>
<td><?= $value->question ?>
<div class="form-check mt-1">
<input class="form-check-input" type="radio" name="indikator[$value->id]" value="1">
Very Good
<br>
<input class="form-check-input" type="radio" name="indikator[$value->id]" value="2">
Good
<br>
<input class="form-check-input" type="radio" name="indikator[$value->id]" value="3">
Decent
<br>
<input class="form-check-input" type="radio" name="indikator[$value->id]" value="4">
Bad
<br>
<input class="form-check-input" type="radio" name="indikator[$value->id]" value="5">
Very Bad
</div>
</td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan="2"><button type="submit" class="btn btn-info btn-lg btn-block">Submit</button></td>
</tr>
</form>
I'm trying to pass selected radio button value to my controller file using <form action=""> but every question have different answer, how to send array of all answered question to my controller via <form action=""> ?
It seems everyone prefer to use javascript, url, and form_radio(). Any help would be appreciated, thank you.
<form action="What should i write here" ...>
Write there the route/path meant to receive your HTTP POST request. I.e:
<form action="/survey" method="post">
The state a route definition meant to receive the route in app\Config\Routes.php. I.e:
// ...
$routes->post("/survey", "SurveyController::create");

Radio buttons are not clickable

When executed, the radio buttons are not working, which they were when they were introduced without PHP.
Here is my code:
<td><?php echo date('l');?></td>
<?php
while ($meal_num<=4)
{
echo '<td>';
echo $row[$meal_num+2];
echo '<form action="rating.php?hostel='.$hostel.'&meal_type='.$meal_num.'" method="POST"><br><br>';
?>
<span class="radiolabel" >
<input type="radio" name="radio1" id="radio-1" value="1" />
<label for="radio-1">1</label>
<input type="radio" name="radio1" id="radio-2" value="2"/>
<label for="radio-2">2</label>
<input type="radio" name="radio1" id="radio-3" value="3"/>
<label for="radio-3">3</label>
<input type="radio" name="radio1" id="radio-4" value="4"/>
<label for="radio-4">4</label>
<input type="radio" name="radio1" id="radio-5" value="5"/>
<label for="radio-5">5</label>
</span>
<span> <input type="submit" class="submitbutton1" value="OK"></span>
</form>
</td>
<?php
$meal_num=$meal_num+1;
}?>
</tr>
</table>
Probably because your radio inputs are in a while loop, so you recreate multiple sets of radio inputs with the same set of ids.
Try attributing a different value for "id" for each loop that's executed with an increment number.
For example:
$i = 1;
while (blabla) {
echo '<input type="radio" id="radio-'.$i.'" name="radio1" />';
$i++;
}
So the radios from one loop will not interfere with the radios from another loop.
After each radio you put, you increment the number so the id will never be the same (an id should be unique in a page).

How to show checkbox selected in edit page if user select the checkbox on submit

I have a check box simple form with two fileds, on submit I am submitting the values yes or no in the database, now in edit I want to show that checkbox selected.
<div class="form-group col-md-6">
<label> Is Paid</label></br>
Yes <input type="checkbox"
name="paid"
value="yes"
value=" <?php echo $row_cat['is_paid']; ?>"
/>
No <input type='checkbox' name="paid" value="no" />
</div>
How can I show prefilled checkbox on edit page ?
Try
<div class="form-group col-md-6">
<label> Is Paid</label></br>
Yes <input type="checkbox"
name="paid"
value="yes"
<?php echo $row_cat['is_paid']=='yes'?' checked="checked" ':''; ?>
/>
No <input type='checkbox'
name="paid"
value="no"
<?php echo $row_cat['is_paid']=='no'?' checked="checked" ':''; ?>
/>
</div>

How to click radio button in table for handle multiple records?

I am creating a table of class attendance module in which teacher clicks on a radio button in front of student name to mark that if he/she is present,absent or on leave and when we submit the rows are saved in MySQL database.
This is how it looks like:
Problem:
when teacher clicks on radio button the first student attendance got marked and when teacher click on the second student's attendance it also get marked but the marked attendance of the first student get unmarked. The error is teacher can only mark one student attendance at a time but I want to add multiple students attendance in MySQL table .....
And this is my code
code:
<input type="hidden" name="class_id" value="<?php echo $row1['fk_class_id']; ?>" />
<input type="hidden" name="student_id" value="<?php echo $row1['student_id']; ?>" />
<tr>
<td>
<?php echo $row1['first_name'];?>
</td>
<td>
<?php echo $row1['last_name']; ?>
</td>
<td>
<?php echo $row1['gender']; ?>"
</td>
<td>
<input type="radio" name="att" value="P" />P
<input type="radio" name="att" value="A" />A
<input type="radio" name="att" value="L" />L
</td>
</tr>
Each row is required to be treated as independent value, thus you must have the form act in such a way.
<!-- row 1 -->
<input type="radio" name="row1" value="P" />P
<input type="radio" name="row1" value="A" />A
<input type="radio" name="row1" value="L" />L
<!-- row 2 -->
<input type="radio" name="row2" value="P" />P
<input type="radio" name="row2" value="A" />A
<input type="radio" name="row2" value="L" />L
The way that radio buttons behave with one another is through the name attribute.
Rename each group of att buttons based on per row.
you can put an array as the name with the student_id:
<input type="radio" name="att[<?php echo $row1['student_id']; ?>]" value="P" />P
<input type="radio" name="att[<?php echo $row1['student_id']; ?>]" value="A" />A
<input type="radio" name="att[<?php echo $row1['student_id']; ?>]" value="L" />L
the variable $_POST['att'] will be an array with student_ids as keys.
'att' =>
array
'student_1' => string 'P'
'student_2' => string 'A'
'student_3' => string 'L'

Getting radio button value by ajax

I want to get radio button values and send them through AJAX to PHP.
My AJAX is running but is currently inserting a 0 in each row, so it's not picking up the value from the radio button. Any help would be appreciated.
$("#save_privacy").submit(function() {
var message_pri = $("#message_pri").val();
var follow_pri = $("#follow_pri").val();
var post_pri = $("#post_pri").val();
var check7 = $("#check7").val();
var s = {
"message_pri": message_pri,
"follow_pri": follow_pri,
"post_pri": post_pri,
"check": check7
}
$.ajax({
url: 'edit_check.php',
type: 'POST',
data: s,
beforeSend: function() {
$(".privacy_info").html("<img src=\"style/img/ajax/load2.gif\" alt=\"Loading ....\" />");
},
success: function(data) {
$(".privacy_info").html(data);
}
});
return false;
});
<form id="save_privacy">
<table align="center" width="70%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td style="padding: 5px;">
<b>Message Buttun: </b>
</td>
<td style="padding: 5px;">
<input type="radio" id="message_pri" name="message_pri" value="1" /> ON
<input type="radio" id="message_pri" name="message_pri" value="2" /> OFF
<input type="radio" id="message_pri" name="message_pri" value="3" /> FOLLOWERS
</td>
</tr>
<tr>
<td style="padding: 5px;">
<b>Follow Buttun: </b>
</td>
<td style="padding: 5px;">
<input type="radio" id="follow_pri" name="follow_pri" value="1" /> ON
<input type="radio" id="follow_pri" name="follow_pri" value="2" /> OFF
</td>
</tr>
<tr>
<td style="padding: 5px;">
<b>Who Can Post On Your Profile: </b>
</td>
<td style="padding: 5px;">
<input type="radio" id="post_pri" name="post_pri" value="1" /> Evry one
<input type="radio" id="post_pri" name="post_pri" value="2" /> Followers
</td>
</tr>
<tr>
<td colspan="2" style="padding: 5px;">
<input type="hidden" id="check7" value="save_privacy" name="check7" />
<input class="small color blue button" type="submit" value="Save" />
</td>
</tr>
</table>
<div class="privacy_info"></div>
</form>
Firstly you have a lot of duplicated id attributes, which is incorrect. Use classes instead, then use the :checked selector to get the specific instance of the radio which was selected.
Try this:
<input type="radio" class="message_pri" name="message_pri" value="1" /> ON
<input type="radio" class="message_pri" name="message_pri" value="2" /> OFF
<input type="radio" class="message_pri" name="message_pri" value="3" /> FOLLOWERS
var message_pri = $(".message_pri:checked").val();
And so on for your other radio inputs.
dont use id two time first thing
now for selected value of radio box use
$("input:radio[name=post_pri] :selected").val();
I would like to add that it is best to use the onChange event instead of the onClick event on the radio fieldset button call to the AJAX function. I am not sure why, but in this case it posts the correct value every time. When using the onClick event it sometimes posts a value different from the checked value. It is not much but it will definitely save somebody somewhere from a slight headache.
Example of the radion button group:
<fieldset **onChange="return rating_score()"** id="rating_selectors" data-
role="controlgroup" data-type="horizontal">
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"1"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_0"
value="1" />
<label title="1" for="ratings_0"></label>
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"2"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_1"
value="2" />
<label title="2" for="ratings_1"></label>
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"3"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_2"
value="3" />
<label title="3" for="ratings_2"></label>
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"4"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_3"
value="4" />
<label title="4" for="ratings_3"></label>
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"5"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_4"
value="5" />
<label title="5" for="ratings_4"></label>
</fieldset>
Example of the AJAX function:
<script type="text/javascript">
function rating_score ( txt_rating )
{ $.ajax( { type : "POST",
data: {"txt_captcha" : $("#txt_captcha").val(), "txt_rating" :
$("input[name=rating]:checked").val()},
url : "functions/reviewrater.php",
success : function (txt_rating)
{
$('#rating-container').load(document.URL + ' #rating-container');
$('span.stars').stars();
},
error : function ( xhr )
{ alert( "error" );
}
} );
return false;
}
</script>
Quick explanation:
The onChange event in the fieldset sends the value of the checked radio button to the AJAX function. I have added validation for the rest of the elements on the page, so the <?php if (!(strcmp($row_rs_new_rating['rating_value'],"3"))) {echo "checked=\"checked\"";} ?> compares the value stored in the rating session and retrieves the value again, so the user does not have to click on the rating again once they are warned that some of the other elements are empty. It looks much more complicated than it is, but all I actually wanted to say was to use the onChange instead of the onCLick event. :-)
You can visit this page to see the above code in action:
Rental Property Reviews Page
Try this use serialize function check serialize here
$("#save_privacy").submit(function(){
var serialise = $("#save_privacy").serialize();
$.ajax({
url:'edit_check.php',
type:'POST',
data:serialise,
beforeSend: function (){
$(".privacy_info") .html("<img src=\"style/img/ajax/load2.gif\" alt=\"Loading ....\" />");
},
success:function(data){
$(".privacy_info") .html(data);
}
});
return false;
});

Categories