creating Dynamic controls in html with different names - php

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" />

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

Using a default value with an html form radio button

I have a web form that updates a customer record. The entries are prefilled with the data stored in mysql when called. One field shows if equipment was returned as follows:
<font size=5>Returned:</font><input type="text" name="ud_Returned" value="<?php echo $rtrnd; ?>" /><br />
My question is I setup to convert this to radio button like:
<font size=5>Returned:</font><input type="radio" name="ud_Returned" value=Yes /> Yes<br />
<input type="radio" name="ud_Returned" value="No" /> No<br />
But I want it so the value stored in the $rtrnd variable fills in the existing radio button with the appropriate Yes/No for that customer when form is called. Any ideas?
Maybe something like this would do the trick:
<input type="radio" name="ud_Returned" value="Yes" <?php if($rtrnd) echo 'checked'; ?> />
<input type="radio" name="ud_Returned" value="No" <?php if(!$rtrnd) echo 'checked'; ?> />
Assuming that $rtrnd is a boolean value. If it isn't just use a comparison in the if statements like if($rtrnd == 'yes').
Assuming your store this information as 1 or 0 in the database you'll have to -
<?php
// Get stuff from database
$result = /* The resulting array from your query */;
if( $result["ud_Returned"] == 1 )
{
echo('<font size=5>Returned:</font>
<input type="radio" name="ud_Returned" value="Yes" checked="checked" /> Yes<br />
<input type="radio" name="ud_Returned" value="No" /> No<br />');
}
else
{
echo('<font size=5>Returned:</font>
<input type="radio" name="ud_Returned" value="Yes" /> Yes<br />
<input type="radio" name="ud_Returned" value="No" checked="checked" /> No<br />');
}
I'm not suggesting that this is the exact implementation you should pursue but this is the logic - get information from database, make a comparison, add the checked attribute to the appropriate radio button.

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'

mySQL data type for form elements

what datatypes do i use for checkboxes and radio buttons and for input type=date in the pmySQL??
<pre>
<tr>
<td class="right_align">*Occupation : </td>
<td>
<input type="radio" name="occupation" value="student" />Student<br />
<input type="radio" name="occupation" value="business" />Business<br />
<input type="radio" name="occupation" value="pvtsect" />Priavte Sector<br />
<input type="radio" name="occupation" value="govtsect" />Govt. Sector<br />
<input type="radio" name="occupation" value="others" />Others<br />
</td>
</tr>
<tr>
<td class="right_align">*On which project you want to work :</td>
<td>
<input type="checkbox" name="" ="projectname" value="shine"/>Project Shine<br />
<input type="checkbox" name="" ="projectname" value="sahyog" />Project Sahyog<br />
<input type="checkbox" name="" ="projectname" value="smile" />Spreading Smiles
</td>
</tr>
</pre>
1) you will get string as the value of radio so it will be varchar type
2) for check boxes any one can have multiple values so you need to create a separate table(working_project) for this values( data type will be same as radio) and another table for mapping(user_working_project)
user_working_project table will containing user id and working project id
e.g one user has id=1, and working_project=1,2
then in your user_working_project table entries will be like this:
user_working_project_id working_project_id user_id
1 1 1
2 2 1

PHP Variables with undefined values from HTML form

Okay so I have a script which is basically like an order form. However my only input options are radio buttons. When I run my script, I noticed that it turned back errors. Instead I would like it to display and empty field. The page posts back to itself and it updates a box I left blank on the bottom before submission. After submission details are filled in from a multitude. I also used a two dimensional array if that makes any difference. So my problems are with empty variables in the forms. I was not able to fix this and I think it might be the way I'm using it. I get an undefined index error for the variables that receive the data. In this case $settype and $differentype come out as errors since they are defined to be whatever was in the textbox. I thought that by using empty() I could eliminate that issue. I did not approach it correctly of course. Can anyone give me some guidance here? Thank you.
<form name="form1" method="POST" action="order.php">
<input type="radio" name="set" value="1" /> 1
<input type="radio" name="set" value="2" /> 2
<input type="radio" name="set" value="3" /> 3
<input type="radio" name="different" value="a" /> a
<input type="radio" name="different" value="b" />
<input type="radio" name="different" value="c" />
<input type="submit" name="submit1" value="login" />
</form>
<?php
if(isset($_POST['submit'])) {
$settype = $_POST['set'];
$differentype = $_POST['different'];
if ($settype == '1' && $differenttype =='a'){
$order = $field[0][1];
}
if (empty($settype) && empty($differenttype)){
$order = "";
}
}
?>
<table>
<tr>
<td>
<?php print($order); ?>
</td>
</tr>
</table>
Where is your input submit ? like this <input type="submit" name="submit" value="submit" />
EDIT 1: You should have a default radio button selected value like
<input type="radio" name="set" value="1" checked="checked"/> 1
<input type="radio" name="different" value="a" checked="checked" /> a

Categories