CodeIgniter Marking checkboxes from database - php

I want to repopulate a page that contains checkboxes from values in the database. Currently, they are being stored as a string that gets split into an array when processing e.g 3,4,5.
I am able to check the boxes with the data from my database but another empty checkbox is created by its side. How do I only check the boxes with data from the database without having a duplicate empty box by its side?
<tbody>
<?php
if(!empty($datatable)){
foreach ($datatable as $data){
?>
<tr>
<td>
<?php foreach ($event_contacts as $checked){?>
<?php if($checked==$data->id){?>
<input type="checkbox" name="id[]" id="id" checked value="<?php echo $data->id; ?>"/>
<?php }else{ ?>
<input type="checkbox" name="id[]" id="id" value="<?php echo $data->id; ?>"/>
<?php }}?>
</td>
<td><?php echo $data->first_name." ".$data->last_name; ?></td>
<td><?php echo $data->email; ?></td>
<td><?php echo $data->phone_number; ?></td>
<td><?php echo $data->address;?></td>
</tr>
<?php
}
}
?>
</tbody>
Hope someone can help. Thanks
EDIT: Added photo for clarity

just change your code like this
<tbody>
<?php
if(!empty($datatable)){
foreach ($datatable as $data){
?>
<tr>
<td>
<?php
if (in_array($data->id, $event_contacts)) {
?>
<input type="checkbox" name="id[]" id="id" checked value="<?php echo $data->id; ?>"/>
<?php
}else{
?>
<input type="checkbox" name="id[]" id="id" value="<?php echo $data->id; ?>"/>
<?php
}
?>
</td>
<td><?php echo $data->first_name." ".$data->last_name; ?></td>
<td><?php echo $data->email; ?></td>
<td><?php echo $data->phone_number; ?></td>
<td><?php echo $data->address;?></td>
</tr>
<?php
}
}
?>
</tbody>

Related

Form Action PHP Sql

I have coding like this, it should when clicked Dekripsi button, it will appear download.php file, but this coding does not work
<?php
$nik = $_SESSION['nik'];
$no = 1;
$sql = "SELECT enkrip.Id,
(SELECT user.nama
FROM user
WHERE user.nik=enkrip.filefrom) as nama,
enkrip.filefrom,
enkrip.filename,
enkrip.kunci,
enkrip.dateupload,
enkrip.folder
FROM enkrip
WHERE enkrip.fileto = '$nik'
ORDER BY enkrip.Id ASC";
$data = mysql_query($sql);
while ($master = mysql_fetch_array($data)) {
?>
<form action="download.php?Id=<?php echo $master['Id']; ?>&file=<?php echo $master['folder']; ?>" method="post" target="_blank" >
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $master['nama'] ?></td>
<td><?php echo $master['filename'] ?></td>
<td><?php echo $master['kunci'] ?></td>
<td><?php echo $master['dateupload'] ?></td>
<td><input type="text" class="form-control" name="kunci" placeholder="Masukan Kunci Dekrip"></td>
<td><button type="submit" class="btn btn-success">Dekripsi</button></td>
<tr>
</form>
<?php } ?>
You are using method post for your form so:
<form action="download.php" method="post" target="_blank" >
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $master['nama'] ?></td>
<td><?php echo $master['filename'] ?></td>
<td><?php echo $master['kunci'] ?></td>
<td><?php echo $master['dateupload'] ?></td>
<td><input type="text" class="form-control" name="kunci" placeholder="Masukan Kunci Dekrip"></td>
<td><button type="submit" class="btn btn-success">Dekripsi</button></td>
<tr>
<input type="hidden" name="Id" value="<?php echo $master['Id']; ?>">
<input type="hidden" name="file" value="<?php echo $master['folder']; ?>">
</form>
All the parameters must be passed in post mode (using hidden input fields)

How to add multiple rows of data from one sql table to another with selection based on checkboxes?

<form method="post">
<?php
$sql_u="SELECT * from cubaan";
$query_u = mysqli_query($conn,$sql_u);
while($row=mysqli_fetch_assoc($query_u)){
?>
<table border="2">
<tr>
<td><input type="checkbox" name="bio[]" value="<?php $row['name'];?>" data-valuetwo="<?php $row['age'];?>" data-valuethree="<?php $row['job'];?>"></td>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['age'];?></td>
<td><?php echo $row['job'];?></td>
</tr>
</table>
<?php
}
?>
<input type="submit" name="post">
</form>
this is my code where I want to select data from the database and the selected data will insert to the new database
Use input type hidden as following
<form method="post">
<?php
$sql_u="SELECT * from cubaan";
$query_u = mysqli_query($conn,$sql_u);
while($row=mysqli_fetch_assoc($query_u)){
?>
<table border="2">
<tr>
<td>
<input type="checkbox" name="bio[<?php echo $row['id'];?>]" value="<?php echo $row['name'];?>">
<input type="hidden" name="age[<?php echo $row['id'];?>]" value="<?php echo $row['age'];?>">
<input type="hidden" name="job[<?php echo $row['id'];?>]" value="<?php echo $row['job'];?>">
</td>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['age'];?></td>
<td><?php echo $row['job'];?></td>
</tr>
</table>
<?php
}
?>
<input type="submit" name="post">
</form>
And while your form submit action use
<?php foreach($_POST['bio'] as $id=>$name) {
echo $name;
echo $_POST['age'][$id];
echo $_POST['job'][$id];
}
?>

while loop result at table, sends first row when submit form in php

1.fetch user data and display in tabular fashion
<?php
while($row=mysqli_fetch_assoc($rResultSet))
{
?>
<td><?php echo $row['id']?></td>
<td><?php echo $row['name']?></td>
<td><?php echo $row['user_role']?></td>
<td><?php echo $row['start_date']?></td>
<td>
<form id='euser' action='edituser.php' method='post'>
<input type="hidden" name="id" value="<?php echo $row['id']?>">
<input type="hidden" name="type" value="edituser">
Edit</i>|
</form>
<?php
}
?>
result of this code
2.when click on Edit link every time first id which 16 throw by form
i need , when press link edit at id 17 then id 17 thrown by form
Try below if it help :
<?php
while($row=mysqli_fetch_assoc($rResultSet))
{
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['user_role'];?></td>
<td><?php echo $row['start_date'];?></td>
<td>
<form id='<?php echo $row['id'];?>' action='edituser.php' method='post'>
<input type="hidden" name="id" value="<?php echo $row['id'];?>">
<input type="hidden" name="type" value="edituser">
Edit|
</form>
</td>
</tr>
<?php
}
?>
The problem is because of form's id attribute, it has to be unique for each row. Right now you're assigning the same id value euser for each row. For maintaining the uniqueness of id value, what you can do is, append the $row['id'] with euser for each table row. Also, you forgot to encapsulate each table row in <tr>...</tr>. So your code should be like this:
<?php
while($row=mysqli_fetch_assoc($rResultSet)){
?>
<tr>
<td><?php echo $row['id']?></td>
<td><?php echo $row['name']?></td>
<td><?php echo $row['user_role']?></td>
<td><?php echo $row['start_date']?></td>
<td>
<form id='euser<?php echo $row['id']?>' action='edituser.php' method='post'>
<input type="hidden" name="id" value="<?php echo $row['id']?>">
<input type="hidden" name="type" value="edituser">
Edit</i>|
</form>
<tr>
<?php
}
?>

PHP Radio button grab two values

So the below code grabs the server_id from the row that the user selects. However, I am wondering if it is possible to grab both the server_id and server_name from the row without the user having to do anymore than he already does (select a radio button and push submit). Thanks for any help.
<form method="post" name="server_information" id="server_information" action="test.php">
<label>Server Information</label><br><br>
<table border='1'>
<tr>
<th>Selection</th>
<th>User Name</th>
<th>Date Created</th>
<th>Server Name</th>
<th>Server Id</th>
<th>Public DNS</th>
<th>Server Status</th>
</tr>
<?php while($row = mysql_fetch_assoc($result)): ?>
<tr>
<td><input type="radio" name="server" value="<?php echo $row['serverId']; ?>" /></td>
<td><?php echo $row['userName']; ?></td>
<td><?php echo $row['dateCreated']; ?></td>
<td><?php echo $row['serverName']; ?>"</td>
<td><?php echo $row['serverId']; ?></td>
<td><?php echo $row['publicDNS'] ?></td>
<td><?php echo $row['serverStatus']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<br>
<?php endif; ?>
<input type="submit" name="server_stop" value="Stop Server"/>
<input type="submit" name="server_terminate" value="Terminate Server"/>
</form>
You can put a pipe | or any other character to concatenate both in the radio value, then explode your $_POST['server'] on pipe, you'll get id in index 0 and name in index 1.
<td><input type="radio" name="server" value="<?php echo $row['serverId'].'|'.$row['serverName']; ?>" /></td>
Another solution would be to add a hidden input and name it with the server id like this:
<td>
<input type="radio" name="server" value="<?php echo $row['serverId']; ?>" />
<input type="hidden" name="name_<?php echo $row['serverId'];?>" value="<?php echo $row['serverName'];?>" />
</td>
then in PHP
$server_id=(int)$_POST['server'];
$server_name=$_POST['name_'.$server_id];
You can use an array:
name="server[$row['serverId']]" value="$row['serverName']"
Then get it with:
list($id, $name) = $_POST['server'];
Sure, just concatenate the two values with a comma (or other delimiter that won't appear in the values):
<tr>
<td><input type="radio" name="server"
value="<?php echo $row['serverId'] . ',' . $row['serverName']; ?>" /></td>
<td><?php echo $row['userName']; ?></td>
<td><?php echo $row['dateCreated']; ?></td>
<td><?php echo $row['serverName']; ?>"</td>
<td><?php echo $row['serverId']; ?></td>
<td><?php echo $row['publicDNS'] ?></td>
<td><?php echo $row['serverStatus']; ?></td>
</tr>
Then when you process the form use explode() to separate the results:
$server_info = explode(",", $_POST["server"]); // server_info is an array
$server_selection = $server_info[0];
$server_name = $server_info[1];

pass relevant hidden field values from the form to the controller

Im having a page that shows monthly subscriptions of a user which is created using codeigniter. what i want to do is when a the user clicks on make payment pass the values in the hidden files to the controller.
<?php echo form_open('options/done');?>
<table class="tables">
<thead>
<tr>
<th>Ref Code</th>
<th>Month</th>
<th>Year</th>
<th>action/th>
</tr>
</thead>
<tbody>
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];?>
<input type="hidden" value="<?php echo $month;?>" name="month_<?php echo $s;?>" />
<input type="hidden" value="<?php echo $payment['ref_code'];?>" name="ref_<?php echo $s;?>" />
<tr>
<td><?php echo $payment['ref_code'];?></td>
<td><?php echo $month;?></td>
<td><?php echo $payment['year'];?></td>
<td><input type="submit" value="MAKE PAYMENT" class="red" /></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php echo form_close();?>
so when someone hits the submit button how can i pass only the hidden values which are relevant to that table row?
add form just inside the <tr> elements inside your loop (see below with your code)
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];
?>
<tr>
<form action="target.php" method="post" name="formName_<?php echo $s;?>" >
<input type="hidden" value="<?php echo $month;?>" name="month_<?php echo $s;?>" />
<input type="hidden" value="<?php echo $payment['ref_code'];?>" name="ref_<?php echo $s;?>" />
<td><?php echo $payment['ref_code'];?></td>
<td><?php echo $month;?></td>
<td><?php echo $payment['year'];?></td>
<td><input type="submit" value="MAKE PAYMENT" class="red" /></td>
</form>
</tr>
<?php endforeach; ?>

Categories