Error undefined offset: 2 in php code-igniter - php

I want delete user and user role by using checkbox. first check then submit button click. After click, selected user and user_role should be deleted.
got php undefined offset 2 error on line 491
this is my model:
public function add_participation(){
$user = $this->input->post('user');
$role = $this->input->post('role');
$delete = $this->input->post('delete');
for($i=0;$i<count($user);$i++){
if($user[$i] !=""){
$this->db->where('workflow_activity_id',$this->input->post('batch'));
$this->db->where('role_id',$role[$i]);
$this->db->where('user_id',$user[$i]);
$exist = $this->db->get('workflow_participation');
$data = array(
'user_id' => $user[$i],
'role_id' => $role[$i],
'workflow_activity_id' => $this->input->post('batch'),
);
if($exist->num_rows() == 0){
$this->db->insert('workflow_participation',$data);
}else{
$this->db->where('workflow_activity_id',$this->input->post('batch'));
$this->db->where('role_id',$role[$i]);
$this->db->where('user_id',$user[$i]);
$this->db->update('workflow_participation',$data);
}
if($delete[$i] == '1'){ //**error on this line**
$this->db->where('workflow_activity_id',$this->input->post('batch'));
$this->db->where('role_id',$role[$i]);
$this->db->where('user_id',$user[$i]);
$this->db->delete('workflow_participation');
}
}
}
return true;
}
In this view page user and user_role show in drop-down.
This is my view page
<div class="form-group">
<label class="control-label col-md-3">User :</label>
<div class="col-md-8">
<select id="user" name="user[]" class="select form-control">
<option value="" selected="selected">-------</option>
<?php
if(!empty($user)){
foreach($user as $user_result){?>
<option value="<?=$user_result->id;?>" <?php if(!empty($participent) && !empty($participent[1]) && $participent[1]->user_id == $user_result->id){?>selected="selected"<?php }?>><?=$user_result->username;?></option>
<?php }}?>
</select>
</div>
<label class="control-label col-md-3">Role :</label>
<div class="col-md-8">
<select id="role" name="role[]" class="select form-control">
<option value="" selected="selected">-------</option>
<?php
if(!empty($role)){
foreach($role as $role_result){?>
<option value="<?=$role_result->id;?>" <?php if(!empty($participent) && !empty($participent[1]) && $participent[1]->role_id == $role_result->id){?>selected="selected"<?php }?>><?=$role_result->name;?></option>
<?php }}?>
</select>
</div>
<label class="control-label col-md-3">Delete :</label>
<div class="col-md-8">
<input type="checkbox" name="delete[]" value="1">
</div>

Since you are using delete[] as array, i hope your view is in a loop.
a checkbox value is submitted only when it is checked. So you will receive your input in wrong order.
ie, you selected first user and role and checked last delete, you may receive delete for first user. The second and third delete will be undefined. Please see this link for details and hidden field hack for this issue
So I suggest change the following 3 lines in your html as following. you can use $participent[1]->user_id ( or a counter) as index in view file.
<select id="user" name="user[<?php echo $participent[1]->user_id;?>]" class="select form-control">
...
<select id="role" name="role[<?php echo $participent[1]->user_id;?>]" class="select form-control">
...
<input type="checkbox" name="delete[<?php echo $participent[1]->user_id;?>]" value="1">
change the loop from this
for($i=0;$i<count($user);$i++){
to
foreach( $user as $i => $unused ) {
Then instead of equal checking
if($delete[$i] == '1'){ //**error on this line**
use
if( isset($delete[$i]) ){ //**error on this line**
// if( $delete[$i] == '1' ){ //uncomment if you need double confirmation

First check if the variable $role have the same amount of data as $user, if it's not so you have a problem.
Next for the line for($i=0;$i<count($user);$i++), create an outside variable for your count, here everytime you enter the for, the count() is called, bad optimisation.

Related

How to display form field on condition using PHP in CodeIgniter?

I have select input in my form for manufacturers.
<div class="form-group">
<label for="manufacturer">Manufacturer</label>
<select id="manufacturerSelect" name="manufacturer" class="form-control">
<option disabled selected value> -- select an manufacturer -- </option>
<?php foreach ($manufacturers as $manufacturers_item): ?>
<option value="<?=$manufacturers_item['id'];?>" <?php echo set_select('manufacturer',$manufacturers_item['id'], ( !empty($manufacturer) && $manufacturer == $manufacturers_item['id'] ? TRUE : FALSE )); ?> ><?=$manufacturers_item['name'];?></option>
<?php endforeach; ?>
<option disabled>──────────</option>
<option value="24" <?php echo set_select('manufacturer','24', ( !empty($manufacturer) && $manufacturer == '24' ? TRUE : FALSE )); ?> >Other</option>
</select>
<?php echo form_error('manufacturer'); ?><br />
</div>
If "other" (value == 24) is checked additional input is asked:
$('body').on('change', '#manufacturerSelect', function() {
if ($(this).val() == 24) {
$("#otherManufacturerSelect").removeClass('hidden');
} else {
$("#otherManufacturerSelect").addClass('hidden')
}
});
And HTML:
<div id="otherManufacturerSelect" class="form-group">
<label for="otherManufacturer" >What is it then?</label>
<input type="text" name="otherManufacturer" class="form-control">
<?php echo form_error('otherManufacturer'); ?><br />
</div>
CSS:
.hidden {
display: hidden;
}
Now if user picks "other" as manufacturer addition input is displayed. Form validation rule for otherManufacturer is added in server side if manufacturer == 24. The problem is that the other manufacturer input is displayed every time user get response from server. I could add class="hidden" by default to other manufacturer div but if the form validation doesnt run other manufacturer field will not be displayed again to user.
What I need is PHP IF condition inside:
<div id="otherManufacturerSelect" <?php if(/*???*/):?>class="hidden"<?php endif; ?> class="form-group">
So that class="hidden" would be added only if manufacturer is not "other". but I cannt think of rigth condition.
Any help would be appreciated!
EDIT
Controller:
public function create()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('manufacturer', 'Manufacturer', 'required');
if($this->input->post('manufacturer') == '24'){
$this->form_validation->set_rules('otherManufacturer', 'Other manufacturer', 'required');
}
$data['manufacturers'] = $this->puzzles_model->getManufacturers();
if ($this->form_validation->run() === FALSE)
{
$this->load->view('puzzles/create', $data);
}
else
{
/* do the upload, return upload errors or save in db*/
}
}
In your particular case this would fix the problem:
<div id="otherManufacturerSelect" class="form-group <?php if(isset($manufacturer) && $manufacturer !== '24') { echo 'hidden'; } ?> ">
<label for="otherManufacturer" >What is it then?</label>
<input type="text" name="otherManufacturer" class="form-control">
<?php echo form_error('otherManufacturer'); ?><br />
</div>
Then you can remove the JS snippet. The additional form will be hidden on server side (class="hidden" will be set).
I saw that you're using var $manufacturer in the same template. I can't see your controller and how you're passing variables but instead of $manufacturer you can also use $_GET['manufacturer'] or $_POST['manufacturer'] (depending on your form action method).
Notice: $_GET['manufacturer'], $_POST['manufacturer'] and $_REQUEST['manufacturer'] is NOT sanitized input. When using $manufacturer I assume that it's sanitized in your controller.

How to pass default values from codeigniter application to different website

Error
Not able to pass the value from this application to other website.
View
In this view using action i have called the controller function. If the user selects Pay-Me then value 1 will be passed to controller function.
<form id="formMuktinath" method="post" action="<?php echo base_url().'index.php/booking/bookManakamana'?>">
<div class="form-group">
<label for="name" class="col-sm-3 col-lg-offset-2 control-label">Payment From </label>
<div class="col-sm-4">
<select class="select form-control" name="manakamana[payment_from]">
<option value="1" selected>Pay-Me</option>
<option value="2">BIL</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-5">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Controller
If 1 is selected then it calls payme website. Then there redirect function using $data does not pass amt value to payme website.
public function bookManakamana(){
if ($data = $this->input->post('manakamana')) {
$data['created_on'] = date('Y-m-d H:i:s');
if ($data['payment_from'] == '1') {
$data['amt'] = '100';
redirect('http://dev.payme.com/pay/main',$data);
}
if ($data['payment_from'] == '2') {
echo 'bli';
exit;
}
redirect('booking/index');
} else {
$this->load->view('index');
}
}
if you want to pass values in link as get variable
www.somewebsite.com/?var='value'
but if you want to send variable as post you need to use curl

Can you assign values to hidden fields in a form, through a php function, to be stored in a database?

I created a form you know; text fields, radio buttons and the submit button
within the said form, I have a div enclosing a section of the radio buttons hidden and a text field upon page load using inline CSS display:none;
If the end user chose yes, the hidden fields will be displayed using a jquery function. If the user chose No or Not Sure, the form will remain hidden or become hidden using the same jquery function.
If the user chose No or Not Sure, i want to automatically assign values for the hidden fields and store them in database.
Here is my form:
<div id = "relation" style = "display: none;">
<p class= "form-p" >Who are you related o?</p>
<div class="form-group">
<label class="col-sm-2 control-label"></label>
<div class="col-sm-4">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default">
<input type="radio" autocomplete="off" name="family" value="Bride" required />Bride
</label>
<label class="btn btn-default">
<input type="radio" autocomplete="off" name="family" value="Groom" required />Groom
</label>
<label class="btn btn-default">
<input type="radio" autocomplete="off" name="family" value="Friend" required />Friend
</label>
<span class="error"><?php echo $famErr;?></span>
</div>
</div>
</div>
<p class = "form-p">Guests in your party, including yourself: </p>
<div class = "form-group">
<label class="control-label col-sm-4"></label>
<div class="col-xs-2">
<input type = "text" class = "form-control" name="num" placeholder = "0" required />
<span class="error"><?php echo $numErr;?></span>
</div>
</div>
</div> <!-- end of id relation-->
Here are the functions:
// function to add RSVP user entry to the database
public function user_attending_storage_RSVP($name, $email, $attend, $fam, $num){
$replies = "INSERT INTO rsvp (name, attending, family, total) VALUES (:name,:attending,:family,:total)";
try{
$query = $this->conn->prepare($replies);
$results = $query->execute(array(":name"=>$name, ":attending"=>$attend, ":family"=>$fam, ":total"=>$num));
}
catch(PDOException $e){
die($e->getMessage());
}
}
// function to add RSVP user entry to the database
public function user_not_attending_storage_RSVP($name, $email, $attend){
$replies = "INSERT INTO rsvp (name, attending, family, total) VALUES (:name,:attending,:family,:total)";
try{
$query = $this->conn->prepare($replies);
$results = $query->execute(array(":name"=>$name, ":attending"=>$attend, ":family"=>$fam, ":total"=>$num));
}
catch(PDOException $e){
die($e->getMessage());
}
}
Here's how I call the function on the webpage
// check for data in fields
if(isset($_POST['name']) ==true && isset($_POST['email']) ==true && isset($_POST['attending']) && isset($_POST['family']) && isset($_POST['num'])){
$name=test_input($_POST['name']);
$email=test_input($_POST['email']);
$attend=test_input($_POST['attending']);
$fam=test_input($_POST['family']);
$num=test_input($_POST['num']);
if($attend == "No" || $attend == "Not Sure"){
$fam = "nothing";
$num = 0;
//inserting user data from form into database
$genQuery->user_Not_attending_storage_RSVP($name, $email, $attend);
}
else{
//inserting user data from form into database
$genQuery->user_attending_storage_RSVP($name, $email, $attend, $fam, $num);
}
// send mail to user
$genQuery->user_invite_confirmation_RSVP($name, $email, $attend,$fam, $num);
}
You can use the getElementById() method...
if(getElementById('input_id').value() == 'no' || getElementById('user_id').value() == 'not sure'){getElementById('input_you_want_to_change).value('Defualt value')}
This will change the value if the user selects no or not sure. Then use PHP to store this new value in the database.

dropdown select value in yii

I am currently trying to create a sample shopping site,The problem came when I created a search page.In my search page I Have two fields one for enter the keyword and other for select type of item
Which means search,keyword in the selected item.but the item defined in DB is 0 and 1 and i Trying to get it my code but fails...could any one help me...
The Gfunction code is
public static function item(){
$optionList='';
$CategoryList = OfferEvents::model()->findAllByAttributes(array());
foreach($CategoryList as $catdata)
{ $optionList.="<option value='".$catdata['type']."'>".$catdata['name']."</option>"; }
return $optionList;
}
and view code is
<form action="<?php echo Yii::app()->baseUrl; ?>/offer?>" method="GET" class="form-inline form-section-2 row fadeInDown animated">
<div class="col-sm-5 form-group">
<input type="text" name="search" class="form-control" id="search" value="" placeholder="Enter Your Keyword">
</div>
<div class="col-sm-4 form-group">
<select name="cat" class="form-control selectpicker">
<option value='0'>Select type</option>
<?php echo GFunctions::item(); ?>
</select>
</div>
<div class="col-sm-3 form-group">
<button type="submit" class="btn btn-default btn-new">Search</button>
</div>
</form>
why don't you use Yii built in functions to handle the dropdown?
I am not sure how your Models looks like but you can choose one of the following functions to achieve what you want instead of trying to return a String $optionList with HTML tags.
CHtml::activeDropDownList() // for directly bind to active record model
CHtml::dropDownList()
Try this code....
Function
public static function item(){
$CategoryList = OfferEvents::model()->findAllByAttributes(array());
$optionList = CHtml::listData($countries, 'type', 'name')
return $optionList;
}
In view
<?php echo CHtml::dropDownList('cat', '', GFunctions::item(), array('prompt'=>'Select type'));?>

Cant get values of checkboxes in codeigniter

Im trying to insert multiple checkboxes but cant get their values in codeigniter 2
this is my code in View
<!-- Multiple Checkboxes (inline) -->
<div class="form-group">
<div class="col-md-4">
<label class="checkbox-inline" for="checkboxes-0">
<input type="checkbox" name="checkboxes[]" id="checkboxes-0" value="22">
Пентхаус
</label>
<br>
<!-- Text input-->
<div class="form-group">
<div class="col-md-8">
<input id="cena" name="cena[]" type="text" placeholder="Въведи цена" class="form-control input-md">
</div>
</div>
<label class="checkbox-inline" for="checkboxes-1">
<input type="checkbox" name="checkboxes[]" id="checkboxes-1" value="21">
Гараж/Паркомясто
</label>
<br>
<!-- Text input-->
<div class="form-group">
<div class="col-md-8">
<input id="cena" name="cena[]" type="text" placeholder="Въведи цена" class="form-control input-md">
</div>
</div>
This is my Model:
public function InsertCheckbox() {
$property_typesRequest = $this->input->post('checkboxes');
foreach($property_typesRequest as $value){
$this->db->insert_batch('property_type_details', $property_typesRequest);
}
}
in Controller i just use this:
$this->estate_m->InsertCheckbox();
And this going insert 0 in database, when i var_dump $property_typesRequest theres shows bool(false). I cant get values of checkboxes...
EDIT...
I have try to edit my code but still no result:
public function edit()/*this is controller */
{
$data=array('column_name'=>$this->input->post('checkboxes');
$result=$this->estate_m->InsertCheckbox($data);
if($result==true)
{
echo "Success";
}
else
{
echo "Fail";
}
}
public function InsertCheckbox($data) /*this is Model */
{
$this->db->insert('property_type_details', $data);
return ($this->db->affected_rows() != 1 ) ? false : true;
}
With this edited code always gives me Succes
Form submitted values should be in multi dimension array
To achieve that your form inputs should be in multi dimension.
For insert_batch() function, the array should be in multi dimension.
In array every key must be field names in db table and value must be the form input value.
So change the form structure like the below array.
array(
array(
'checkboxes' => 'checkboxe value' ,
'cena' => 'cena values'
),
array(
'checkboxes' => 'checkboxe value' ,
'cena' => 'cena values'
)
);
Form inputs should be like below:
<input name="data[1][checkbox_columnname]" type="checkbox" value="21">Пентхаус
<input name="data[1][textbox_columnname]" type="text">
<input name="data[2][checkbox_columnname]" type="checkbox" value="22">Гараж/Паркомясто
<input name="data[2][textbox_columnname]" type="text">
And the model should not have foreach loop.
Simply pass the data as below.
$data=$this->input->post('data');
$this->db->insert_batch('mytable', $data);
Please use this code in model.
public function InsertCheckbox() {
$property_typesRequest = $this->input->post('checkboxes');
foreach($property_typesRequest as $value){
$data['columnename'] = $value;
$this->db->insert('tablename', $data);
}
}
Hope it inserts into the database
Thank you

Categories