PHP radio button not posting correctly - php

I'm stuck in a bit of a pickle I can't figure out. Any help is appreciated.
I set up an HTML radio table
<div class="radio-group">
<label class="heading">Choose your game</label><br/>
<table>
<tr>
<td>
<input type="radio" name="radio" value="Radio 1"> Radio 1
</td>
</tr>
<tr>
<td>
<input type="radio" name="radio" value="Radio 2"> Radio 2
</td>
</tr>
<tr>
<td>
<input type="radio" name="radio" value="Radio 3"> Radio 3
</td>
</tr>
<tr>
<td>
<input type="radio" name="radio" value="Radio 4"> Radio 4
</td>
</tr>
</table>
</div>
</br>
<input type="submit" name="submit" value="Submit" />
<input type="reset" name="reset" value="Reset"/>
</form>
</div>
My PHP page will POST the correct selection, but loads other stuff it shouldn't. How do I differentiate between the radio selections if there are all the same name? If I name it separately, you can click on every selection.
if (isset($_POST['submit'])) {
if(isset($_POST['radio']))
{echo '<iframe etc etc></iframe>;
}
else{echo '<iframe other></iframe>;}
}

You can check what value was posted and then make your echo statement based on that. Here's an example of two radio buttons in the same group and name
<input type="radio" name="radio" value="1"> Radio 1
<input type="radio" name="radio" value="2"> Radio 2
and here is the PHP to see which one has been submitted
if(isset($_POST['radio'])) {
$value = $_POST['radio'];
if($value == "1") {
echo "SOMETHING HERE FOR VALUE 1";
} elseif($value == "2") {
echo "SOMETHING HERE FOR VALUE 2";
}
}
Notice how the value is set to something simple like an integer, it makes it easier to check it later on and then display the appropriate or desired outcome for that selected button :)

Related

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).

remember checkbox value if its checked array name in a form php

lets say we have this:
echo '<form method="post">
<div class="form-group">
<table class="table table-bordered table-hover table-striped" style="width:auto">
<tr>
<td><label for="array">ARRAY_NAME</label></td>
<td>
<input type="checkbox" name="array[]" value="1" /> option1<br />
<input type="checkbox" name="array[]" value="2" /> option2
</td>
</tr>
<tr>
<td><label for="array2">ARRAY_NAME2</label></td>
<td>
<input type="checkbox" name="array2[]" value="1" /> option1<br />
<input type="checkbox" name="array2[]" value="2" /> option2
</td>
</tr>
<tr>
<td><label for="array3">ARRAY_NAME3</label></td>
<td>
<input type="checkbox" name="array3[]" value="1" /> option1<br />
<input type="checkbox" name="array3[]" value="2" /> option2
</td>
</tr>
</table>
</div>
<button type="submit" name="submit" class="btn btn-success">Submit</button>
</form>';
I tried to implement this code: <?php echo (isset($_POST['array1[0]']) && $_POST['array1[0]'] == 1) ? "checked='checked'" : "" ?>
but it didn't work! It only works if you have name="array1" and name="array2". this way I'm thinking I can save multiple data in a parent array saved! Like this form[array1[],array2[],array3[]].
Can someone give me a solution because I'm stuck! Thanks in advance guys!!
You are trying to access the values incorrectly.
You can access a array posted like this:
echo $_POST['array1'][0];
php.net/$_POST See the first comment.
When you put square brackets in the name of form control, PHP will inflate that set of elements with similar names into an array.
You need to access:
$_POST['array1'][0]
not
$_POST['array1[0]'] // This is incorrect
(You also need to match the actual name of your control: Your HTML has array, array2 and array3 but not array1)

keep radio button values and set default checked

I have a form with a radio button group. I want to set 'checked' as default the second radio button, and also keep the value after submitting form if user clicks on the first one.
Note that I'm using the <span class="custom-radiobutton"></span> for style the radiobuttons, with a lower z-index than the real <input type="radio", which has opacity:0.
This is a snippet of my code:
<div class="group-wrapper">
<div class="radiobutton-wrapper boolean">
<span class="custom-radiobutton"></span>
<input type="radio" id="hosting-1" name="hosting[]" value="1" class="w100" <?php if ((isset($_POST['hosting'])) && ((isset($_POST['hosting'])) == 1)) {echo 'checked="checked"';}; ?> />
<label for="hosting-1">Sí</span>
</div>
<div class="radiobutton-wrapper boolean">
<span class="custom-radiobutton"></span>
<input type="radio" id="hosting-2" name="hosting[]" value="0" class="w100" <?php if ((!isset($_POST['hosting'])) || ((isset($_POST['hosting'])) == 0)) {echo 'checked="checked"';}; ?> />
<label for="hosting-2">No</label>
</div>
</div>
Additional info:
I'm using HTML5.
I'm validating the form with PHP (I want to keep this, even if I know maybe is better jQuery+PHP validation).
I have noticed that I need two clicks to select the first radio button. This only occurs from original state. After this, It works with one click, as expected.
I'm expending a lot of hours trying to figure out what's wrong, so any help will be very appreciated.
Cheers,
<input type="radio" id="hosting-1" name="hosting[]" class="w100" checked="checked" /> 1
try this in your code.
for example:
<tr>
<td><br><b>Reservation Handling : <span style="color:#FF0000">* </span></td>
<td><br><input type="radio" name="reservation" value="Delighted" required> Delighted<br></input></td>
</tr>
<tr>
<td> </td>
<td><input type="radio" name="reservation" checked="checked" value="Satisfied"> Satisfied</input></td>
</tr>
<tr>
<td> </td>
<td><input type="radio" name="reservation" value="Dissatisfied"> Dissatisfied</input></td>
</tr>
<tr>
<td> </td>
<td><input type="radio" name="reservation" value="N/A"> N/A</input></td>
</tr>
You have an error in your logic. You're using isset twice.
<input type="radio" id="hosting-1" name="hosting[]" value="1" class="w100" <?php if ((isset($_POST['hosting'])) && ((isset($_POST['hosting'])) == 1)) {echo 'checked="checked"';}; ?> />
You're essentially saying does isset equal 1, which in your case will always be true if $_POST['hosting'] has a value.
You should do this
<input type="radio" id="hosting-1" name="hosting[]" value="1" class="w100" <?php if (isset($_POST['hosting']) && $_POST['hosting'] == 1) {echo 'checked="checked"';} ?> />

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'

creating Dynamic controls in html with different names

I am creating radiobuttons in HTML .I am getting data from SQL Database.I am creating an attendence form in which there will be student name,Present Radio button ,Absent Radio button.
I put the Present and Absent Radio button in a single Group,(so that only one could be clicked.The problem is that I can't create more than one radio group..My code is
<html>
<tr>
<td><?php echo $i;?></td> // Serial Number
<td> <label><?php echo $att['std_name'];?></label></td> //student name
<td><input type="radio" name="std_r[]" id="std_r[]"></td> // for present
<td><input type="radio" name="std_r[]" id="std_c[]"></td> // for absent
<td><input type="hidden" value="<?php echo $att['rollnum'];?>" > </td> //hidden field that will take roll number of student on next page
</tr>
</html>
<?php
I want that for each student there should be seperate group of radio buttons.thats why i want to create buttons with different name.
Anyone who can help me with this
Thanks
I guess what you are asking is there are several students in the same page and each student with his own present/absent option.
You should do something like this:
<input type="radio" name="test[1]" value="present" />
<input type="radio" name="test[1]" value="absent" />
<input type="radio" name="test[2]" value="present" />
<input type="radio" name="test[2]" value="absent" />
<input type="radio" name="test[3]" value="present" />
<input type="radio" name="test[3]" value="absent" />
You may also specific the student_id as index, this will be easier for you do handle in PHP:
<input type="radio" name="test[33888]" value="present" />
<input type="radio" name="test[33888]" value="absent" />
<input type="radio" name="test[90909]" value="present" />
<input type="radio" name="test[90909]" value="absent" />

Categories