I have some issue about the inserting data. It will insert only the waybillno data but the quantity is always same. Please check my code - I think the model is wrong.
Controller
public function create_cargo_manifest(){
$core_model = new Core_m;
$core_model->save_cargo_details($this->input->post());
redirect('core/cargo_lookup/');
}
Model
function save_cargo_details(){
$quantity = $this->input->post('quantity');
$waybilldate = $this->input->post('waybilldate');
$data = array();
foreach($this->input->post('sys_wbdetails') as $sys_wbdetails) {
$data[] = array(
'waybillno' => $sys_wbdetails,
'quantity' => $quantity,
'waybilldate' => $waybilldate,
);
}
return $this->db->insert_batch('sys_cargodetails', $data);
}
View
<?php foreach($waybill_header as $waybill_header) { ?>
<?php echo form_open('core/create_cargo_manifest'); ?>
<td><input type="checkbox" name="sys_wbdetails[]" value="<?php echo $waybill_header->waybillno; ?>"></td>
<td><?php echo $waybill_header->waybillno; ?></td>
<td><?php echo $waybill_header->waybilldate; ?><input type="hidden" value="<?php echo $waybill_header->waybilldate; ?>" name="waybilldate"></td>
<td><input type="text" size="5" value="<?php echo $waybill_header->quantity; ?>" name="quantity"></td>
<td><input type="submit" value="save"></td>
<?php } ?>
<?php form_close(); ?>
All your inputs for quantity have the same name : quantity. So you're only submitting the last value in your form. You need to use an array for those inputs (quantity[]), just like for your checkboxes. And you might want to do the same for the waybilldate inputs.
<td><input type="text" size="5" value="<?php echo $waybill_header->quantity; ?>" name="quantity[]"></td>
And then in PHP, something like that :
$data = array();
// Count distinct entries in the form
$count = count($this->input->post['sys_wbdetails']);
for($i=0; $i < $count; $i++) {
$data[] = array(
'waybillno' => $this->input->post['sys_wbdetails'][$i],
'quantity' => $this->input->post['quantity'][$i],
'waybilldate' => $this->input->post['waybilldate'][$i],
);
}
EDIT : also, take a look at this answer if you want a clean way to keep track of which form input goes where.
Related
This is my view Page
This is Dynamically fields Test Name and units are from Table I need to insert Test name and Result and Normal value also Unit.
My Controller
$patient_id = $this->input->post('patient_id');
$doctor_id = $this->input->post('doctor_id');
$prescription_id = $this->input->post('prescription_id');
$lab_result =$this->input->post('lab_result');
$lab_test =$this->input->post('lab_test');
$units =$this->input->post('units');
$normal_value =$this->input->post('normal_value');
$cat_id = $this->input->post('cat_id');
for($i=0; $i<count($prescription_id); $i++)
{
$labreport[] = array(
'patient_id' => $patient_id[$i],
'doctor_id' => $doctor_id[$i],
'prescription_id' =>$prescription_id[$i],
'lab_result' => $lab_result[$i],
'lab_test' => $lab_test[$i],
'units' => $units[$i],
'normal_value' => $normal_value[$i],
'cat_id' => $cat_id, );
//echo '<pre>'; print_r($labreport); '</pre>'; exit;
}
$stringlabreport= json_encode($labreport);
$this->db->insert('patient_lab_report',$stringlabreport);
if($this->db->affected_rows()){
return true;
} else {
return false;
}
$this->session->set_flashdata('message','Lab Report Added Successfully');
redirect('laboratory_report/all');
=========================
And This is My View Code
<tr>
<td><input type="hidden" value="<?php echo $data->name; ?>"name="lab_test[]"><?php echo $data->name; ?></td>
<td><input type="text" value="" name="lab_result[]" class="form-control"></td>
<td><input type="hidden" value="<?php echo $data->units; ?>" name="units[] "> <?php echo $data->units; ?></td>
<td><input type="hidden" value="<?php echo $data->n_value; ?>" name="normal_value[] "> <?php echo $data->n_value; ?></td>
</tr>
Use array names for input. I.E.:
<form action="" method="post" name="someform">
<input name="row[0][test_name]"/>
<input name="row[0][result]"/>
<input name="row[1][test_name]"/>
<input name="row[1][result]"/>
<input type="submit" name="submitted" value="send">
</form>
So upon submission, parsing in php, use filter_input with FILTER_REQUIRE_ARRAY as option:
<?php
$submitted = filter_input(INPUT_POST, 'submitted');
if(!empty($submitted)){
$rows = filter_input(INPUT_POST,'row', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
$parsed_rows = [];
foreach($rows as $row){
if(!empty($row['test_name']) && !empty($row['result'])){
$parsed_rows[] = $row;
}
}
}
?>
I am trying to create an input element of type radio within a foreach loop and save the values in a database when the submit button is clicked. I'm using Codeigniter framework.
My views:
<?php echo form_open('students_report/produce_aptitude_score/'.$y->id); ?>
<table>
<tbody>
<?php
$i = 1;
foreach ($aptitudes as $p) { ?>
<input type="hidden" name="aptitude[]" value="<?php echo $p->aptitude; ?>" />
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $p->aptitude; ?></td>
<td>
<div class="form-group">
<input class="form-control" type="radio" name="score[<?php echo $i; ?>]" value="1" />
</div>
</td>
<td>
<div class="form-group">
<input class="form-control" type="radio" name="score[<?php echo $i; ?>]" value="2" />
</div>
</td>
</tr>
<?php $i++; //increment index
} //endforeach ?>
</tbody>
</table>
<button class="btn btn-success btn-lg">Submit</button>
<?php echo form_close(); ?>
My Controller
public function produce_aptitude_score($id) {
$y = $this->common_model->get_student_details_by_id($id);
$this->form_validation->set_rules('aptitude[]', 'Aptitude', 'trim');
$aptitude = $this->input->post('aptitude', TRUE);
$score = $this->input->post('score', TRUE);
if ($this->form_validation->run()) {
for ($i = 0; $i < count($aptitude); $i++) {
$d_aptitude = $aptitude[$i];
$d_score = $score[$i];
$query = $this->students_report_model->check_aptitude_score_exists($id, $d_aptitude);
if ($query->num_rows() == 0) { //data does not exists, do insert
$this->students_report_model->insert_aptitude_score($id, $d_aptitude, $d_score);
} else { //data already exists, do update
$this->students_report_model->update_aptitude_score($id, $d_aptitude, $d_score);
}
}
$this->session->set_flashdata('status_msg', "Aptitude score submitted successfully for {$y->first_name}");
redirect($this->agent->referrer());
} else {
$this->produce_report($id); //form validation fails, reload page with errors
}
}
My Model
public function insert_aptitude_score($id, $aptitude, $score) {
$y = $this->common_model->get_student_details_by_id($id);
$data = array(
'admission_id' => $y->admission_id,
'aptitude' => $aptitude,
'score' => $score,
'session' => current_session,
'term' => current_term,
);
return $this->db->insert('aptitude_scores', $data);
}
public function update_aptitude_score($id, $aptitude, $score) {
$y = $this->common_model->get_student_details_by_id($id);
$query = $this->check_aptitude_score_exists($id, $aptitude);
$result_id = $query->row()->id;
$data = array(
'aptitude' => $aptitude,
'score' => $score,
);
$this->db->where('id', $result_id);
return $this->db->update('aptitude_scores', $data);
}
When I submit, why do I get the following error?
PHP: Undefined offset: 0
Database: Column 'score' cannot be null
I want to update the data using array and checkbox. If the checkbox checked, status become "1". Else, leave it "0".
I have try something like this
<?php
foreach($report as $r){;
?>
<input type="checkbox" name="status[]" value="1" value="<?php echo $r->status;?>">
<input type="hidden" name="id_name[]" value="<?php echo $r->id_name;?>">
<input type="hidden" name="name[]" value="<?php echo $r->name;?>">
<?php } ?>
and this
<input type="checkbox" name="status[]" value="1">
<input type="hidden" name="id_name[]" value="<?php echo $r->id_name;?>">
<input type="hidden" name="name[]" value="<?php echo $r->name;?>">
But both of them update the first row even I check the third or the fourth row.
my controller is something like this
function update_approval() {
$status = $this->input->post('status');
$id_name = $this->input->post('id_name');
$name = $this->input->post('name');
for($a=0; $a< sizeof ($id_name); $a++) {
$data[$a] = array(
'status' => $status[$a],
'id_name' => $id_name[$a],
'name' => $name[$a]
);
}
$this->db->update_batch('tbl_m_name', $data, 'id_name');
}
Thanks in advance
I've solved it. Thanks,
I change the position, here is my views
<input type="checkbox" name="id_name[]" value="<?php echo $r->id_name;?>">
<input type="hidden" name="name[]" value="<?php echo $r->name;?>">
and here is my controller
function update_approval() {
$id_name = $this->input->post('id_name');
$name = $this->input->post('name');
$data[$a] = array();
for($a=0; $a< sizeof ($id_name); $a++) {
$data[] = array(
'status' => 1,
'id_name' => $id_name[$a],
'name' => $name[$a]
);
}
$this->db->update_batch('tbl_m_name', $data, 'id_name');
}
so if I check the box, it will post the id_name that I choose
thanks, but I had tried yours, I get undefinied variable $a. So, I changed the position and removed $data[$a]= array(); . Below is my code (after resolve my problem) based on yours:
function update_approval() {
$id_name = $this->input->post('id_name');
$name = $this->input->post('name');
for($a=0; $a< sizeof ($id_name); $a++) {
$data[$a] = array(
'status' => 1,
'id_name' => $id_name[$a],
'name' => $name[$a]
);
}
$this->db->update_batch('tbl_m_name', $data, 'id_name');
}
It's my first time posting a question:
I'm working on a WordPress plugin that allows the user to create rows of data in the database. I'm experiencing a problem when there are many (upwards of 100) rows of data to be updated by a form. Each row of data holds eight POST data variables, so when there are 100 rows in the form, over 800 post variables are sent. However, only a certain number of the variables update the database, right now only 112 rows update. I can't figure out what would stop the function from completing the update to the database. It almost seems like I'm overloaded with too many post variables or post data size?
Everything works perfectly with fewer entries, but once it goes over 100 rows, things stop working.
Here is my table structure:
$sql2 = "CREATE TABLE IF NOT EXISTS $item_table (
id smallint(5) NOT NULL AUTO_INCREMENT,
menu smallint(5) NOT NULL,
itemorder smallint(5) NOT NULL,
item text NOT NULL,
description text,
image tinytext NOT NULL,
value tinytext NOT NULL,
value2 tinytext NOT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
}
Here is my POST data handler function:
foreach($_POST['id'] as $i){
$image = $_POST['image'][$i];
$item = $_POST['item'][$i];
$desc = $_POST['desc'][$i];
$value = $_POST['value'][$i];
$value2 = $_POST['value2'][$i];
$order = $_POST['order'][$i];
if ($_POST['strike'][$i] == 'checked' ){
$wpdb->query( $wpdb->prepare("DELETE FROM $item_table WHERE id = $i") );
}
else{
$wpdb->update( $item_table, array(
'image' => $image,
'item' => $item,
'itemorder' => $order,
'description' => $desc,
'value' => $value,
'value2' => $value2
),
array( 'id' => $i ) );
}
}
//Sort items by order, then rewrite the order with no gaps left from deleted items
$targetmenu = $_POST['targetmenu'];
$rows = "SELECT * FROM $item_table WHERE menu = $targetmenu ORDER by itemorder ASC";
$result = $wpdb->get_results($rows);
$n = 1;
foreach ($result as $r){
$id = $r->id;
$wpdb->update( $jsrm_item_table , array( 'itemorder' => $n ), array( 'id' => $id ) );
++$n;
}
$loc = "&mode=editmenu&targetmenu=".$targetmenu;
header("Location:".JSRM_SELF.$loc);
exit();
}
And here is my PHP Form:
$the_menu = $wpdb->get_row("SELECT * FROM $menu_table WHERE id = $_GET[targetmenu]");
$menuid = $the_menu->id;
$q = "SELECT * FROM $item_table WHERE menu = $menuid ORDER by itemorder ASC";
$result = $wpdb->get_results($q);
if ($result) {
?>
<form id="edit-menu-form" action="<?php echo _SELF; ?>" method="post">
<input type="hidden" name="targetmenu" value="<?php echo $menuid; ?>">
<input type="hidden" name="dbtouch" value="updateitems">
<table>
<?php
foreach ($result as $r) {
$order = $r->itemorder;
$image = $r->image;
$imagesrc = ($image) ? esc_html(stripslashes($r->image)) : 'addimage.jpg';
$item = esc_html(stripslashes( $r->item ));
$description = esc_html(stripslashes($r->description));
$value = esc_html(stripslashes($r->value));
$value2 = esc_html(stripslashes($r->value2));
$id = $r->id;
?>
<tr id="<?php echo $id ?>">
<td><?php echo $order ?></td>
<td><a class="edit-item-img" id="item-image-<?php echo $id ?>" style="background-image:url(<?php echo $imagesrc ?>);" title="Edit image"></a>
<input type="hidden" name="image[<?php echo $id ?>]" id="field-item-image-<?php echo $id ?>" value="<?php echo $image ?>" />
<img class="remove-image-button" id="image-<?php echo $id ?>" src="removeimage.png"
<?php if(!$image){ ?>
style="visibility:hidden;"
<?php } ?>
/>
</td>
<td><textarea name="item[<?php echo $id ?>]"><?php echo $item ?></textarea></td>
<td><textarea name="desc[<?php echo $id ?>]"><?php echo $description ?></textarea></td>
<td><input type="text" name="value[<?php echo $id ?>]" value="<?php echo $value ?>" /></td>
<td><input type="text" name="value2[<?php echo $id ?>]" value="<?php echo $value2 ?>" /></td>
<td><input type="checkbox" class="strike" name="strike[<?php echo $id ?>]" value="checked"/></td>
<input type="hidden" name="order[<?php echo $id ?>]" value="<?php echo $order ?>" id="order<?php echo $id ?>"/>
<input type="hidden" name="id[<?php echo $id ?>]" value="<?php echo $id ?>" id="id<?php echo $id ?>"/>
</tr>
<?php
}
?>
</table/>
<p><input type="submit" id="update-items-button" value="Update All" class="button-primary"/></p>
</form>
<?php
}
?>
I had a similar problem today. I had a form with 250+ rows and 5 variables per row, but the $_POST variable appeared to be truncated. In my case, it stopped after 1000 elements.
There is a PHP setting called max_input_vars that defaults to 1000. This setting sets an upper limit on how many variables it will pull into your PHP script. You may need to increase this value on your server settings to make your page work. There are some security implications that I don't fully understand with increasing this value that could enable a denial of service attack.
Since you are developing a Wordpress plugin, you may need to see if there are ways to change your form to reduce the number of variables you send, because you probably can't alter server configurations for people using your plugin.
Read more about the setting here: http://www.php.net/manual/en/info.configuration.php#ini.max-input-vars
I am developing an ordering page in PHP for some products. The user needs to be able to select multiple types of products, so checkboxes will be necessary in the HTML form.
I've established an array for the checkboxes via the "name" attribute.
My problem is, I want to display what the user has selected on a confirmation page, so I'd like the "value" of those checkboxes to ultimately return a product name AND a product price. As far as I can tell I'm going to be stuck with one value only, so I've attempted to get around this:
<?php
//variables for the array and the count
$parray = $_POST['product'];
$pcount = count($parray);
//arrays for each product's information; i've only listed one for simplicity
$br = array("Big Red", 575);
/*The idea for this was for each product ordered to add a line giving the product information and display it. When the $key variable is assigned, it is stored simply as a string, and not an array (yielding [0] as the first letter of the string and [1] as the second), which I expected, but my general idea is to somehow use $key to reference the above product information array, so that it can be displayed here*/
if (!empty($parray))
{
for ($i=0; $i < $pcount; $i++)
{
$key = $parray[i];
echo "<tr><td height='40'></td><td>" . $key[0] . "</td><td>" . $key[1] . "</td></tr>";
}
}
?>
Is there anyway to make my $key variable actually act as if it is the array's name it is set to?
If not, is there any good way to do this?
So in your HTML table, you'll need to render each checkbox like this:
<input type="checkbox" name="selectedIDs[]" value="$key" />
where $key is the item number.
Then, in your PHP, you'll have a $_POST["selectedIDs"] variable, which is an array of all the item numbers that were checked.
Assuming you've got a product list array like this:
$products = array( 1 => array("Big Red", 575), 2 => array("Spearmint", 525));
You can then print a list of the selected products using a loop like this:
for($i = 0; $i < count($_POST["selectedIDs"]); $i++) {
$key = $_POST["selectedIDs"][$i];
$product = $products[$key];
echo "<tr><td height='40'></td><td>" . $product[0] . "</td><td>" . $product[1] . "</td></tr>";
}
The only real difference between this and what you wrote is that my $products array is two-dimensional, and my $key is used to grab the relevant product from the $products array.
You can give the value of the options the value of the ID of the corresponding item. Then you'll receive an array of ID's, which you can then again map to the $br array, where you look up the name and price of an item by its ID. You could use the array key as ID, so:
<!-- Form: -->
<input type="checkbox" name="product[]" value="1" />
<input type="checkbox" name="product[]" value="..." />
<input type="checkbox" name="product[]" value="15" />
[...]
<?php
$product = array();
$product[1] = array('name' => "Big Red", 'price' => 575);
$product[...] = array('name' => "...", 'price' => ...);
$product[15] = array('name' => "Small Green", 'price' => 475);
foreach ($_POST['product'] as $pId)
{
echo $products[$pId]['name'] . " costs " . $products[$pId]['price'];
}
?>
If of course your array originates from a database, you can use the table's ID's as values for the checkboxes. You could then implode the $_POST['product'] (of course after escaping it) with a comma, and use it in a SELECT ... WHERE ID IN ($productIds) query.
If i'm understanding you correctly, you'll have checkboxes something like this:
<input type="checkbox" name="product_id[]" value="1">Product #1<br/>
<input type="checkbox" name="product_id[]" value="2">Product #2<br/>
<input type="checkbox" name="product_id[]" value="3">Product #3<br/>
That should post whatever is checked as an array to your server.
gen_paper1.php
<div style="overflow: auto; height: 500px; border: 1px solid;">
<form action="" method="post">
<fieldset style="font-size: 15px; font-weight: bolder;">
<h4><u>Your Existing Header's are listed below</u> </h4>
<hr / >
<?php
$xhdr = mysql_query("SELECT * from s_user_header ORDER BY 'ASC'");
while($rheader = mysql_fetch_array($xhdr)){
$h = $rheader['h_content'];
$hid = $rheader['h_id'];?>
<script>
$(document).ready(function(){
//$('input[type=checkbox]').val('<?php echo $hid ; ?>').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
//$('#c').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
$('').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
});
</script>
<table>
<td><input type="checkbox" id="c" name="u_hdr[]" value="<?php echo $hid; ?>"></td>
<td style="font-weight: bolder"><?php echo $h."<br />"; ?></td>
</table>
<?php } ?>
</fieldset>
</div>
gen_paper2.....3.....
i need all this values in further page, so have taken hidden variables
<input type="hidden" name="hstdid" value="<?php echo $stdid; ?>">
<input type="hidden" name="hsubid" value="<?php echo $subid; ?>">
<input type="hidden" name="hdifid" value="<?php echo $difid; ?>">
<input type="hidden" name="hmarks" value="<?php echo $t_marks; ?>">
<input type="hidden" name="h_hid[]" value="<?php echo $hh; ?>">
gen_paper4.php
$getstdid = $_POST['hstdid3'] ;
$getsubid = $_POST['hsubid3'] ;
$getdifid = $_POST['hdifid3'] ;
$gethmarks = $_POST['htmarks'] ;
$hdr= $_POST['h_hdrid'] ;
$h = implode($hdr);
<table>
<?php
$h1 = explode(",",$h);
count($h1) . "<br />";
for($i=0;$i<count($h1);$i++){
$h1[$i];
$xheader = mysql_query("SELECT * from s_user_header WHERE h_id = ".$h1[$i]);
while($row = mysql_fetch_array($xheader)){ ?>
<tr>
<td><b>
<?php echo $i + 1 . ".   "; ?>
</b></td><td>
<?php echo $header = $row['h_content'] . "<br />";
?></td>
</tr>
<?php
}
}
//echo $header; ?>
</table>