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];
Related
I have the following code. I want to get only selected checkbox values but I get only last checkbox value. see what's wrong in my code.
<div class="box-body table-responsive no-padding">
<h4>Select Jobwork</h4>
<table id="data-table" class="table table-hover jobworks-table table-responsive">
<thead>
<tr style="background-color: #DDDD; color:firebrick;">
<th><input type='checkbox' value="1" name="select_all" /></th>
<th>JobWork Name</th>
<th>Description</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php foreach ($item as $key=>$value){
$decoded = json_decode($value['jobWorkJsonString']); ?>
<?php foreach($decoded as $row){ ?>
<tr class="odd gradeX">
<td><input type='checkbox' name="<?php echo $row->jobwork; ?>" /></td>
<td style="padding-left:0px;"><?php echo $row->jobwork; ?><input type="hidden" name="jobwork_name" value="<?php echo $row->jobwork; ?>"></td>
<td style="padding-left:0px;"><?php echo $row->description; ?><input type="hidden" name="jobwork_description" value="<?php echo $row->description; ?>"></td>
<td style="padding-left:0px;"><?php echo $row->jobPrice; ?><input type="hidden" name="price" value="<?php echo $row->jobPrice; ?>"></td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
<br>
</div>
If you submit multiple fields with the same name, then PHP will only put the last one into $_POST / $_GET.
The exception is when you end the name with [] in which case it will generate an array instead.
However, checkboxes are only successful controls when checked, so simply adding [] to the names will cause the association between each set of date to be lost.
Instead of submitting all the data about each jobwork (you aren't trying to change it, otherwise you wouldn't be using hidden inputs!), put all the data you need into the checkbox input. Look up the rest of the data on the server.
<td><input type='checkbox' name="jobwork[]" value="<?= htmlspecialchars($row->id); ?>"/></td>
I've used $row->id as an example. I don't know how your data is structured on the server.
You will then be able to do:
foreach ($_POST['jobwork'] as $jobwork_id) {
$row = look_up_jobwork_by_id($jobwork_id);
}
… only the checked checkboxes values will appear in that array.
You have issue in the first checkbox code.
Name is given as value, both names should be same for the checkbox,
and get the request as an array
<?php foreach ($item as $key=>$value){
$decoded = json_decode($value['jobWorkJsonString']); ?>
<?php foreach($decoded as $row){ ?>
<tr class="odd gradeX">
<td><input type='checkbox' name="jobwork" value="<?php echo $row->jobwork; ?>" /></td>
<td style="padding-left:0px;"><?php echo $row->jobwork; ?><input type="hidden" name="jobwork_name" value="<?php echo $row->jobwork; ?>"></td>
<td style="padding-left:0px;"><?php echo $row->description; ?><input type="hidden" name="jobwork_description" value="<?php echo $row->description; ?>"></td>
<td style="padding-left:0px;"><?php echo $row->jobPrice; ?><input type="hidden" name="price" value="<?php echo $row->jobPrice; ?>"></td>
<?php } ?>
</tr>
<?php } ?>
get the value of job_work as an array, you will get the selected job_work
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)
<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];
}
?>
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>
I need to show radio as checked if the array $row['featured']; = 1 if $row['featured']; = 0 don't show as checked
here is the code
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td width="6%" align="center"><input type="radio" name="features" value="<?php echo $row['featured']; ?>" /></td>
<td width="35%"><?php echo $row['job_title']; ?></td>
<td width="24%"><?php echo $row['category_name'] ?></td>
<td width="15%"><?php echo $row['job_location']; ?></td>
<td width="10%"><?php echo $row['status'] ?></td>
<td width="10%">Edit</td>
</tr>
<?php
}
?>
<input type="radio" name="features" value="<?php echo $row['featured']; ?>"
<?php echo ($row['featured'] == 1?'checked':'')?>/>
DEMO
<input type="radio" name="features" value="<?php echo $row['featured']; ?>"
<?php echo $row['featured']?'checked="checked"':'';?>/>