Codeigniter can't pass value to controller - php

i have views here
<?php echo form_open('c_kategorimaterial/tambah'); ?>
<table>
<tr>
<td>Kode Kategori Material / Jasa</td> //primary key of table
<td><?php $inkode=array('name' => 'kkmj', 'disabled' => 'disabled', 'value' => $nextval, 'class' => 'GUI'); echo form_input($inkode) ?></td>
<td class="error"> <?php echo form_error('kkmj'); ?></td>
//i make the form_input disabled, because the value is
//auto generated from the last ID of table in database
</tr>
<tr>
<td>Nama Material / Jasa</td>
<td><?php $innama=array('name' => 'nmj', 'class' => 'GUI'); echo form_input($innama) ?></td>
<td class="error"> <?php echo form_error('nmj'); ?></td>
</tr>
<tr>
<td></td>
<td><?php $intambah=array('name' =>'login','class' =>'button','value' => 'Tambah'); echo form_submit($intambah); echo nbs(); $inreset=array('name' =>'reset','class' =>'button','value' => 'Hapus'); echo form_reset($inreset); ?></td>
</tr>
<?php echo form_close(); ?>
and it pass the value to the controller
function tambah()
{
$id = $this->input->post('kkmj');
$nama = $this->input->post('nmj');
$this->form_validation->set_rules('nmj','Nama','trim|required|min_length[2]|max_length[20]|xss_clean');
if($this->form_validation->run() == false)
{
$lastval = $this->m_admin->getlastval('KKMJ','ms_kategori_material','kode_kategori_material_jasa');
$data['nextval'] = $this->m_admin->gencode('KKMJ',3,$lastval);
$data['title'] = 'QB Tambah Kategori Material';
$this->load->view('head',$data);
$this->load->view('content/add_kategori_material',$data);
}
else
{
echo 'id adalah '.$id.' namanya adalah '.$nama; //$id is not printed
}
}
and then i fill the second field with 'water' and click the button. my page showing blank white screen with a text id adalah namanya adalah water
how come the ID is not printed ? how do i resolve this ?

Disabled elements do not get posted when you submit a form. If you need a field with unchangeable value, try to set it as read-only instead of disabled.

Because the field is disabled.
Use readonly attribute.

Related

Unable to fetch correct data. Invalid argument supplied for foreach() warning

The query isn;t running, not sure where Iam going wrong
Input from previous page was doctor's licence number and date from javascript calender
Posting the values using fetchApponitment function to next page->
if(isset($_POST['BookAppointment']))
{ $DOCTOR_LICENSE_NO = $_POST['DOCTOR_LICENSE_NO'];
$APPOINTMENT_DATE = $_POST['APPOINTMENT_DATE'];
}
$appointmentdetails = fetchAppointments($DOCTOR_LICENSE_NO,$APPOINTMENT_DATE);
?>
<table class="table-style-three">
<thead>
<!-- display Doctor details header -->
<th>DOCTOR_LICENSE_NO</th>
<th>DOCTOR_FNAME</th>
<th>DOCTOR_LNAME</th>
<th>DOCTOR_EMAIL_ID</th>
<th>DOCTOR_PHONE</th>
<th>APPOINTMENT_DATE</th>
<th>APPOINTMENT_TIME</th>
<th>APPOINTMENT_STATUS</th>
</thead>
<tbody>
<?php
foreach($appointmentdetails as $displayAppointment) { ?> <!-- foreach is very important -->
<tr>
<td><?php print $displayAppointment['DOCTOR_LICENSE_NO']; ?></td>
<td><?php print $displayAppointment['DOCTOR_FNAME']; ?></td>
<td><?php print $displayAppointment['DOCTOR_LNAME']; ?></td>
<td><?php print $displayAppointment['DOCTOR_EMAIL_ID']; ?></td>
<td><?php print $displayAppointment['DOCTOR_PHONE']; ?></td>
<td><?php print $displayAppointment['APPOINTMENT_DATE']; ?></td>
<td><?php print $displayAppointment['APPOINTMENT_TIME']; ?></td>
<td><?php print $displayAppointment['APPOINTMENT_STATUS']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
<?php require_once("footer.php");?>
The function looks like this -->
function fetchAppointments($DOCTOR_LICENSE_NO,$APPOINTMENT_DATE) {
global $mysqli,$DOCTOR_LICENSE_NO, $APPOINTMENT_DATE, $db_table_prefix,$row;
$args = array($DOCTOR_LICENSE_NO, $APPOINTMENT_DATE);
//echo $DOCTOR_LICENSE_NO;
//echo $APPOINTMENT_DATE;
echo $args[0];
echo $args[1];
$stmt = $mysqli->prepare(
"SELECT doctor.DOCTOR_LICENSE_NO,
doctor.DOCTOR_FNAME,
doctor.DOCTOR_LNAME,
doctor.DOCTOR_EMAIL_ID,
doctor.DOCTOR_PHONE,
appointment.APPOINTMENT_DATE,
appointment.APPOINTMENT_TIME,
appointment.APPOINTMENT_STATUS
FROM doctor INNER JOIN appointment ON (doctor.DOCTOR_LICENSE_NO = appointment.DOCTOR_LICENSE_NO)
WHERE doctor.DOCTOR_LICENSE_NO= ?
AND appointment.APPOINTMENT_DATE= ? ");
$stmt->bind_param("ss",$args[0], $args[1]);
$stmt->execute();
$stmt->bind_result($DOCTOR_LICENSE_NO,$DOCTOR_FNAME, $DOCTOR_LNAME, $DOCTOR_EMAIL_ID, $DOCTOR_PHONE,$APPOINTMENT_DATE , $APPOINTMENT_TIME, $APPOINTMENT_STATUS);
while ($stmt->fetch()){
$row[] = array(
'DOCTOR_LICENSE_NO' => $DOCTOR_LICENSE_NO,
'DOCTOR_FNAME' => $DOCTOR_FNAME,
'DOCTOR_LNAME' => $DOCTOR_LNAME,
'DOCTOR_EMAIL_ID' => $DOCTOR_EMAIL_ID,
'DOCTOR_PHONE' => $DOCTOR_PHONE,
'APPOINTMENT_DATE' => $APPOINTMENT_DATE ,
'APPOINTMENT_TIME' => $APPOINTMENT_TIME,
'APPOINTMENT_STATUS' => $APPOINTMENT_STATUS
);
}
$stmt->close();
return ($row);
}

Looping through HTML table checked checkboxes using php

i have a similar thread using javascript but i found some problem and some bugs. so i changed to php
so far this is my code
if(!empty($_POST['check'])) {
foreach($_POST['check'] as $check) {
//get the html data beside the checked checkboxes
}
}else{
echo '<script language="javascript">';
echo 'alert("Nothing checked")';
echo '</script>';
}
and my stripped html code
<tr>
<td><?php echo $r['Name'] ?></td>
<td><?php echo $r['Age'] ?></td>
<td><?php echo $r['Address'] ?></td>
<td><input name="check[]" type="checkbox" ></td>
</tr>
i wan't to get the data on the same row where a checkbox is checked. please help me. thanks
You have to loop through the POST data, and assign $r[NAME] = VALUE
For the PHP code:
array $r;
foreach($_POST as $post=>$value) {
//get the html data beside the checked checkboxes
$r[ $post] = $value;
}
}
}else{
echo '<script language="javascript">';
echo 'alert("Nothing checked")';
echo '</script>';
}
Assuming that the HTML part your referring to is the result page
For the HTML:
<tr>
<td><?php echo $r['Name'] ?></td>
<td><?php echo $r['Age'] ?></td>
<td><?php echo $r['Address'] ?></td>
</tr>
First, lets add some data:
<?php
$data = [
'111' => [
'Name' => 'John',
'Age' => '25',
'Address' => 'Baker street, 11'
],
'222' => [
'Name' => 'Mary',
'Age' => '19',
'Address' => 'Elm street, 14'
],
'333' => [
'Name' => 'Nick',
'Age' => '23',
'Address' => '64th stree, 2'
]
];
?>
Output all data items and set checks from previous posted data:
<form method="POST">
<table>
<?php foreach($data as $id => $item): ?>
<tr>
<td><?= $item['Name'] ?></td>
<td><?= $item['Age'] ?></td>
<td><?= $item['Address'] ?></td>
<td><input name="check[]" type="checkbox" value="<?= $id; ?>"
<?php if (isset($_POST['check']) && in_array($id, $_POST['check'])): ?>
checked="checked"
<?php endif; ?>/></td>
</tr>
<?php endforeach; ?>
</table>
<input type="submit" />
</form>
<?php if (empty($_POST['check'])): ?>
<script>alert("Nothing checked");</script>
<?php endif; ?>
You'll get reloaded page with properly marked checkboxes after the form submission.

How can i set a condition based on database value to add a new user in Codeigniter?

I have three users Admin Reseller and User. Now Admin creates both Reseller and User. So Admin add user for a specific reseller. Reseller has a filed in database called Allocation Block This is a max value of users a reseller can have So I add my users in user controller,there i want to check a condition that if i am adding a user for XYZ reseller, then i must check the allocation block value. if its greater than it the show a error message and not allow to add that user.
Below is my code to add a user :
Controller:
public function add_user()
{
$usertype = $this->session->userdata('usertype');
$this->load->model('reseller_m');
if ($usertype == "admin") {
$this->data['user'] = $this->user_m->get_new();
$rules = $this->user_m->rules_admin;
$rules['password']['rules'] .= '|required';
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == TRUE) {
$data = $this->user_m->array_from_post(array('sip_id', 'sip_pass', 'name', 'key', 'email', 'password', 'phone', 'status', 'created', 'balance'));
$data['password'] = $this->user_m->hash($data['password']);
$key = $this->user_m->save($data, $id);
redirect('admin/user');
}
$resellers = $this->reseller_m->get_drop_down();
if(count($resellers) > 0) {
foreach($resellers as $value) {
$dropdown[$value->key] = $value->key;
}
}
$this->data['resellers'] = $dropdown;
$this->data['subview'] = 'admin/user/add';
$this->load->view('admin/_layout_main', $this->data);
} else {
$this->load->view('permission');
}
}
The Add View of Users:
<h3><?php echo empty($user->id) ? '<i class="glyphicon glyphicon-user"></i> Add a Reseller' : 'Edit User ' . $user->name; ?></h3>
<?php echo validation_errors(); ?>
<?php echo form_open(); ?>
<table class="table">
<tr>
<td>SIP Username</td>
<td><?php echo form_input('sip_id', set_value('sip_id', $user->sip_id)); ?></td>
</tr>
<tr>
<td>SIP Password</td>
<td><?php echo form_input('sip_pass', set_value('sip_pass', $user->sip_pass)); ?></td>
</tr>
<tr>
<td>Key</td>
<td>
<?php echo form_dropdown('key', set_value('key', $resellers,$user->key));?>
</td>
</tr>
<tr>
<td>Name</td>
<td><?php echo form_input('name', set_value('name', $user->name)); ?></td>
</tr>
<tr>
<td>Email</td>
<td><?php echo form_input('email', set_value('email', $user->email)); ?></td>
</tr>
<tr>
<td>Password</td>
<td><?php echo form_password('password'); ?></td>
</tr>
<tr>
<td>Confirm password</td>
<td><?php echo form_password('password_confirm'); ?></td>
</tr>
<tr>
<td>Balance</td>
<td>Yes<?php echo form_radio('balance','Yes', $this->input->post('balance') ? $this->input->post('balance') : $user->balance); ?> No <?php echo form_radio('balance','No', $this->input->post('balance') ? $this->input->post('balance') : $user->balance); ?></td>
</tr>
<tr>
<td>Phone</td>
<td><?php echo form_input('phone', set_value('phone', $user->phone)); ?></td>
</tr>
<tr>
<td>Status</td>
<td><?php echo form_dropdown('status', array('Active' => 'active', 'Inactive' => 'inactive', 'Delete' => 'delete'), $this->input->post('status') ? $this->input->post('status') : $user->status ); ?></td>
</tr>
<tr>
<td>Created</td>
<td><?php echo form_input('created', set_value('created', $user->created)); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table>
<?php echo form_close();?>
First of all, in the user table you must have a column to indicate to the corresponding reseller id for a particular user. So you should add that column in your table if it doesn't exist already.
Then in the model, you can add a function to count the users by reseler id.
function reseler_users_count($reseler_id){//count users by reseller
$this->db->where('key', $reseler_id);
return $this->db->count_all_results('users_tbl');
//DB Query: Select * FROM users_tbl WHERE key = '$reseller_id';
//return number of rows
}
Also in your model, add a function that checks the allocation block value and compares it with the number of users already registered for that reseller. It returns true or false, but you can modify it to return registered users or false, or whatever.
function check_reseller_allocation($reseler_id){
$this->db->select('allocation_block');
$this->db->where('id', $reseler_id);
$query = $this->db->get('reselers_tbl');
//DB QUERY: SELECT 'allocation_block' FROM 'reselers_tbl' WHERE 'id' = $reseller_id;
$reseler = $query->row();// Get row (it should be only one row, but I didn't add the condition though; you can add if($query->num_rows() == 1) )
$allocation_block = $reseler->alocation_block;
$reseller_users = $this->reseler_users_count($reseler_id);//call reseler_users_count() to find users registered for this reseller
if($reseller_users < $allocation_block)return TRUE;//is the number of users registered with ('key' = $reseller_id) is smaller then the ALLOCATION BLOCK, return TRUE else return FALSE;
return FALSE;
}
Then your controller would look something like this:
public function add_user() {
$usertype = $this->session->userdata('usertype');
$this->load->model('reseller_m');
if ($usertype == "admin") {
$this->data['user'] = $this->user_m->get_new();
$rules = $this->user_m->rules_admin;
$rules['password']['rules'] .= '|required';
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == TRUE) {
if($this->reseller_m->check_reseller_allocation($reseller_id)){//check_reseller_allocation will return TRUE if ALLOCATION BLOCK value is not reached (so you can add another user) and FALSE if it is reached, so you cannot add another user
//if TRUE (you can add one more). then the script continues, adding the user
$data = $this->user_m->array_from_post(array('sip_id', 'sip_pass', 'name', 'key', 'email', 'password', 'phone', 'status', 'created', 'balance'));
$data['password'] = $this->user_m->hash($data['password']);
$key = $this->user_m->save($data, $id);
redirect('admin/user');
}else{//if FALSE (ALLOCATION BLOCK val is reached) display some error view; I called it 'max_allocation', but you may call it whatever you want
$this->load->view('max_allocation');}
}
$resellers = $this->reseller_m->get_drop_down();
if(count($resellers) > 0) {
foreach($resellers as $value) {
$dropdown[$value->key] = $value->key;
}
}
$this->data['resellers'] = $dropdown;
$this->data['subview'] = 'admin/user/add';
$this->load->view('admin/_layout_main', $this->data);
} else {
$this->load->view('permission');
}
}
EDIT: I had mistaken the table name in reseler_users_count(). Now it's OK. You must change 'users_tbl' with actual name of the users table. Same for 'reselers_tbl', cange it with the actual name of the resellers table. Also, both functions take as parameter the id of the reseller. In add_user() controller function, you must have that value. Maybe you should pass with $_POST.

yii save dropdown checked state on page refresh

In yii save a dropdown checked state of "yes" when the page reloads, if that is chosen? It defaults to "No".. Thanks!!
getAttributeLabel('MULTI_PART_FORM')); ?>
<td class="type-text"><?php echo CHtml::dropDownList('MULTI_PART_PO','',array( 0=>'No',1=>'Yes'), array('onChange' => "$('#MULTI_PART_VIEW').show();")); ?></td>
</tr>
<tr id="MULTI_PART_VIEW" style="display:none">
<th><?php echo CHtml::encode($model->getAttributeLabel('MULTI_PART_VIEW')); ?></th>
<td><?php echo CHtml::activeTextField($model,'MULTI_PART_PO',array('size'=>120,'maxlength'=>64,'value'=>$model->MULTI_PART_PO)); ?></td>
<?php
//VIEW CODE
echo CHtml::beginForm(Yii::app()->createUrl("{controllername}/save",array(/*'{controller parameter}'=>{parameter value},*/)),'post',array('id'=>'form_id'));
echo CHtml::dropDownList('MULTI_PART_PO',$saved_dropdown_value,array( 0=>'No',1=>'Yes'), array('onChange' => "$('#MULTI_PART_VIEW').show();"));
//dropDownList(string $name, string $select="the selected value", array $data, array $htmlOptions=array ( ))
echo CHtml::endForm();
//CONTROLLER CODE (add function Save to accessRules array)
public function actionSave(/*{controller parameter}*/)
{
$model=new {Modelname};
if(isset($_POST["MULTI_PART_PO"]))
{
$model->multi_part_po = $_POST["MULTI_PART_PO"];
$model->save();
}
$this->render('{Viewname}',array(
'saved_dropdown_value'=>$model->multi_part_po,
));
}

CakePHP only selecting last checkbox in a loop

I found other people that had the same problem when trying to select multiple checkboxes, only the last one is selected. Here is one and here is another.
People have suggested using options like this
echo $this->Form->input('video',array('options'=> array('Value 1'=>'Label 1',
'Value 2'=>'Label 2',
'Value 3'=>'Label 3'
),
'multiple' => 'checkbox'
));
I don't think either of these answer my question. My checkboxes are generated in a loop, but everyone suggested using "options" to set the values. I have no idea how many checkboxes are going to be generated so I don't see how this would work.
<table>
<?
echo $this->Form->create(null,array(
'onsubmit'=>'return confirm("Are you sure you want to archive?'));
?>
<th>Order ID</th><th>Order Date</th><th>Order Total</th><th>Status</th><th>View</th><th>Select to Archive</th>
<?php foreach ($orders as $order):
?>
<tr>
<td><?php echo $order['Order']['orderid'];?> </td>
<td><?php echo $formatted_date." ".$time; ?></td>
<td><?php echo $order['Order']['total'];?> </td>
<td><?php echo $order['Order']['order_status'];?> </td>
<td><a href="/orders/details/<?php echo $order['Order']['orderid']; ?>"/>View Order</a> </td>
<td><? echo $this->Form->checkbox('archive_value', array('hiddenField' => false, 'value' => $order['Order']['orderid'])); ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php //options for the archiving values
$archive_options = array(
'selected' => 'Archive Selected Orders',
'filled' => 'Archive All Filled Orders',
);
?>
<table class = "table_order_status">
<tr>
<td>
<?
echo $this->Form->input('archive_values', array('options' => $archive_options, 'value' => $select_value, 'name' => 'archive'));
?>
</td>
<td>
<?
echo $this->Form->end(__('Submit'));
?>
</td>
</tr>
</table>
<?
Try appending a period to the end of the input element's name, i.e. video. (derived from this forum post). This will apparently cause the elements to adopt the array name style notation, e.g. video[], though it might be Model.video[]. In any case, this should allow you to receive all of the checked values back in the form data.

Categories