pass relevant hidden field values from the form to the controller - php

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; ?>

Related

Why submitting this form it submits wrong ids?

I am submitting this from and in the same page getting the values of the $_POST['id] and displaying them but its getting the wrong id not the ones I am selecting from the row in the table:
<tbody>
<?php if (!empty($found_devices)) {
foreach ($found_devices as $devs) : ?>
<tr>
<td>
<input type="checkbox" id="myCheck" onclick="display_hide_selectClient(this)" name="devices[<?php echo $devs["id"] ?>]" value="<?php echo $devs["id"] ?>">
</td>
<td>
<?php echo $devs["serial_imei"] ?>
</td>
<td>
<?php echo $devs["serial_no"] ?>
</td>
<td>
<?php echo $devs["created_date"] ?>
</td>
<td>
<input hidden name="edit_device_imei" value="<?php echo $devs["serial_imei"] ?>">
<input hidden name="edit_device_serial" value="<?php echo $devs["serial_no"] ?>">
<input hidden name="edit_device_id" value="<?php echo $devs["id"] ?>">
<button type="submit" name="edit_device">Edit</button>
</td>
</tr>
<?php endforeach;
} ?>
</tbody>
So, when submitting this form with the button edit_device I am receiving the wrong values in PHP.
PHP Ccode:
if (isset($_POST['edit_device']) && !empty($_POST['edit_device_id'])) {
$get_selected_Devices = e($_POST['edit_device_id']);
$get_selected_Devices_s = e($_POST['edit_device_serial']);
$get_selected_Devices_i = e($_POST['edit_device_imei']);
}

unable to get checkbox values

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

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];
}
?>

How to get the input value from multiple value when button pressed php

i want to make a cart and my product is pizza.
so i'm listing all the menu to the table, and add a button to submit menu to cart.
here's my index.php
<?php
require("connect.php");
$result = mysqli_query($con,'select * from menu');
?>
<form action="cart.php" method="get">
<table border="1" style="width:100%">
<tr>
<th>Pizza Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Buy</th>
</tr>
<?php while($product = mysqli_fetch_object($result)){ ?>
<tr>
<td><?php echo $product->nama_menu; ?></td>
<td><?php echo $product->harga_menu; ?></td>
<td><input type="number" name="quantity" min="1" max="20"></td>
<td><button type="submit" name="id" value="<?php echo $product->id_menu; ?>">Add To Cart</button></td>
</tr>
<?php } ?>
</table>
</form>
But when i pressed the button "Add To Cart", it sends all the quantity from the menu listing and can't be read in my cart.php
can anyone help me how to get the right quantity value when i pressed the button add to cart.
Make separate form for each of the item. Try below code.
<?php
require("connect.php");
$result = mysqli_query($con,'select * from menu');
?>
<table border="1" style="width:100%">
<tr>
<th>Pizza Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Buy</th>
</tr>
<?php while($product = mysqli_fetch_object($result)){ ?>
<form action="cart.php" method="get">
<tr>
<td><?php echo $product->nama_menu; ?></td>
<td><?php echo $product->harga_menu; ?></td>
<td><input type="number" name="quantity" min="1" max="20"></td>
<td><button type="submit" name="id" value="<?php echo $product->id_menu; ?>">Add To Cart</button></td>
</tr>
</form>
<?php } ?>
</table>
Try to use array in name then submit it will give you seperate quantity.
<td><input type="number" name="quantity[<?php echo $product->nama_menu; ?>]" min="1" max="20">
Output:
quantity['pizza1'] = 10
quantity['pizza2'] = 20
....
Another Option is use dynamic name for number.
<td><input type="number" name="quantity_<?php echo $product->nama_menu; ?>" min="1" max="20">
Output:
quantity_pizza1 = 10
quantity_pizza2 = 20
....

how to save radio button has been selected at the time the page is reloaded?

how to save radio button has been selected at the time the page is reloaded? suppose I have selected some of the radio button and then reload the browser page. but after the page is reloaded radio buttons have been still the same. I use Codeigniter.
here is my view code
<div class="container">
<?php
$no=1;
foreach($hasil->result() as $row):
?>
<form action="<?php echo base_url();?>mahasiswa/hasil" method="post" class="form" enctype="multipart/form-data" name="form" onsubmit="stopCounter();">
<input type="hidden" name="id_soal[<?php echo $row->id_soal;?>]" value="<?php echo $row->id_soal;?>" />
<table>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td><b><?php echo $no.'. ';?></b></td>
<td><?php echo $row->pertanyaan;?></td>
</tr>
<tr>
<td rowspan="5"> </td>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="A" />a. <?php echo $row->a;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="B" />b. <?php echo $row->b;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="C" />c. <?php echo $row->c;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="D" />d. <?php echo $row->d;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="E"/>e. <?php echo $row->e;?></label></span></td>
</tr>
<?php
$no++;
?>
<input type="hidden" name="id_sesi" value="<?php echo $row->id_sesi;?>" />
<input type="hidden" name="jumlah" value="<?php echo $jumlah;?>" />
<?php
endforeach;
?>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Selesai" class="btn btn-default"/></td>
</tr>
</table>
</form>
</div>
here my controller code
public function mulai_tes(){
$id=$this->uri->segment(3);
$this->load->model('m_mahasiswa');
$result=$this->m_mahasiswa->data_mahasiswa();
foreach($result->result() as $row):
$data['user']=$row->username;
$data['nama']=$row->nama;
endforeach;
$cek=$this->m_mahasiswa->validasi_tes($id,$data['user']);
foreach($cek->result() as $c){
if($c->id_sesi==$id){ ?>
<script type="text/javascript" language="javascript">
alert("Anda telah mengikuti tes soal ini");
</script>
<?php
echo "<meta http-equiv='refresh' content='0; url=".base_url()."mahasiswa/tes'>";
}
}
$data['hasil']=$this->m_mahasiswa->mulaites($id);
$data['jumlah']=$data['hasil']->num_rows();
$data['judul']='Mulai Tes';
$this->load->view('elearning/template',$data);
}
How I set cookie and where I can put cookie code in controller?
Check which radio(s) was selected in controler (http://ellislab.com/codeigniter%20/user-guide/libraries/input.html).
Pass this data to view (http://ellislab.com/codeigniter/user-guide/general/views.html).
Mark radio(s) as selected within view depending on data passed by controller (Assign an initial value to radio button as checked).

Categories