multiple radio button on array codeigniter - php

I want to make multiple radio-button on my CodeIgniter webapps, but this value is null after I submit my form.
This is my view on codeigniter apps
<?php $no=0; foreach ($catsub_1_isi as $list2_1a_1a): $no++ ?>
<form role="form" method="post" action="<?php echo base_url('c_QA_roe/simpan_list'); ?>">
<input type="hidden" name="WSDetailID[]" value="<?php echo $kode_detail;?>" class="form-control">
<input type="hidden" name="WSHeaderID[]" value="<?php echo $judul->WSHeaderID;?>" class="form-control">
<input type="hidden" name="AppDate[]" value="<?php $tgl=date('Y-m-d-H-i-s'); echo $tgl; ?>" class="form-control">
<input type="hidden" name="InputDate[]" value="<?php $tgl=date('Y-m-d-H-i-s'); echo $tgl; ?>" class="form-control">
<input type="hidden" name="InputUser[]" value="<?php echo $UserName; ?>" class="form-control">
<input type="hidden" name="WSListID[]" value="<?php echo $list2_1a_1a->WSListID;?>" class="form-control">
<tr>
<td><?php echo $no;?></td>
<td><?php echo $list2_1a_1a->Subject;?></td>
<td><?php echo $list2_1a_1a->Standard;?></td>
<td align="center">
<div class="radio">
<input type="text" name="YesNo[<?php echo $list2_1a_1a->WSListID;?>]" value="0">
<input type="radio" name="YesNo[<?php echo $list2_1a_1a->WSListID;?>]" class="minimal" value="<?php echo $list2_1a_1a->Value;?>" checked>Y
<input type="text" name="YesNo[<?php echo $list2_1a_1a->WSListID;?>]" value="0">
<input type="radio" name="YesNo[<?php echo $list2_1a_1a->WSListID;?>]" class="minimal" value="<?php echo $list2_1a_1a->Value;?>">N
</div>
</td>
<td align="center">
<select name="IsRedudance[]">
<option value="0">-</option>
<option value="<?php echo $list2_1a_1a->Value;?>">Yes</option>
</select>
</td>
<td align="center">
<select name="IsUse[]">
<option value="YES">Yes</option>
<option value="NO">No</option>
</select>
</td>
</tr>
<?php endforeach;?>
This is my controller on codeigniter apps
public function simpan_list()
{
$WSDetailID = $this->input->post('WSDetailID[]');
$WSHeaderID = $this->input->post('WSHeaderID[]');
$WSListID = $this->input->post('WSListID[]');
$IsYes = $this->input->post('IsYes[]');
$IsNo = $this->input->post('IsNo[]');
$IsRedudance = $this->input->post('IsRedudance[]');
$IsUse = $this->input->post('IsUse[]');
$AppDate = $this->input->post('AppDate[]');
$InputDate = $this->input->post('InputDate[]');
$InputUser = $this->input->post('InputUser[]');
$Yes = $this->input->post('YesNo[]');
$No = $this->input->post('YesNo[]');
$data = array();
$index = 0;
foreach($WSDetailID as $datanis)
{
array_push($data, array(
'WSDetailID' =>$datanis,
'WSHeaderID' =>$WSHeaderID[$index],
'WSListID' =>$WSListID[$index],
'IsYes' =>$Yes[$index],
'IsNo' =>$No[$index],
'IsRedudance' =>$IsRedudance[$index],
'IsUse' =>$IsUse[$index],
'AppDate' =>$AppDate[$index],
'InputDate' =>$InputDate[$index],
'InputUser' =>$InputUser[$index],
));
$index++;
var_dump($data['IsYes']);
}
}
on my controller i'am confused about POST data on $YesNo especialy variable for multiple radio-button, so I tried to make post('YesNo[]') Is this true? but the value is still null

there is no checkbox. but if you want to post the value of checkboxes. you can post this way.
//view
<input type="checkbox" name="active" placeholder="Active" checked >
//controller
'field_name' => ($this->input->post('field_name') == "true") ? 1 : 0;

Related

Onsubmit Event do form validation in PHP

I have a HTML/PHP form that needs validation of each of the entered fields before updating MySQL database. It would be nice if this cold be done as each field is entered...however, I could use the form's action attribute to run a different validation script but then if there are errors I would need to return to the form and re-populate it with previously entered data for correction - not sure how I would do all this but I guess it's possible. However, I've seen a post here that shows how this can be done with a javascript function for the validation but is this possible to do in PHP? I'm just learning PHP so I'd hate to now have to also learn javascript :( Some guidance appreciated.
<?php
if(isset($_POST)) {
echo __LINE__ . " Form has been posted <br>";
echo __LINE__ . $_POST['state_city'];
}
?>
<form action="" method="post">
<table border="0" align="center">
<tr height="29px" width = "220px">
<td>
<label for="student_id"><b>Student-ID</b></label>
</td>
<td>
<input type="text" placeholder="Student ID" name="studentid" value="<?php echo $row['studentid'];?>" required>
</td>
</tr>
<tr height="29px" width = "220px">
<td>
<label for="student_name"><b>Student name</b></label>
</td>
<td>
<input type="text" placeholder="Student name" name="name" value="<?php echo $row['$studentname'];?>" required>
</td>
</tr>
<tr height="29px" width = "220px"><td><label for="start_date"><b>Start Date</b></label></td>
<td><input type="date" placeholder="Enter start date" name="start_date" value="<?php echo $row['startdate'];;?>" required></td>
</tr>
<tr height="29px"><td width = "220px">
<b>Student Grade</b></td>
<td>
<select name="sgrade" id="sgrade">
<option value="1" <?php if ($row['$sgrade'] == '1') echo "selected"; ?>>Grade 1<option>
<option value="2" <?php if ($row['$sgrade'] == '2') echo "selected"; ?>>Grade 2<option>
<option value="3" <?php if ($row['$sgrade'] == '3') echo "selected"; ?>>Grade 3<option>
<option value="4" <?php if ($row['$sgrade'] == '4') echo "selected"; ?>>Grade 4<option>
</select>
</td>
<tr>
// value?
<tr height="29px"><td width = "220px"><b>Country of Birth</b></td>
<td>
<select name="country" id="country">
<?php
$sql = "SELECT * FROM countries";
$result = mysqli_query($con, $sql);
?>
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
}
?>
</select>
<?php
mysqli_free_result($result);
mysqli_close($con);
?>
</td>
</tr>
<tr height="29px" width = "220px">
<td><label for="state_city"><b>State and/or main city</b></label></td>
<td><input type="text" placeholder="Student State and/or Main City" name="state_city" value="<?php echo $row['$state_city'];?>" required></td>
</tr>
<tr height="29px" width = "220px">
<td><label for="healthcheck"><b>Checked?</b></label></td>
<td>
<input type="radio" id="checkyes" name="check" value="1" <?php if ($row['check']) echo "checked";?>>
<label for="checkyes"> Yes</label><br>
<input type="radio" id="checkno" name="check" value="0" <?php if (!$row['check']) echo "checked";?>>
<label for="checkno"> No</label><br><br>
</td>
</tr>
</table>
<button type="submit">Save</button>
<div class="container" style="background-color:#f1f1f1">
<button type="button" onclick="history.back()" value="Go back!" class="cancelbtn">Cancel</button>
</div>
</form>
<script>
function check() {
document.getElementById("checkyes").checked = true;
}
function uncheck() {
document.getElementById(hcheckyes").checked = false;
}
</script>
</body>
</html>
If you want it to be dynamic as the user enters the information you will have to use JavaScript as that would be a front-end solution. With PHP you can do you validation/sanitizing of the data on the server-side as the form is submitted. It is suggested you do both.
For your question of repopulating this is one way
<input type="text" name="name" value="<?php echo htmlspecialchars($_POST['name'] ?? '', ENT_QUOTES); ?>">
For more info : click here

insert array type checkbox and text with php

How to insert array data from checkbox and input text with php codeigniter
this is my view
<?php foreach ($data_ikan as $row) { ?>
<tr>
<td>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="ikan[]" value="<?php echo $row['nama_ikan']; ?>" id="checkbox<?php echo $row['id_data_ikan']; ?>">
<label class="form-check-label" for="checkbox<?php echo $row['id_data_ikan']; ?>">
<?php echo $row['nama_ikan']; ?>
</label>
</div>
</td>
<td>
<input type="number" name="produksi[]" class="form-control form-input-sm my-1" id="produksi<?php echo $row['id_data_ikan'] ?>" placeholder="Kg" disabled>
</td>
</tr>
<?php } ?>
Can help me?
Thanks
Best Regrads.
Please Try this :
<?php foreach ($data_ikan as $row) { ?>
<tr>
<td>
<input class="form-check-input" onclick="return show('<?=$row['id_data_ikan']?>')" type="checkbox" name="ikan[]" value="<?=$row['id_data_ikan']?>" id="checkbox<?=$row['id_data_ikan']?>"><?=$row['nama_ikan']?>
</td>
<td>
<input type="number" name="produksi<?=$row['id_data_ikan']?>" class="form-control form-input-sm my-1" id="produksi<?=$row['id_data_ikan']?>" placeholder="Kg" disabled>
</td>
</tr>
<?php } ?>
add javascript function
<script>
function show(id) {
if(document.getElementById('checkbox'+id).checked){
document.getElementById("produksi"+id).disabled = false;
}else{
document.getElementById("produksi"+id).disabled = true;
}
}
</script>
For checkboxes
$checkboxes = $_POST["ikan[]"];
Then you can loop through it
foreach($checkboxes as $checkbox){
//code
}

receive serialize data in php sent via ajax and loop through it

I am sending a form data via ajax call to a php script. I am serializing the data in ajax and on the php script I want to loop through that data to extract the values.
This is my ajax call
$("#submitAttendance").click(function(){
var data = $('form#attendanceForm').serialize();
$.ajax({
url: 'save-attendance.php',
method: 'post',
data: {formData: data},
success: function(data){
console.log(data);
alert(data);
}
});
});
and in the attendance.php I am doing
print_r(($_POST['formData']));//prints the entire serialize data
when I do this
parse_str($_POST['formData'], $searcharray);
print_r(($searcharray));//prints only last user and all radio buttons
I want to extract values so I can save it in db.
This is my form
<form action="" id="attendanceForm">
<?php
if(mysqli_num_rows($result)>0){
while($row = $result->fetch_assoc()){ ?>
<tr>
<input type="hidden" value="<?php echo($row['id']);?>">
<td><input type="text" name="name" value="<?php echo $row['fullname'];?>" readonly></td>
<td><input type="text" name="email" value="<?php echo $row['email'];?>" readonly</td>
<td><input type="text" name="class" value="<?php echo $row['class'];?>" readonly</td>
<td><input type="radio" value="present" name="<?php echo($row['id']); ?>" checked></td>
<td><input type="radio" value="absent" name="<?php echo($row['id']); ?>"></td>
</tr>
<?php }
}
?>
<input id="submitAttendance" type="button" class="btn btn-success" value="Submit Attendance" name="submitAttendance">
</form>
You need to rename your items to be able to post arrays (that is call them "whatever" + "[]" and loop over them in PHP), e.g.:
HTML:
<form action="" id="attendanceForm">
<?php
if(mysqli_num_rows($result)>0){
while($row = $result->fetch_assoc()){ ?>
<tr>
<input type="hidden" value="<?php echo($row['id']);?>">
<td><input type="text" name="name[]" value="<?php echo $row['fullname'];?>" readonly></td>
<td><input type="text" name="email[]" value="<?php echo $row['email'];?>" readonly</td>
<td><input type="text" name="class[]" value="<?php echo $row['class'];?>" readonly</td>
<td><input type="radio" value="present" name="<?php echo($row['id']); ?>" checked></td>
<td><input type="radio" value="absent" name="<?php echo($row['id']); ?>"></td>
</tr>
<?php }
}
?>
<input id="submitAttendance" type="button" class="btn btn-success" value="Submit Attendance" name="submitAttendance">
</form>
Later in PHP:
foreach ($_POST["formData"]["name"] as $name)
echo "Wow, $name is a really pretty name!";
Additionally, I am not sure what present and absent are meant to do and why they should have the same name (an id). You are already posting the id as an hidden field, why should it be done twice? One overrides the other one (as the names have to be unique).
In addition to #Jan answer I did following to get the complete data and loop through it
parse the incoming data
parse_str($_POST['formData'], $searcharray);
then loop through the array
for ($i = 0 ; $i <= sizeof($searcharray) ; $i++){
$name = $searcharray['name'][$i];
$email= $searcharray['email'][$i];
$class = $searcharray['class'][$i];
$present= ($searcharray['present'][$i]);
}
and my form code is
<form action="" id="attendanceForm">
<?php
if(mysqli_num_rows($result)>0){
$i=0;
while($row = $result->fetch_assoc()){
?>
<tr>
<input type="hidden" value="<?php echo($row['id']);?>">
<td><input type="text" name="name[]" value="<?php echo $row['fullname'];?>" readonly></td>
<td><input type="text" name="email[]" value="<?php echo $row['email'];?>" readonly</td>
<td><input type="text" name="class[]" value="<?php echo $row['class'];?>" readonly</td>
<td><input type="radio" value="present" name="present[<?php echo $i; ?>]" checked></td>
<td><input type="radio" value="absent" name="present[<?php echo $i; ?>]"></td>
</tr>
<?php $i++;
}
}
?>
<input id="submitAttendance" type="button" class="btn btn-success" value="Submit Attendance" name="submitAttendance">
</form>

Strange page rendering in codeigniter

I have the following code in the view of an application I am developing using the codeigniter framework
<?php foreach($query->result_array() as $row){ ?>
<tr>
<?php echo form_open('MainController/AttendanceSet');?>
<input type="hidden" name="child_nric" value="<?php echo $row['child_nric']; ?>"/>
<input type="hidden" name="teachernric" value="<?php echo $_SESSION['username']; ?>"/>
<input type="hidden" name="progcode" value="<?php echo $program; ?>"/>
<td><?php echo $row['child_nric']; ?></td>
<td>
<select name="attendance">
<option value="1" selected="<?php if($selected==1) echo "selected"; ?>">Present</option>
<option value="0" selected="<?php if($selected==0) echo "selected"; ?>">Absent</option>
</select>
</td>
<td> <input type="submit" name="submit" value="Submit"></form></td>
</tr>
<?php } ?>
Have made edit to the code based on the answers below now it is like this
<?php foreach($query->result_array() as $row){ ?>
<tr>
<?php echo form_open('MainController/AttendanceSet');?>
<input type="hidden" name="child_nric" value="<?php echo $row['child_nric']; ?>"/>
<input type="hidden" name="teachernric" value="<?php echo $_SESSION['username']; ?>"/>
<input type="hidden" name="progcode" value="<?php echo $program; ?>"/>
<td><?php echo $row['child_nric']; ?></td>
<td>
<select name="attendance">
<option value="1" selected="<?php if($selected==1) echo "selected"; ?>">Present</option>
<option value="0" selected="<?php if($selected==0) echo "selected"; ?>">Absent</option>
</select>
</td>
<td> <input type="submit" name="submit" value="Submit"></td>
</tr>
<?php echo form_close();} ?>
But the output is still the same
However when the view is loaded, the code comes out like this
<tr>
<form action="http://sms-dev.anovatesoft.com/index.php/MainController/AttendanceSet" method="post" accept-charset="utf-8"></form>
<input type="hidden" name="child_nric" value="A12">
<input type="hidden" name="teachernric" value="T001">
<input type="hidden" name="progcode" value="FEE001">
<td>A12</td>
<td>
<select name="attendance">
<option value="1" selected="">Present</option>
<option value="0" selected="">Absent</option>
</select>
</td>
<td> <input type="submit" name="submit" value="Submit"></td>
</tr>
Notice the difference in the </from> position? In my code I have it after the submit button but when it renders it closes just after the <form> opens making the form useless as I am not able to submit it.
What is the reason behind this strange behavior? And how do I fix it?
The View
<?php
$this->load->helper('form');
?>
<script>
function ListView(){
//if($("#id").val() != "0"){
var formURL = "<?php echo base_url();?>MainController/Attendance/"+$("#program").val();
$.post(formURL).done(function(data){$("#body_view_paste").html(data); });
}
function fire(){
alert("I have been fired");
}
</script>
<div align="center">
<table align="center" style="max-width:80%">
<tr>
<td>Program</td>
<td>
<select id="program" name="program" onchange="ListView()">
<option value="0">Select</option>
<?php
$nricno = $_SESSION['username'];
$sql = "SELECT distinct programs.activities, programs.progcode FROM events
LEFT JOIN programs ON programs.progcode=events.progcode WHERE events.teacher_nric='$nricno'";
$activities = $this->db->query($sql);
foreach($activities->result_array() as $row){?>
<option value="<?php echo $row['progcode'];?>"><?php echo $row['activities'];?></option>
<?php } ?>
</select>
</td>
</tr>
</table>
</div>
<div>
<?php if(isset($program)){
$selected=3;
$query = $this->db->query("SELECT child_nric FROM child_reg_prog WHERE progcode = '$program'");
?>
<table>
<thead>
<tr>
<td>NRIC NO</td>
<td>Attendance</td>
<td>Action</td>
</tr>
</thead>
<?php foreach($query->result_array() as $row){ ?>
<tr>
<?php echo form_open('MainController/AttendanceSet');?>
<input type="hidden" name="child_nric" value="<?php echo $row['child_nric']; ?>"/>
<input type="hidden" name="teachernric" value="<?php echo $_SESSION['username']; ?>"/>
<input type="hidden" name="progcode" value="<?php echo $program; ?>"/>
<td><?php echo $row['child_nric']; ?></td>
<td>
<select name="attendance">
<option value="1" selected="<?php if($selected==1) echo "selected"; ?>">Present</option>
<option value="0" selected="<?php if($selected==0) echo "selected"; ?>">Absent</option>
</select>
</td>
<td> <input type="submit" name="submit" value="Submit"></td>
</tr>
<?php echo form_close();} ?>
</table>
<?php } ?>
</div>
The Controller part
public function Attendance($program = "")
{
$this->main_model->pagePermissions("Attendance");
if ($program == "") {
$this->load->view('Attendance');
} else {
$data['program'] = $program;
$this->load->view('Attendance', $data);
}
}
public function AttendanceSet()
{
$child_nric = $_POST['child_nric'];
$attendance = $_POST['attendance'];
$teachernric = $_POST['teachernric'];
$progcode = $_POST['progcode'];
$ispresent="No";
if($attendance==1)
{
$ispresent="Yes";
}
$this->load->model("Attendance_model");
$this->Attendance_model->insert($progcode, $childnric, $teachernric, $ispresent);
$this->load->view('Attendance');
}
The model part
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Attendance_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function insert($progcode, $childnric, $teachernric, $ispresent)
{
$sql = "INSERT INTO attendence (progcode, childnric, teachernric, ispresent ) VALUES (?,?,?,?) ";
$query = $this->db->query($sql, array($progcode, $childnric, $teachernric, $ispresent));
}
}
?>
try this:
<?php foreach($query->result_array() as $row){ ?>
<?php echo form_open('MainController/AttendanceSet');?>
<tr>
<input type="hidden" name="child_nric" value="<?php echo $row['child_nric']; ?>"/>
<input type="hidden" name="teachernric" value="<?php echo $_SESSION['username']; ?>"/>
<input type="hidden" name="progcode" value="<?php echo $program; ?>"/>
<td><?php echo $row['child_nric']; ?></td>
<td>
<select name="attendance">
<option value="1" selected="<?php if($selected==1) echo "selected"; ?>">Present</option>
<option value="0" selected="<?php if($selected==0) echo "selected"; ?>">Absent</option>
</select>
</td>
<td> <input type="submit" name="submit" value="Submit"></td>
</tr>
<?php echo form_close(); ?>
<?php } ?>
Both of the below statements should come outside the foreach loop
<?php echo form_open('MainController/AttendanceSet');?>
<?php echo form_close(); ?>// you need to close it

Select time from database after choose a ticket number

I need to make a edit / update function based from searching ticket number. When user type a ticket number he had, would appear a form that contains data from database based on ticket number that he had.I can see name value, ticket number value and date value from database but I can't see a clock time value in select tag.
<?php
$no = $_GET['ticket'];
$st = "SELECT * FROM event WHERE no='$no'";
$check = mysql_query($st,$connection) or die("Failed");
$c = mysql_fetch_array($check);
?>
<form name="form" method="POST" action="">
<table>
<tr>
<td>Reference Number</td>
<td> : </td>
<td>
<input type="text" name="no" value="<?php echo $c['no'] ;?>" disabled>
<input type="hidden" name="no" value="<?php echo $c['no'] ;?>">
</td>
</tr>
<tr>
<td>Name</td>
<td> : </td>
<td>
<input type="text" name="name" value="<?php echo $c['name'] ;?>" disabled>
<input type="text" name="name" value="<?php echo $c['name'] ;?>" disabled>
</td>
</tr>
<tr>
<td>Date</td>
<td> : </td>
<td>
<input type="text" id="date" name="date" value="<?php echo $c['date'] ;?>">
</td>
</tr>
<tr>
<td>Clock Time</td>
<td> : </td>
<td>
<select name="time" value="<?php echo $c['time'] ;?>">
</td>
</tr>
I want to see the clock time value on database in select tag. After that, if user want to make changes on clock time based on the date he chosen, he can do that thing.
Thanks before,
The stucture of drop down in htm is
<select name ="postname">
<option value="postvalue"> display value</option>
more options
.
.
.
.
</select>
use following code in your case
<select name="postname" >
<option value="<?php echo $c['date'] ;?>" > <?php echo $c['date'] ;?> </option>
</select>
select tags must have options. you need to popuate the select tag with options and then check it against your value to set the selected option.
this method is wrong <select name="time" value="<?php echo $c['date'] ;?>">
<select name="time">
<?php foreach($options as $option) { ?>
<?php $sel = ''; if ($option->value = $c['date']) { $sel = 'selected'; } ?>
<option value="<?php echo $option->value; ?>" <?php echo $sel; ?>><?php echo $option->name; ?></option>
<?php } ?>
</select>
put your select like this:-
<select name="time" >
<option value=value="<?php echo $c['date'] ;?>" selected > <?php echo $c['date'] ;?> </option>
</select>

Categories