$_POST gives index undefined.. how to resolve? - php

I am doing a project in codigniter and I have passed a variable from my controller to view. In view, I am populating a select with the data I passed so when I try to go to next page by using one of the ids from my array (by using $_POST) I always get index undefined. I am new to this and any help is appreciated.
Here is my code:
<form method ="post" action ="<?php echo base_url(); ?>test/<?php if(isset($_POST['name'])){$var = $_POST['name'];echo $_POST['name'];}else{$var = null;echo "No name";}?>">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label for="form-field-select-1">Name</label>
<select id="name" name="name" class="chosen-select form-control">
<?php foreach ($names['list_names'] as $p) { ?>
<option value="<?php echo $p['id']; ?>" title="<?php echo $p['fname']; ?>"><?php echo $p['lname'].' '.$p['fname']; ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
<button type ="submit" class="btn red" style="height: 40px; border-radius:4px;">Continue</button>
<div class="space-10"></div>
</form>

Related

How to display data from database using JQuery Repeater and php

I have been tying to make JQuery repeater plugin works fetching data from database. I already have this working inserting the data into the database but I have not look retrieving the inserted data to edit it.
This is the code I have in php and html.
<div id="repeater">
<div class="repeater-heading" align="left">
<a role="button" class="btn btn-info repeater-add-btn" ><span class="glyphicon glyphicon-plus"> </span>Add Transactional Impact</a>
</div>
<div class="clearfix"></div>
<br>
<div class="items" data-group="impacts">
<div class="item-content">
<div class="form-group">
<?php
$query_raw_num = "SELECT * FROM revenue_impact_number WHERE revenue_impact_id='".$id."' ";
$results = $mysqli->query($query_raw_num);
while($impacts = $results->fetch_assoc()){
?>
<div class="row" data-repeater-item>
<div class="col-md-2">
<label>Region:</label>
<select data-skip-name="true" data-name="region_id[]" id="region_id" class="form-control">
<option value="">-- Select --</option>
<?php
$query_raw = "SELECT regions_id, regions_name FROM regions ";
$result = $mysqli->query($query_raw);
while($row = $result->fetch_assoc()){
?>
<option value="<?php echo $row['regions_id']; ?>" <?php echo ($row['regions_id']==$impacts['region_id'] ? 'selected' : ''); ?> ><?php echo $row['regions_name']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-2">
<label>Product:</label>
<select data-skip-name="true" data-name="product_id[]" id="product_id" class="form-control">
<option value="">-- Select --</option>
<?php
$query_raw_products = "SELECT product_id, product_name FROM products ";
$result_products = $mysqli->query($query_raw_products);
while($products = $result_products->fetch_assoc()){
?>
<option value="<?php echo $products['product_id']; ?>" <?php echo ($products['product_id']==$impacts['product_id'] ? 'selected' : ''); ?> ><?php echo $products['product_name']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-2">
<label>Impact:</label>
<input type="text" data-skip-name="true" data-name="impact_number[]" id="impact" value="<?php echo $impacts['impact_number'];?>" class="form-control" style="width:200px;"/>
</div>
<div class="col-md-2">
<label>Expected:</label>
<input type="text" data-skip-name="true" data-name="expected[]" id="expected" value="<?php echo $impacts['expected'];?>" class="form-control" style="width:200px;"/>
</div>
<div class="col-md-2" style="margin-top:24px;" align="center">
<a id="remove-btn" role="button" onclick="$(this).parents('.item-content').remove()" class="btn btn-danger" ><span class="glyphicon glyphicon-trash"> </span>Remove</a>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
</di>
This is the script function I am using.
$(function(){
$('#repeater').createRepeater();
});
When the page load for the first time everything is displayed successfully. See below image
The issue I am having is that when I try to add new group of fields it is displaying the whole group again and when I click on remove button it is removing the whole data group as well. See below image.
While retrieving the existing data while edit there is an issue in repeater code. Comment below lines from "repeater.js" files
/*
if (key == 0) {
items.remove();
addItem(newItem, key);
}
*/

Can not send Multiple choices from select2 with fromdata to upload file

I have html select2,it can selected multiple option:
<div class="form-group">
<label class="col-sm-1 control-label no-padding-right" for="form-field-recipient">Recipient:</label>
<div class="col-sm-6 col-xs-12">
<span class="input-icon block col-xs-12 no-padding">
<select name="for" id="for" class="for" multiple="multiple" placeholder="For" style="width: 92%">
<?php
foreach($this->forList as $dataFor){
?>
<option value="<?php echo $dataFor['ohp_id'];?>"><?php echo $dataFor['title'];?> Of <?php echo $dataFor['name'];?></option>
<?php } ?>
<?php
foreach($this->group as $group){
?>
<option value="<?php echo $group['object_group_id'];?>"><?php echo $group['object_group_name'];?></option>
<?php } ?>
</select>
<i class="ace-icon fa fa-user col-xs-0"></i>
</span>
</div>
</div>
$(document).ready(function() {
$(".refnd").select2({placeholder: "Reference ND"});
$(".for").select2({placeholder: "For"});
And this jquery:
But in PHP,it just show 1 option selected. How To fix it?
Change the dropdown name as an array:
<select name="for" id="for" class="for" multiple="multiple" placeholder="For" style="width: 92%">
<select name="for[]" id="for" class="for" multiple="multiple" placeholder="For" style="width: 92%">
JS:
var selectedOpt = $('#for').val(); // it will return an array of selected options
// Now use the selectedOpt array in your ajax call

Unable to determine why I am unable to update a record with php?

Using PDO I am attempting to update a particular database record via a form that is in part created by php. However, when I hit the submit button the page redirects to .
The Relevent Controller function:
public function updateRecord ($selectTable, $value) {
include ('View/selectTable.php');
$tableFields = $this->db->getTableFields ($selectTable);
$results = $this->db->getRecord($selectTable, $value);
echo $_POST['saveContactForm'];
include '/View/updateRecord.php';
}
View:
<form class="form-horizontal" method="POST" id= "saveContactForm">
<?php for ($a = 1; $a < count ($tableFields); $a++):?>
<div class="form-group">
<label class="col-sm-2 control-label" for=" <?php echo $tableFields [$a] ?>"><?php echo $tableFields [$a] ?></label>
<div class="col-sm-10">
<input type="text" class="form-control" id="<?php echo $tableFields[$a] ?>" placeholder="<?php echo $results[0][$tableFields[$a]] ?>">
</div>
</div>
<?php endfor ?>
<input type ="submit" class ="btn btn-primary">
</form>
I am not getting error or warnings. However, I did discover that regardless of where I put the echo $_POST['saveContactForm'] I am unable to get the post variables.

How to print the value that is selected in dropdown list in PHP or set it as selected?

I want to set the value to selected that has been selected in dropdown list and also want to print that value that has been submitted in that form.
When i print print_r[$value];
i just get some values.
Below is my code:
<form method="post" action="" id="date_date" name="date_data">
<div class="col-sm-4">
<h3><label for="sel1">Select From Date:</label></h3>
<h3><?php echo $this->input->post('date_data');?></h3>
<input type="text" id="datepicker" name="date_data">
</div>
<div class="col-sm-4">
<h3><label for="sel1">Select To Date:</label></h3>
<h3><?php echo $this->input->post('to_data');?></h3>
<input type="text" id="datepicker_to" name="to_data">
</div>
<div class="col-sm-4">
<?php
$user_type = $this->session->userdata('user_type');
if($user_type =="admin")
{
?>
<div class="form-group">
<h3><label for="sel1">Select Employoee Name:</label></h3>
<select name="attendance-list" id="attendance-list" class="form-control" >
<?php foreach($attendance_dropdown as $value)
{
?>
<option id="emp" value="<?php echo $value['emp_id'];?>"><?php echo $value['emp_name'];?></option>
$dropdown = str_replace("value='".$rid."'","value='".$rid."' selected=\"selected\"",$dropdown);
<?php
}
?>
</select>
<?php echo "<br>"; ?>
</div>
<?php
}
else
{
?>
<div class="form-group">
<input type="hidden" name="attendance-list" value="<?php echo $this->session->userdata('emp_id'); ?>">
</div>
<?php
}
?>
</div> <!--Col-sm-8-->
<div class="col-sm-4">
<h3><label for="sel1"></label></h3>
<input type="submit" value="submit" class="btn btn-primary">
</div>
</form>
Here is your answer, it will select your last select dropdown state...
Remove id from option <option id="emp" use class if you want, or use unique id.
<select name="attendance-list" id="attendance-list" class="form-control" >
<?php
$save_drop_down_value = $this->input->post('attendance-list"');
//change this line according to your project..
foreach($attendance_dropdown as $value) {
$select = "";
if( $save_drop_down_value == $value['emp_id']){
$select = "selected";
}
?>
<option <?php echo $select; ?> value="<?php echo $value['emp_id'];?>">
<?php echo $value['emp_name'];?>
</option>
<?php } ?>

Ajax Serialize Not Getting All Values of form

So i have form having some input fieldd and it is submitting via Ajax Serialize.
The problem is It is not taking all the values of the form in serialize output.I know i have give Name for each field, Here is my code for HTML :
<form action="" id="BindData">
<input type="hidden" name='style_id' id='style_id' value="<?php echo $model->style_id; ?>">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Select Category:</span>
<select id="category" name="cat_id" class="form-control">
<option value="">Select Category</option>
<?php foreach($all_categories['models'] as $Category) { ?>
<option value="<?php echo $Category->category_id; ?>"><?php echo $Category->category_name; ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="form-group tr_sub_cat" style="display:none;">
<div class="input-group">
<span class="input-group-addon">Select SubCategory:</span>
</div>
<div class="input-group">
<span class="sub_category"></span>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Weightage:</span>
<div class="col-xs-1">
<input type="text" name="weightage" id="weightage" class="form-control" value="0"><span class="badge bg-red">%</span>
</div>
</div>
</div>
<button type="button" class="btn btn-primary addBind pull-left">Submit</button>
</form>
In My Above code The serialize method is not taking the values of cat_id & weightage while i have given them name.
The sub category loaded from cat_id select box is coming in serialize data and changes in weightage value is not coming in out put.
Here is my output of serialize :
style_id=1&cat_id=&sub_cat=95&weightage=0
cat_id is blank and weightage is still 0, not the updated one.
Thanks in advance.
you can use val function to collect data from inputs:
var cat = $("#cat_id").val();
var weightage = $("#weightage").val();
you shouldn't have to do it manually for every field. That's what serialize is for.
you can use above code for debugging to check correct form value save in variable or not.

Categories