Send multiple values with checkbox - php

I have a form and this form contains this table:
<?php foreach($resultTable as $key => $value)
{
?>
<table>
<tr>
<td><input type = "checkbox" name="idPriv[]" id="idPriv" onclick="evaluateIT(this)" data-related-item="adminPanelShow" value ="<?php echo $value["id"]?>" />
<input name="rowID[]" id="rowID[]" class="adminPanel" hidden="hidden" type="text" value="<?php echo $value["id"]?>"/></td>
<td><input type="text" name="userName[]" id="userName" class="adminPanel" value="<?php echo $value["userName"]?>"/></td>
<td><input name="firstName[]" type="text" id="firstName" class="adminPanel" value="<?php echo $value["firstName"]?>"/></td>
</tr>
<?php } ?>
</table>
When I'm selecting the wanted checkboxes and submitting the form, I want to use only the checkboxes that I checked. That means, the idPriv[] array returns only the checkboxes that was pressed, BUT the other arrays (userName[], firstName[]) send all of the data for all the rows.
How do I extract the data from those arrays only (and disregard the rows that wasn't checked)?

To check if the checkboxes are checked, parse them as such:
foreach($resultTable as $key => $value)
{
if($value==="on")
{
// checked checkbox has a value of "on"
// triple = sign means value is exactly "on"
}
}

Connect each userName, firstName field with checkbox value:
foreach($resultTable as $key => $value)
{?>
<table>
<tr>
<td><input type = "checkbox" name="idPriv[]" id="idPriv" onclick="evaluateIT(this)" data-related-item="adminPanelShow" value ="<?php echo $value["id"]?>" />
<input name="rowID[]" id="rowID[]" class="adminPanel" hidden="hidden" type="text" value="<?php echo $value["id"]?>"/></td>
<td><input type="text" name="userName[<?php echo $value["id"]?>]" id="userName" class="adminPanel" value="<?php echo $value["userName"]?>"/></td>
^--- here
<td><input name="firstName[<?php echo $value["id"]?>]" type="text" id="firstName" class="adminPanel" value="<?php echo $value["firstName"]?>"/></td>
^--- here
</tr>
}
</table>
After that in your script you can do something like that:
foreach ($_POST[`idPriv`] as $id) {
$firstName = $_POST['firstName'][$id];
$userName = $_POST['userName'][$id];
// ...
}

Related

Form send data to php with multiple inputs with the same name

I'm trying make a form where me or user can insert data. Some parts are coming form data base like: Work name and work price. In input fields can insert work amount. In each input row have checkbox, if checkbox ar checked, that row will be seen in php.
<table class="table">
<thead>
<tr>
<th scope="col">*</th>
<th scope="col">Work name</th>
<th scope="col">Quantity</th>
<th scope="col">Price for unit</th>
<th scope="col">Total price</th>
</tr>
</thead>
<form method="POST" action="process/pdf.process.php">
<tbody>
<?php
$works = ORM::for_table('work')->find_many();
foreach($works as $work){
?>
<tr id="<?php echo $work->id; ?>">
<td><input type="checkbox" name="useit[]" value="<?php echo $work->id; ?>"/></td>
<td><?php echo $work->name; ?></td>
<td><input type="text" placeholder="" class="amount"/> <?php echo $work->unit; ?></td>
<td class="work_price" data="<?php echo $work->price; ?>"><?php echo $work->price.'€/'.$work->unit; ?></td>
<td class="total_price">0€</td>
<input type="" name="work_id[]" value="<?php echo $work->id; ?>" />
<input type="hidden" name="work_name[]" value="<?php echo $work->name; ?>" />
<input type="hidden" name="amount[]" class="<?php echo $work->id; ?>_copy_amount" value="" />
<input type="hidden" name="unit[]" value="<?php echo $work->unit; ?>" />
<input type="hidden" name="unit_price[]" value="<?php echo $work->price; ?>€" />
<input type="hidden" name="unit_total[]" class="<?php echo $work->id; ?>_copy_total" value="" />
</tr>
<?php
}
?>
</tbody>
<input type="submit" name="do_pdf" value="Pga jkāuztaisa ar jquery" />
</form>
</table>
Now, there is php, but how can i show only checked rows in while loop?
<?php
$data = array();
$work_id = array();
$work_names = $_POST['work_name'];
$amounts = $_POST['amount'];
$units = $_POST['unit'];
$units_prices = $_POST['unit_price'];
$units_total = $_POST['unit_total'];
if(isset($_POST['useit'])){
$work_id = $_POST['useit'];
}
$data = array($work_id, $work_names, $amounts, $units, $units_prices, $units_total);
echo '<pre>';
echo htmlspecialchars(print_r($data, true));
echo '</pre>';
?>
There are different possibilities how to do that. One Suggestion (i limit it to the parts which are relevant)
form (note that i gave work_name the id as an index, use_it not (but it could have)
<td><input type="checkbox" name="useit[]" value="<?php echo $work->id; ?>"/></td>
<input type="hidden" name="work_name[<?php echo $work->id?>]" value="<?php echo $work->name; ?>" />
The form only submits the checked checkboxes values in an array, all other are omited. Therefore we could loop over the checked checkbox values like this
foreach($_POST['useit'] as $work_id){
$work_name = $work_names[$work_id];
//do something with the checked rows only (all others are not looped over)
}
This is only possible, due to the given id as an array key in the form work_name[<?php echo $work->id?>].
A general sidenote for better (and more secure) code: please note that your data could be modified by the user, and send back with wrong data (or worse). So please make sure to sanitize your input, or probably better in this case only submit the id in question and the new data and pickup the rest directly from your database. So you can make sure the hidden data has not been modified on the client side and send back wrong.

How to display comma separated values inside an input field from the database in php

Disclaimer: I am fairly new at this!
This is what I am doing:
if ($submit) {
$gasTime= implode(",", $_POST['gasTime']);
$meterTime= implode(",", $_POST['meterTime']);
$heatInput= implode(",", $_POST['heatInput']);
$labTemp= implode(",", $_POST['labTemp']);
$db->query("INSERT INTO tester SET
mainId='$mainId',
gasTime='$gasTime',
meterTime='$meterTime',
heatInput='$heatInput',
labTemp='$labTemp'");
$success = "Tested successfully!";
}else{
$db->query("SELECT gasTime FROM tester WHERE gasTime='$gasTime'");
$gasTime = explode(",", $gasTime );
$meterTime= explode(",", $meterTime);
$heatInput= explode(",", $heatInput);
$labTemp= explode(",", $labTemp);
**$result = $db->query($sql);
$row = $result->fetch_assoc();**
}
HTML :
<tr>
<td>Time gas</td>
<td>TG</td>
<td>s</td>
<td><input class="form-control" type="text" id="gasTime" name="gasTime[]" value="<? echo #$row[gasTime ]; ?>"></td>
<td><input class="form-control" type="text" id="gasTime" name="gasTime[]" value="<? echo #$row[gasTime ]; ?>"></td>
<td><input class="form-control" type="text" id="gasTime" name="gasTime[]" value="<? echo #$row[gasTime ]; ?>"></td>
<td><input class="form-control" type="text" id="gasTime" name="gasTime[]" value="<? echo #$row[gasTime ]; ?>"></td>
<td><input class="form-control" type="text" id="gasTime" name="gasTime[]" value="<? echo #$row[gasTime ]; ?>"></td>
<td></td>
</tr>
submitting works perfectly fine but displaying the data to an input field doesn't seem to work.
EDIT:
So apparently I wasn't retriving the data and thanks to your help I can now retrieve the values in the input field but the problem now is that I am retriving all 5 values with their commas e.g. "1,2,3,4,5", is there a way to retrieve a value at a time on each input field? e.g. first input 1 second 2 and etc.

to get the form data from multiple checkbox

As i want to get the form data and want to process that data and store into database..I am getting all checked box values but i am enable to get the text value although i used $_POST['text-name'] in the code...Please help me to get the
error..My code is Below
if(isset($_POST['give-score'])&&!empty($_POST['checked'])){
$employeedetails = $_POST['checked'];
$score = $_POST['score'];
$username = $employeedetails[1];
$workname = $employeedetails[2];
changeworkstatus($username,$workname,$con);
$workname = $employeedetails[2];
addscorepoints($workname,$score,$con);
}else{
echo "";
}
and my form html code is below
<td><input type="checkbox" name="checked[]" id="employeework" value="" style="align: center"></td>
<td><input type="checkbox" name="checked[]" id="employeework"value="<?php echo $results['username']; ?>"><?php echo $results['username']; ?></td>
<td><input type="checkbox" name="checked[]" id="employeework"value="<?php echo $results['work_name'];?>"><?php echo $results['work_name'];?></td>
<td><input type="text" name="score" id="score" placeholder="Your Score Here"></td>
<td><input type="submit" name="give-score"></td>
php part used in the table are working fine..but the input[type=text] i am not getting that value..
PHP arrays index starts from 0 not 1. then you must change this lines:
$username = $employeedetails[0];
$workname = $employeedetails[1];
Hope this help you!
I think there is no problem in your code even then I am providing the following code which I've tried. I am getting all values from the form elements.
Code of the html file
<html>
<body>
<form action="test.php" method="POST"> Checked value:
<td><input type="checkbox" name="checked[]" id="t1" value="test1">test1</td>
<td><input type="checkbox" name="checked[]" id="t2" value="test2">test2</td>
<td><input type="checkbox" name="checked[]" id="t3" value="test3">test3</td>
<td><input type="text" name="score" id="score" placeholder="Your Score Here"> </td>
<td><input type="submit" name="give-score"></td>
</form>
</body>
</html>
Code of PHP file
<?php
if(isset($_POST['give-score'])&&!empty($_POST['checked'])){
$employeedetails = $_POST['checked'];
echo $score = $_POST['score']."<br>";
echo $username = $employeedetails[0]."<br>";
echo $workname = $employeedetails[1]."<br>";
echo $workname = $employeedetails[2]."<br>";
}else{
echo "No data found";
}
?>
I hope this will help you!

How to return to a dynamic page on submit

I'm trying to return to a page that is dynamically created from data stored in a database. However when I try to return to the main dynamic page it does not work. It should on submit redirect to the page that shares the yard id for the track. Instead it just gives me a failure message
Controller
public function update_track($yard_id)
{
$data = array(
'track_id'=>$this->input->post('track_id'),
'train_id'=>$this->input->post('train_id'),
'train_direction'=>$this->input->post('train_direction'),
'train_length' =>$this->input->post('train_length'),
'train_hpt'=>$this->input->post('train_hpt'),
'train_tons'=>$this->input->post('train_tons'),
'train_status'=>$this->input->post('train_status'),
);
$this->load->model('atisyard_model');
$this->atisyard_model->update_track($data);
if($this->db->affected_rows()>0)
{
redirect('/atis/'.$value->yard_id);
}
else
{
echo 'failure';
}
}
View
<?=form_open('atis/update_track/'); foreach ($record as $value) {?>
<h1>Edit Track:</h1>
<b><p>Track Name: </b><?php echo $value->track_no;?></br>
<b>Track Length: </b><?php echo $value->track_length;?>'</br></br></p>
<table cellpadding="5" border="0" align="center">
<input type="hidden" name="track_id" title="track_id" value="<?php echo $value->track_id;?>">
<tr>
<td>Lead Engine No. / Symbol / Work Order:</td>
<td><input type="text" name="train_id" id="train_id" size="28" maxlength="28" value="<?php echo $value->train_id;?>"></td>
</tr>
<tr>
<td>Train Direction:</td>
<td><?=form_dropdown('train_direction', $train_direction, $value->train_direction);?></td>
</tr>
<tr><td>Train Length:</td>
<td><input type="text" name="train_length" id="train_length" size="4" maxlength="6" value="<?php echo $value->train_length;?>"></td>
</tr>
<tr><td>HP/T:</td>
<td><input type="text" name="train_hpt" id="train_hpt" size="2" maxlength="5" value="<?php echo $value->train_hpt;?>"></td>
</tr>
<tr><td>Train Tons:</td>
<td><input type="text" name="train_tons" id="train_tons" size="4" maxlength="6" value="<?php echo $value->train_tons;?>"></td>
</tr>
<td>Train Status:</td>
<td> <?=form_dropdown('train_status', $train_status, $value->train_status);?></td>
</tr>
<tr>
<tr><td><button id="submitbtn" >Submit</button></td></tr>
</table>
Back to Yard
<?php } echo form_close();?>
Taking a second look at your code, would it be better to add the yard_id as a hidden field on your form:
<input type="hidden" name="yard_id" name ="<?=$value->yard_id; ?>" />
then change your redirect to:
redirect('/atis/'.$this->input->post('yard_id'));
If I understand this correctly and you are passing $yard_id into your update_track() function, then shouldn't this:
redirect('/atis/'.$value->yard_id);
Be just :
redirect('/atis/'.$yard_id);
Because I can't see where your getting $value from.

How to send multiple row value submit button?

I get the values from same row, but many values with fetch array. I did it, but with buttons, as many as how many values there are. If I have 5 rows, then I should have five submits, but I want one submit button.
Here is my code:
$result2 = mysql_query ("select * from price where dom='$cat'",$db);
$myrow2= mysql_fetch_array($result2);
<form action="priceupdatetes.php" method="post">
<?php
do {
echo <<<here
<td><input name="etiket[$myrow2[id]]" type="text" value="$myrow2[etiket]"/></td>
<td><input name="pricestandart[$myrow2[id]]" type="text" value="$myrow2[pricestandart]"/></td>
<td><input name="number[$myrow2[id]]" type="text" value="$myrow2[number]"/></td>
<td><input name="totalunper[$myrow2[id]]" type="text" value="$myrow2[totalunper]" disabled="disabled"/></td>
<td><input name="discount[$myrow2[id]]" type="text" value="$myrow2[discount]"/></td>
<td><input name="totalwithper[$myrow2[id]]" type="text" value="$myrow2[totalwithper]" disabled="disabled"/></td>
</tr>
here;
}
while($myrow2= mysql_fetch_array($result2)) ;
?>
<input NAME="id[]" TYPE=hidden value="<?php foreach($myrow2[id] as $mid) {print $mid;} ?> "/>
<input name="submit" type="submit" value="Submit"/><br>
</form>
HERE is UPDATEPAGE.php:
if (isset($_POST['etiket'])) {$etiket = $_POST['etiket']; }
if (isset($_POST['pricestandart'])) {$pricestandart = $_POST['pricestandart'];}
if (isset($_POST['number'])) {$number = $_POST['number']; }
if (isset($_POST['discount'])) {$discount = $_POST['discount']; }
if (isset($_POST['id'])) {$id = $_POST['id']; }
$totalunper=$pricestandart*$number;
$percent=$discount/100;
$totalwithper1=$totalunper*$percent;
$totalwithper=$totalunper-$totalwithper1;
foreach($id as $team_id)
{
$result = mysql_query("UPDATE price SET etiket='$etiket[$team_id]',pricestandart='$pricestandart[$team_id]',number='$number[$team_id]',totalunper='$totalunper[$team_id]',discount='$discount[$team_id]',totalwithper='$totalwithper[$team_id]' WHERE id='$team_id'"); }
How can I get values with different id's and update them?
<input NAME="id[]" TYPE=hidden value="<?php foreach($myrow2[id] as $mid) {print $mid;} ?> "/>
is wrong. $myrow2[id] is not an array. Inside your while-loop, you should add:
<input NAME="id[]" TYPE="hidden" value="$myrow2[id]"/>

Categories