Im confused. I have been analyzing this for hours..
What I want:
"EACH table row with the checkbox thats checked would be updated with the value inputted."
Anyone can help?
thank you,
Leo
Admin.php
function submit_acc(){
$this->access_chk(); //CHECK USER ACCESS
$this->form_validation->set_error_delimiters(' ', '<br />');
$checkbox=$this->input->post('cek');
$sbm=$this->input->post('sbm');
if($sbm == "Update" && $checkbox!=false){
foreach ($checkbox as $row_id) {
// echo($this->input->post('bank_accname'));
$this->form_validation->set_rules("type", "bank name", "required");
$this->form_validation->set_rules("isactive", "Status Bank", "required");
if(!$this->form_validation->run()){ exit(validation_errors()); }
echo "update x where y =$row_id";
echo $_POST["accgroup"].$_POST["type"].$this->input->post('bank_accname').$_POST["bank_accnumber"].$_POST["isactive"];
$data[] = array (
"acc_id" => $row_id,
"acc_accgroup_id" => $_POST["accgroup"],
"acc_bank_name" => $_POST["type"],
"acc_bank_accname" => $this->input->post('bank_accname'),
"acc_bank_accnumber" => $_POST["bank_accnumber"],
"acc_isactive" => $_POST["isactive"]
);
$uid=$row_id;
// echo $uid;
}
//var_dump($data);
//var_dump($checkbox);
$this->db->update_batch('acc', $data, 'acc_id');
exit("end");
View.php
<form action="<?php echo site_url("admin/submit_acc"); ?>" method="post" id="form">
<table>
<tr >
<td align="center"><?php echo $i; ?></td>
<input type="hidden" name="id[]" value="<?php if(isset($r)) echo $r["acc_id"]; ?>" />
<input type="hidden" name="isactive[]" value="<?php if(isset($r)) echo $r["acc_isactive"]; ?>" />
<td><input type="text" class="textbox" style="width:200px;" value="<?php if(isset($r)) echo $r["acc_bank_name"]; ?>" name="type[]" id="type" /></td>
<td><input type="text" class="textbox" style="width:200px;" value="<?php if(isset($r)) echo $r["acc_bank_accname"]; ?>" name="bank_accname[]" id="bank_accname" /></td>
<td><input type="text" class="textbox" style="width:200px;" value="<?php if(isset($r)) echo $r["acc_bank_accnumber"]; ?>" name="bank_accnumber[]" id="bank_accnumber" /></td>
<!--<input type='radio' name="isactive_<?php echo $r['acc_id'];?>" value=1
<?php if ($r["acc_isactive"]=='1') echo 'checked'; ?> > -->
G
<select name="accgroup[]">
<option value='<?php echo $r["accgroup_no"]; ?>' id="account_id"><?php echo $r['accgroup_no']; ?></option>
<?php echo $stropt; ?></select>
<!-- <input type='radio' name='isactive_<?php echo $r['acc_id'];?>' value=0 <?php if ($r["acc_isactive"]=='0')echo 'checked'; ?>> Off -->
" >
</table>
</form>
MY TRY AND ITS NOT WORKING : if I dont select the first row, the second row wont be updated . the point is my checkbox should be checked all to function properly. if not, it would start updating the rows according to numbers of checked checkboxes. Can somebody please tell me what did I miss?????
$data=array();
$data['checkbox']=$this->input->post('cek');
$data['id']=$this->input->post('id',true);
$data['accgroup']=$this->input->post('accgroup',true);
$data['type']=$this->input->post('type',true);
$data['bank_accname']=$this->input->post('bank_accname',true);
$data['bank_accnumber']=$this->input->post('bank_accnumber',true);
$data['isactive']=$this->input->post('isactive',true);
$sbm=$this->input->post('sbm');
if($sbm == "Update" && $data['checkbox']!=false){
foreach ($data['checkbox'] as $key => $each) {
// echo($this->input->post('bank_accname'));
echo $data["accgroup"][$key];
$this->form_validation->set_rules("type", "bank name", "required");
$this->form_validation->set_rules("isactive", "Status Bank", "required");
if(!$this->form_validation->run()){ exit(validation_errors()); }
$temp[] = array (
"acc_id" => $data["id"][$key] ,
"acc_accgroup_id" => $data["accgroup"][$key],
"acc_bank_name" => $data["type"][$key],
"acc_bank_accname" => $data['bank_accname'][$key],
"acc_bank_accnumber" => $data["bank_accnumber"][$key],
"acc_isactive" => $data["isactive"][$key]
);
$uid=$key;
// echo $uid;
}
var_dump($temp);
//var_dump($checkbox);
$this->db->update_batch('acc', $temp, 'acc_id');
exit("end");
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 insert multiple rows into my database table using codeigniter insert_batch. base on the error report it seems like the table columns are not set. just the number of arrays:
My view:
<table class="table">
<center><h3> Activity Name: <?php echo $activity_name; ?></h3><h4> Date: <?php echo $activity_date; ?></h4></center>
<tr>
<td>Member Name and Course</td>
<td>Attendance</td>
<td>fee</td>
</tr>
<?php
$count = 1;
$member_count = 1;
foreach ($students as $row) {
$uID = $row['user_ID'];
$cID = $row['council_ID'];
$aID = $row['activity_ID'];
$name = $row['user_LastName'].", ".$row['user_FirstName'];
$course = $row['user_Course'];
$year = $row['user_Year'];
?>
<tr>
<td>
<h4> <?php echo $member_count.". ".$name; ?></h4>
<h5> <?php echo $course." - ".$year; ?></h5>
</td>
<?php echo form_open('Isidran/InsertAttendance'); ?>
<input type="hidden" id="uID<?php echo $count; ?>" name="uID[]" value="<?php echo $uID;?>">
<input type="hidden" id="cID<?php echo $count; ?>" name="cID[]" value="<?php echo $cID;?>">
<input type="hidden" id="aID<?php echo $count; ?>" name="aID[]" value="<?php echo $aID;?>">
<input type="hidden" id="aname<?php echo $count;?>" name="aname[]" value="<?php echo $activity_name; ?>">
<td><select id="attendance<?php echo $count; ?>" name="attendance[]" class="form-control">
<option value="Absent"> Absent </option>
<option value="Present"> Present </option>
</select>
</td>
<input type="hidden" id="name<?php echo $count;?>" name="name[]" value="<?php echo $name;?>">
<input type="hidden" id="fines<?php echo $count; ?>" name="fines[]" value="<?php echo $activity_fee; ?>">
<td><div id="fee<?php echo $count; ?>"><?php echo $activity_fee; ?></div> </td>
</tr><br />
<script type="text/javascript">
$(function() {
$('#attendance<?php echo $count; ?>').change(function(){
var activity_fee = '<?php echo $activity_fee; ?>'
if($('#attendance<?php echo $count; ?>').val() == 'Absent') {
$('#fee<?php echo $count; ?>').show();
$('#fee<?php echo $count; ?>').text('<?php echo $activity_fee; ?>');
} else {
$('#fee<?php echo $count; ?>').show();
$('#fee<?php echo $count; ?>').text('0');
$('#fines<?php echo $count; ?>').val('0');
}
});
});
</script>
<?php $count++; $member_count++; } ?>
<tr>
<td colspan="3"><button type="submit" id="btn_submit" class="btn btn-info pull-right">Submit</button></td>
</tr>
<?php echo form_close(); ?>
</table>
My Controller:
public function InsertAttendance()
{
$attendance_data = array(
'attendance_userID' => $this->input->post('uID[]'),
'attendance_councilID' => $this->input->post('cID[]'),
'attendance_activityID' => $this->input->post('aID[]'),
'attendance_activity' => $this->input->post('aname[]'),
'attendance_status' => $this->input->post('attendance[]'),
'attendance_sname' => $this->input->post('name[]'),
'attendance_fines' => $this->input->post('fines[]'));
$data['result'] = $this->Site_model->insertAttendance($attendance_data);
if($data['result'] == true)
{
$this->session->set_flashdata('feedback', '<div class="alert alert-success alert-dismissable">
×
<strong>Added Successfully</strong>
</div>');
redirect('Isidran/Home');
}
}
and Model:
public function insertAttendance($data)
{
$this->db->insert_batch('tbl_council_activity_attendance', $data);
if($this->db->affected_rows() > 0)
{
return true;
}
}
Try
public function insertAttendance($data)
{
$attendance=array(
'column1' => $data['index1'],
'column2' => $data['index2'],
'column3' => $data['index3'],
.
.
);
$this->db->insert('tbl_council_activity_attendance', $attendance);
if($this->db->affected_rows() > 0)
{
return true;
}
}
If you want to insert batch than your data array will be
$array_name=array(
array(
'col1'=>'data',
'col2'=>'data',
'col3'=>'data'
),
array(
'col1'=>'data',
'col2'=>'data',
'col3'=>'data'
)
);
I've the following code :
$query = "SELECT * FROM items WHERE SUBSTRING(item_no, 1, ".$length.") BETWEEN
'".$from_new."' AND '".$to_new."' ORDER BY item_no Desc";
$result = mysql_query($query);
$dd=array();
$ii=array();
$qq=array();
$aa=array();
if(mysql_num_rows($result)>0){
$num = mysql_num_rows($result);
?>
<form method="post" action="final_group_items.php">
<?php
echo "<table>";
for($i=0;$i<$num;$i++){
$row = mysql_fetch_array($result);
echo "<tr><td align=center>"; ?>
<input disabled maxlength="2" type="text"
name="ii[]" value="<?php echo strtoupper($row['item_no']); ?>"><?php echo
"</td><td align=center>";?>
<input disabled maxlength="2" type="text"
name="qq[]" value="<?php echo $row['qty'];?>">
<?php echo "</td><td align=center>"; ?>
<input disabled maxlength="2" type="text"
name="aa[]" value="<?php echo $row['actual_price'];?>">
<?php echo "</td><td align=center>";?>
<input required maxlength="2" type="text" name="dd[]" value="<?php echo
$row['discount_price']; ?>">
<?php
echo "</td><tr>";
}
echo "</table>";
?>
<input type="submit" value="Change Values">
</form>
Now when i click Submit it will open the final_group_items.php which has the follow testing code to make sure if all array (ii,qq,dd,aa) are not empty:
if(empty($_POST['qq']))
{
echo "No value inside";
return false;
}
foreach($_POST['qq'] as $test)
{
echo $test;
}
return true;
so by testing all array's, the only one works is $_POST['dd']...Others outputs "no value inside" which I really don't know how or why?
What should I do when I have multiple of fields having uniqe arrays and values.
Thank You
This:
<input disabled maxlength="2" type="text" name="qq[]" value="<?php echo $row['qty'];?>">
Will prevent the browser from even posting the field at form submission, because disabled gets an implicit value of "disabled".
The attribute should be removed:
<input maxlength="2" type="text" name="qq[]" value="<?php echo $row['qty'];?>">
I have a update form that populates from a database dynamically, I have two checkboxes in my form that post to an array so I can hit one submit button and update all rows with a foreach statement. The text fields work as they should, but when the checkbox fields post to their array, they leave out the zeros my arrays become shorter.
How do I add 0 where a null checkbox would be?
this is my form
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<input type="hidden" name="ss_id[]" value="<?php echo $row_rsSnapshot['ss_id']; ?>" />
<input type="hidden" name="ss_yearmonth[]" value="<?php echo htmlentities($row_rsSnapshot['ss_yearmonth'], ENT_COMPAT, 'UTF-8'); ?>" />
<tr>
<td>
<input <?php if (!(strcmp($row_rsSnapshot['ss_inventory'],1))) {echo "checked=\"checked\"";} ?> type="checkbox" name="ss_inventory[]" value="" <?php if (!(strcmp(htmlentities($row_rsSnapshot['ss_inventory'], ENT_COMPAT, 'UTF-8'),""))) {echo "checked=\"checked\"";} ?> />
</td>
<td>
<input <?php if (!(strcmp($row_rsSnapshot['ss_write_off'],1))) {echo "checked=\"checked\"";} ?> type="checkbox" name="ss_write_off[]" value="" <?php if (!(strcmp(htmlentities($row_rsSnapshot['ss_write_off'], ENT_COMPAT, 'UTF-8'),""))) {echo "checked=\"checked\"";} ?>
</td>
<td>
<input type="text" name="ss_date[]" value="<?php echo htmlentities($row_rsSnapshot['ss_date'], ENT_COMPAT, 'UTF-8'); ?>" size="32" />
</td>
<td>
<input type="text" name="ss_transaction[]" value="<?php echo htmlentities($row_rsSnapshot['ss_transaction'], ENT_COMPAT, 'UTF-8'); ?>" size="32" />
</td>...
And here is my sql update query
foreach($_POST['ss_id'] as $key=>$ss_id){
$ss_inventory = GetSQLValueString(isset($_POST['ss_inventory'][$key]) ? "true" : "", "defined","1","0");
$ss_write_off = GetSQLValueString(isset($_POST['ss_write_off'][$key]) ? "true" : "", "defined","1","0");
$ss_date = $_POST['ss_date'][$key];
$ss_transaction = $_POST['ss_transaction'][$key];
$ss_debit = $_POST['ss_debit'][$key];
$ss_credit = $_POST['ss_credit'][$key];
$ss_yearmonth = $_POST['ss_yearmonth'][$key];
$sql = "UPDATE snapshot SET ss_inventory = '$ss_inventory', ss_write_off = '$ss_write_off', ss_date = '$ss_date', ss_transaction = '$ss_transaction', ss_debit = '$ss_debit', ss_credit = '$ss_credit' , ss_yearmonth = '$ss_yearmonth' WHERE ss_id = '$ss_id' ";
mysql_select_db($database_connMyayla, $connMyayla);
$Result1 = mysql_query($sql, $connMyayla) or die(mysql_error());
}
}
mysql_close();
?>
this solved it
If anyone is interested...
I put a counter to find out how many rows are being populated with this query and inserted that into my checkbox name array...
$i = 0;
do {
...
<td>
<input <?php if (!(strcmp($row_rsSnapshot['ss_inventory'],1))) {echo "checked=\"checked\"";} ?> type="checkbox" name="ss_inventory[<?php echo $i;?>]" value="" <?php if (in_array($i, $ss_inventory)) echo "checked='checked'"; ?> />
</td>
<td>
<input <?php if (!(strcmp($row_rsSnapshot['ss_write_off'],1))) {echo "checked=\"checked\"";} ?> type="checkbox" name="ss_write_off[<?php echo $i;?>]" value="" <?php if (in_array($i, $ss_write_off)) echo "checked='checked'"; ?> />
</td>
....
$i++;
} while ($row_rsSnapshot = mysql_fetch_assoc($rsSnapshot));
now the checkboxes that are checked will correspond with the correct array number..
e.g.
lets say you have 4 rows of data, but you only check the checkboxes for row 2 and 4.
//run this to see your result
print_r($_POST['ss_inventory']);
//outputs: Array ([1] => [3] =>)
Before this was my output, everything was pushed up to the start of my array because the false checkboxes were not being submitted or NULL. so rows 1 and 2 would result true.
//outputs: Array ([0] => [1] =>)
Also See
Checkbox "checked"-value jumps up to earlier array values, when array is empty
The browser does not submit unchecked checkboxes. You'll need to either give unique names, or simply use radio buttons.
I am trying to create a quiz with PHP/Mysql...
I have created a form with radio buttons for answers which displays data pulled from the database as values for the radio buttons. I tried to submit the form but the result page does not show anything.
My quiz code goes as follows:
<form method="post" action="insertscore.php" name="cssCheckbox" id = "cssCheckbox">
<?php $query = "SELECT * FROM questions WHERE (`topics` = '.NET' OR `topics` = 'PHP') ORDER BY Rand() LIMIT 5"; $result = mysql_query($query);
if ($result && mysql_num_rows($result)) {
$numrows = mysql_num_rows($result);
$count =1;
while ($row = mysql_fetch_array($result))
{
?>
<div class="group">
<input type="hidden" name="<?php echo $row['key_id']; ?>"><?php $row['key_id']; ?></input>
<span class="test_question"><strong><?php echo $count;?>) <?php echo $row['question']; ?>
</strong><br />
<?php if($row['answer1'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer1']; ?>" id="chkLimit_1" ></input>
<label for="chkLimit_1" ><?php echo $row['answer1']; echo "<br />"; } else {} ?></label>
<?php if($row['answer2'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer2']; ?>" id="chkLimit_2" ></input>
<label for="chkLimit_2" ><?php echo $row['answer2']; echo "<br />"; } else {} ?></label>
<?php if($row['answer3'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer3']; ?>" id="chkLimit_3" ></input>
<label for="chkLimit_3" ><?php echo $row['answer3']; echo "<br />"; } else {} ?></label>
<?php if($row['answer4'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer4']; ?>" id="chkLimit_4" ></input>
<label for="chkLimit_4" ><?php echo $row['answer4']; echo "<br />"; } else {} ?></label>
<?php if($row['answer5'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer5']; ?>" id="chkLimit_5" ></input>
<label for="chkLimit_5" ><?php echo $row['answer5']; echo "<br />"; } else {} ? ></label>
<?php if($row['answer6'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer6']; ?>" id="chkLimit_6" ></input>
<label for="chkLimit_6" ><?php echo $row['answer6']; echo "<br />"; } else {} ?></label>
<?php if($row['answer7'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer7']; ?>" id="chkLimit_7" ></input>
<label for="chkLimit_7" ><?php echo $row['answer7']; echo "<br />"; } else {} ?></label>
<?php if($row['answer8'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer8']; ?>" id="chkLimit_8" ></input>
<label for="chkLimit_8" ><?php echo $row['answer8']; echo "<br />"; } else {} ?></label>
<input type="hidden" name="<?php echo $row['right_answer']; ?>"><?php $row['right_answer']; ?></input>
</div>
<input name="Submit" type="submit" value="Submit Your Answers" class="submit">
</form>
Code on submitted Page looks like:
<?php
if(isset($_POST['Submit'])){
$key_id=$_POST['key_id']; echo $key_id;
$question=$_POST['question']; echo $question;
$answers=$_POST['answers']; echo $answers;
$correctanswer=$_POST['correctanswer']; echo $correctanswer;
}
foreach($_POST as $key => $val)
{
echo "$key --> $val<br />";
}
//var_dump($_POST);
?>
Please let me know if anything is not clear or if I am missing anything....
Thanks,
Shank
I would:
remove comments to //var_dump($_POST); and move this line at the top of the code on submitted Page.
if you still don't see anything, I think the code on submitted page is not in a file called insertscore.php or such file is not in same folder of your form page.