How do I set names for each radio button in the following PHP code using counter variable, or any other alternative?
<?php
$id=0; //counter variable
$sql ="select roll_number,student_name,gender from student_table";
$query=mysqli_query($dbcon, $sql) or die(mysqli_error($dbcon));
while($post=mysqli_fetch_assoc($query)){
echo '<tr>';
echo '<td>'. $post['roll_number'] . '</td>';
echo '<td>'. $post['student_name'] . '</td>';
echo '<td>'. $post['gender'] . '</td>';
echo '<td width=250>';
echo '<input type="radio" name="rad'.$id'" value="P" />Present';
echo '<input type="radio" name="rad'.$id'" value="A" />Absent';
echo '<button type="submit" name="save" value="Save" class="btn btn-success btn-sm">Save</button>';
++$id;
echo '</td>';
echo '</tr>';
}
?>
echo '<input type="radio" name="rad[' . $post['roll_number'] . ']" value="P" />Present';
echo '<input type="radio" name="rad[' . $post['roll_number'] . ']" value="A" />Absent';
On the receiving end you may do like this:
$answers = $_POST['rad'];
foreach($answers as $roll_number=>$answer){
//do something with the $roll_number and $answer...
}
Updated the code, changed the $post to $row, concatenated the strings, fixed the <button> and so on. There is no form-declaration anywhere - if you have only one form, this will save / fetch every item in the form - depending on your needs, you might wanna declare a form per student, with a unique identifier.
<?php
$query = mysqli_query($dbcon, 'SELECT roll_number,student_name,gender FROM student_table') or die(mysqli_error($dbcon));
while ($row = mysqli_fetch_assoc($query)) {
echo '<tr>
<td>'.$row['roll_number'].'</td>
<td>'.$row['student_name'].'</td>
<td>'.$row['gender'].'</td>
<td width="250">
<label><input type="radio" name="rad['.$row['roll_number'].']" value="P">Present</label>
<label><input type="radio" name="rad['.$row['roll_number'].']" value="A">Absent</label>
<input type="submit" name="save" value="Save" class="btn btn-success btn-sm">
</td>
</tr>';
}
?>
Related
i want to update data using radio button for each row of my data.
Example:
My table
id | name | age | number | account | note | place | radio
after i click the radio button (Mencurigakan/Tidak Mencurigakan), the radio will be updated. I tried using this code and i cannot read my id column and i only can update one row
Controller:
public function updateradio($id) {
$data=$this->welcome_model;
$data->radio=$this->input->post("radio$id");
$data->update_bb($id);
}
Model:
public function update_bb($id) {
$id = /*i need something here*/
$data = array('radio' => $this->input->post("radio$id"));
$this->db->where('id', $id);
$this->db->update('info', $data);
}
View:
<?php
foreach ($results as $row) {
echo '<tr>';
echo '<td>' . $row->id . '</td>';
echo '<td>' . $row->name . '</td>';
echo '<td>' . $row->age . '</td>';
echo '<td>' . $row->number . '</td>';
echo '<td>' . "Rp " . $row->account . '</td>';
echo '<td>' . $row->note . '</td>';
echo '<td>' . $row->place . '</td>';
?>
<form action="<?php echo base_url(); ?>welcome/updateradio" method="post">
<div class="radio">
<label>
<input type="radio" name="radio<?php echo $row->id ?>" value="3"/>
Mencurigakan
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio<?php echo $row->id ?>" value="2"/>
Tidak Mencurigakan
</label>
</div>
</td>
<?php
echo '</tr>';
}
?>
<button type="submit" class="btn btn-group">Update</button>
</form>
Any help would be appreciated!
I am new to php and MYSQL. For Morrocan oil, the radio buttons are getting checked when I click on Yes or No. But, the printStatement when I click is Yes/No. The default No is getting checked no matter what. Can you please help me resolve this issue. Thanks!
echo '<tr>
<td>' . _('Signed Moroccan Oil?') . '</td>';
if(strtoupper($_POST['signedMor']) == 'YES'){
echo '<td> <input type="radio" name="signedMor" id="signedMor" value="Yes" checked="checked">Yes
<input type="radio" name="signedMor" id="signedMor" value="No">No</td>';
} else {
echo '<td> <input type="radio" name="signedMor" id="signedMor" value="Yes">Yes
<input type="radio" name="signedMor" id="signedMor" value="No" checked="checked">No</td>';
}
echo '</tr>';
echo '<tr>
<td>' . _('Print Statement?') . '</td>';
if(strtoupper($_POST['printStatement']) == 'Y'){
echo '<td> <input type="radio" name="printStatement" id="printStatement" value="Y" checked="checked">Yes
<input type="radio" name="printStatement" id="printStatement" value="N">No</td>';
} else {
echo '<td> <input type="radio" name="printStatement" id="printStatement" value="Y">Yes
<input type="radio" name="printStatement" id="printStatement" value="N" checked="checked">No</td>';
}
echo '</tr>';
I am using multiple HTML checkboxes to submit student attendance. The checkbox once clicked submit '1' and if not checked, will submit '0'. I am able to submit '1' when checked but cant submit '0'. Below is my code:
<?php
$subcheck = (isset($_POST['subcheck'])) ? 1 : 0;
$date = date("Y/m/d");
foreach ( $student as $attendance ) {
echo "<tr>";
echo "<td>$attendance->id</td>";
echo "<td>$attendance->name</td>";
echo "<td>
$attendance->classroom_id
</td>";?>
<input type="hidden" name="date[]" value="<?php echo $date;?>" />
<td><input type="checkbox" name="status[]" value="<?php echo $attendance->id;?>"></td>
<td><input type="text" name="reason[]" ></td>
<?php
}
?>
<tr>
<td colspan="2" align="center"><input type="submit" value="Save" name="submit"></td>
This might give you some ideas:
<?php
$subcheck = (isset($_POST['subcheck'])) ? 1 : 0;
$date = date("Y/m/d");
$out = '<table id="tblAttendance"><thead><th>ID</th><th>Name</th><th>Room</th><th>Status</th><th>Reason</th></thead><tbody>';
foreach ( $student as $attendance ) {
$out .= '<tr>';
$out .= '<td>' .$attendance->id. '<input type="hidden" name="studentID[]" value="' .$attendance->id. '"></td>';
$out .= '<td>' .$attendance->name. '<input type="hidden" name="name[]" value="' .$attendance->name. '"></td>';
$out .= '<td>' .$attendance->classroom_id. '<input type="hidden" name="classroomID[]" value="' .$attendance->classroom_id. '"></td>';
$out .= '<td><input type="checkbox" name="status[]" value="yes"></td>';
$out .= '<td><input type="text" name="reason[]" ></td>';
$out .= '</tr>';
}
$out .= '<tr><td colspan="2" align="center"><input type="submit" value="Save" name="submit"></td></tr>';
$out .= '</tbody></table>';
$out .= '<input type="hidden" name="date" value="' .$date. '" />';
echo $out;
?>
Since you are using a type="submit" button, I presume you have this inside a form construct?
Recall how checkboxes work in HTML forms: if the box is checked, the value received on the PHP side will be the value="yes" value. In other words, the variable $_POST['status'][n] will have the value yes.
HOWEVER, if the checkbox is not set, then $_POST['status'][n] will not be set.
Reference:
http://www.html-form-guide.com/php-form/php-form-checkbox.html
I have a table that displays every unapproved leave, it looks like this:
For each unapproved there will be a new row, so another checkbox, but the name of the checkbox will be the same or slightly different(name="approve1"). Personally I don't think that's good practice...
Also the evaluation isn't really good, because it has to go into the database.
<form role="form" method="post" action="" id="form">
<h2><?php echo $title ?></h2>
<?php
$page = date('F');
$page = lcfirst($page);
echo "<p><a href='$page.php'>Back</a></p>";
?>
<hr>
<?php
if($i == 0){
echo "<h3>No unapproved leaves.</h3>";
}else{
echo '<table class="table table-striped table-hover">';
echo '<thead>';
echo '<th>';
echo 'Start';
echo '</th>';
echo '<th>';
echo 'End';
echo '</th>';
echo '<th>';
echo 'Employee';
echo '</th>';
echo '<th>';
echo 'Approve';
echo '</th>';
echo '</thead>';
echo '<tbody>';
for($j =0; $j < $i; $j++){
echo '<tr>';
echo '<td>';
echo $unapproved[$j]['start'];
echo '</td>';
echo '<td>';
echo $unapproved[$j]['end'];
echo '</td>';
echo '<td>';
$id = $unapproved[$j]['employee_FK'];
$result = mysql_query("select name, surname from employee where employee_ID = $id");
while ($row = mysql_fetch_assoc($result)) {
$employee[] = $row;
}
echo $employee[0]['name'], " ", $employee[0]['surname'];
echo '</td>';
echo '<td>';
echo '<input type="checkbox" name="approve" value="1" id="approve">';
echo '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
}
?>
<input type="submit" name="submit" value="Submit" class="btn" >
</div>
</div>
</form>
Can anyone think of a better, easier solution?
The practice for tabular data in most frameworks is to name them Model[id][field], so in your example it would look like "<input type='checkbox' name='leave[$j][approved]' value='1' id='leave_$j_approved'>".
Then you can easily access your data by iterating the post data like so:
foreach($_POST['leave'] as $id => $values) {
}
use checkbox name like this
<input type="checkbox" name="approve[]" value="<?php $your_row_id; ?>" id="approve">
here $your_row_id means unique id of a row in database.
And in post you will get an array named approve with all the selected values.
Parse that array using foreach.
You can generate checkboxes using a array notation in the "name" attribute.
<input type="checkbox" name="approve[]" value="1" id="approve">
After on send, you can recover values using array notation $_GET['approve'][0], ...
You can transfer arrays via HTTP :
echo '<input type="checkbox" name="approve[]" value="', $id, ']" id="approve">';
Now $_GET['approve'] will be an array which only contains values of checked boxes.
You can also specify the key associated with the value:
echo '<input type="checkbox" name="approve[', $id, ']" value="', $id, ']" id="approve">';
Hello Coders,
I have a dynamically created table in PHP and for each row there is a radio button. The intention is that depending upon which radio box is selected, the user can perform multiple functions like, Delete, Amend etc. I can make this all work with hard coded "Value" for the radio button like
echo "<td>" . '<input type="radio" name="radioSelect" value="2" checked="checked" />' . "</td>";
What I am looking for is a way to set the "2" dynamically. For example:
while($row = mysql_fetch_array($result) and $i<=100)
{
echo "<tr>";
$sno= $row['SNo'];
echo "<td>" . '<input type="radio" name="radioSelect" value= $sno checked="checked" />' . "</td>";
}
What is the syntax for this? Is this possible at all?
Thanks for your help.
Yes, it's called concatenation, and you're already doing it with the . operator after "<td>", for example.
echo "<td>" . '<input type="radio" name="radioSelect" value="' . $sno . '" checked="checked" />' . "</td>";
I'm not sure why you put it that way, though.
echo '<td><input type="radio" name="radioSelect" value="' . $sno . '" checked="checked" /></td>';
Also, note that only one radio button with the same name can be checked at a time, so you might want to rethink what you're doing there.
You can do like this ? if you are having a problem in CONCATENATION.
<?php
while($row = mysql_fetch_assoc($result) and $i<=100)
{ $sno= $row['SNo'];?>
<table>
<tr>
<td><input type="radio" name="radioSelect" value= <?php echo $sno; ?> checked="checked" /> <?php } ?></td>
</tr>
</table>