UPDATED. I'm having problem about inserting multiple checkbox data in the database.
Here is my view
<?php
$i = 0;
$qArr = array();
$qArrc = 0;
$qArr1 = array();
$qArrc1 = 0;
$uArr = array();
$u = 0;
foreach($questions->result() as $q){ ?>
<input type="checkbox" name="check[<?php echo $i; ?>]" value="<?php echo $qArr[$qArrc++] = $q -> questions; ?>"> <?php echo $q->questions; ?> <br>
<input class="form-control" value="<?php echo $uArr[$u++] = $this->session->userdata('user_id'); ?>" name="hidden[<?php echo $i; ?>]" type="hidden">
<input class="form-control" value="<?php echo $qArr1[$qArrc1] = $q->id; ?>" name="hidden1[<?php echo $i; ?>]" type="hidden">
<?php
$i++;
}?>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
Here's my controller
for($i = 0; $i<count($this->input->post('check')); $i++){
$data1 = array(
'question' => $this->input->post('check')[$i],
'speaker_id' => $this->input->post('hidden')[$i],
'question_id' => $this->input->post('hidden1')[$i]
);
$this->input->post('submit');
$this->Speaker_Model->insert_speakerfeedback($data1);
}redirect('speaker/createfeedback');
Here's my model
public function insert_speakerfeedback($data1){
$this->db->insert("speakerdata", $data1);
}
Modify your for loops :
for($i = 0; $i<count($this->input->post('check')); $i++){
to use foreach instead` :
foreach ($this->input->post('check') as $i => $value) {
So it will prevent getting undefined index if you skip some checkbox. And the redirect line should be outside of the loops.
For bulk insert, you could use insert_batch function.
Within the loop, change :
$data1 = array(
to
$data1[] = array(
So it will not overwriting on each iteration.
And on the Model, change :
$this->db->insert("speakerdata", $data1);
to :
$this->db->insert_batch("speakerdata", $data1);
Related
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
Hey I'm currently having a problem with trying to make my input button have a value of the id / server that I need and it showing a different value this is how my code currently looks I know the HTML and forms are invalid I will be refactoring it however I'm trying to get a server identifier and a id identifier to cross to another page
<?php
$propertyType = $xmlDom1->getElementsByTagName('PropertyType');
$rent = $xmlDom1->getElementsByTagName('rates');
$rooms = $xmlDom1->getElementsByTagName('rooms');
//$server = $xmlDom1->getElementsByTagName('server');
$propertyServer = $xmlDom1->getElementsByTagName('Property');
$propertyID = $xmlDom1->getElementsByTagName('Property');
$imageURL = $xmlDom1->getElementsByTagName('url');
$imageAlt = $xmlDom1->getElementsByTagName('altText');
$server = $propertyID->item($i)->getAttribute('server');
echo '<form action="level5Details.php" method="get" enctype="application/x-www-form-urlencoded">';
echo '<table><th>Type:</th><th>Rent:</th><th>Rooms:</th><th>Server</th>';
$records = $xmlDom1->documentElement->childNodes;
for ($i = 0; $i < $records->length; $i++) {
echo "<tr><td>".$propertyType->item($i)->nodeValue."</td>";
echo "<td>".$rent->item($i)->nodeValue."</td>";
echo "<td>".$rooms->item($i)->nodeValue."</td>";
echo "<td>".$propertyServer->item($i)->getAttribute('Server')."</td>";
echo '<td><img src="data:image/jpeg;base64,'.$imageURL->item($i)->nodeValue.'" alt="'.$imageAlt->item($i)->nodeValue.'"></img></td>';
?>
<td>
<button class="submit" type="submit" value="<?php echo $propertyID->item($i)->getAttribute('pid'); ?>" name="submit22">something </button>
</td>
</tr>
<?php
}
?>
</form>
If you are trying to generate lots of form each with just a button then you need to put the form in a table cell like this
It might also be useful to place the data items you want to pass in hidden fields rather than try and put 2 data items in the button value
<?php
$propertyType = $xmlDom1->getElementsByTagName('PropertyType');
$rent = $xmlDom1->getElementsByTagName('rates');
$rooms = $xmlDom1->getElementsByTagName('rooms');
//$server = $xmlDom1->getElementsByTagName('server');
$propertyServer = $xmlDom1->getElementsByTagName('Property');
$propertyID = $xmlDom1->getElementsByTagName('Property');
$imageURL = $xmlDom1->getElementsByTagName('url');
$imageAlt = $xmlDom1->getElementsByTagName('altText');
$server = $propertyID->item($i)->getAttribute('server');
echo '<table><th>Type:</th><th>Rent:</th><th>Rooms:</th><th>Server</th>';
$records = $xmlDom1->documentElement->childNodes;
for ($i = 0; $i < $records->length; $i++) {
echo "<tr><td>".$propertyType->item($i)->nodeValue."</td>";
echo "<td>".$rent->item($i)->nodeValue."</td>";
echo "<td>".$rooms->item($i)->nodeValue."</td>";
echo "<td>".$propertyServer->item($i)->getAttribute('Server')."</td>";
echo '<td><img src="data:image/jpeg;base64,'.$imageURL->item($i)->nodeValue.'" alt="'.$imageAlt->item($i)->nodeValue.'"></img></td>';
$pid = $propertyID->item($i)->getAttribute('pid');
?>
<td>
<form action="level5Details.php" method="get" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="pid" value="<?php echo $pid;?>">
<input type="hidden" name="server" value="<?php echo $server;?>">
<button class="submit" type="submit" value="submit" name="submit22">something</button>
</form>
</td>
</tr>
<?php
}
?>
If you just want the button to carry all the data and have only one form you will have to package the 2 data items into one string and sent it as the value of the button
<?php
$propertyType = $xmlDom1->getElementsByTagName('PropertyType');
$rent = $xmlDom1->getElementsByTagName('rates');
$rooms = $xmlDom1->getElementsByTagName('rooms');
//$server = $xmlDom1->getElementsByTagName('server');
$propertyServer = $xmlDom1->getElementsByTagName('Property');
$propertyID = $xmlDom1->getElementsByTagName('Property');
$imageURL = $xmlDom1->getElementsByTagName('url');
$imageAlt = $xmlDom1->getElementsByTagName('altText');
$server = $propertyID->item($i)->getAttribute('server');
echo '<form action="level5Details.php" method="get" enctype="application/x-www-form-urlencoded">';
echo '<table><th>Type:</th><th>Rent:</th><th>Rooms:</th><th>Server</th>';
$records = $xmlDom1->documentElement->childNodes;
for ($i = 0; $i < $records->length; $i++) {
echo "<tr><td>".$propertyType->item($i)->nodeValue."</td>";
echo "<td>".$rent->item($i)->nodeValue."</td>";
echo "<td>".$rooms->item($i)->nodeValue."</td>";
echo "<td>".$propertyServer->item($i)->getAttribute('Server')."</td>";
echo '<td><img src="data:image/jpeg;base64,'.$imageURL->item($i)->nodeValue.'" alt="'.$imageAlt->item($i)->nodeValue.'"></img></td>';
$dataPackage = $propertyID->item($i)->getAttribute('pid') . ':' . $server;
?>
<td>
<button class="submit" type="submit" value="<?php echo $dataPackage;?>" name="submit22">something</button>
</td>
</tr>
<?php
}
</table>
</form>
?>
Now in the receiving form unpack the data for use
<?php
// usual checks for things existing
list($pid, $server) = explode(':', $_GET['submit22'];
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');
}
I have a loop that iterates 3 times. Inside the loop I have a HTML form that has radio buttons. I'm processing the input using PHP. When I echo the form data, its not showing the correct values. Is it a wrong way of processing the data ? Any help is appreciated.
test.php
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<form action = 'test.php' method = 'post'>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
</form>
<?php
}
?>
<input type = 'submit' value = 'Go'>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
Move your form outside of your for loop. You are currently creating three forms with one submit button (which isn't attached to any of them).
try this way
<form action = 'test.php' method = 'post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
use following code: Or you can use radiogroup array it is easier than this.
<form action = 'test.php' method = 'post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
#Cagy79 added additional submit buttons.. I have revised the code,
<form action ="" method='post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
This works. :)
You have 3 forms without a submit button (it needs to be in between the tags). You need to create one form with one submit button like this so all your data is posted to $_POST.
<form method='post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
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.