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

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

Related

How to push action form with variable of select?

I want to take value from select in html and push it into action url.
So when users select something value of action buyoption will be the value of the selection
I would prefer to do it with php if there is no option to do it with html.
This is my test buy it doesn't change the buyoption to selection variable..
please help
<form action="<?php echo URLROOT; ?>/trades/trade/buyoption/selloption" method="POST">
<div class="row">
<div class="col-md-9 register-right" >
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<h3 class="register-heading">TRADE</h3>
<div class="row register-form">
<div class="col-md-12">
<label for="buy"><b>YOU SEND:</b></label>
<div class="form-group">
<select class="form-control" name="buyoption" id="buyoption">
<option class="hidden" selected disabled>Please select what you want to sell</option>
<option value="btc">Bitcoin (BTC)</option>
<option value="ltc">LITECOIN (LTC)</option>
<option value="eur">NATIONAL BANK TRANSFER (EUR/USD/HRK)</option>
</select>
</div>
<label for="buy"><b>YOU RECEIVE:</b></label>
<div class="form-group">
<select class="form-control" name="selloption" id="buyoption">
<option class="hidden" selected disabled>Please select what you want receive</option>
<option value="btc">Bitcoin (BTC)</option>
<option value="ltc">Litecoin (LTC)</option>
<option value="eur">NATIONAL BANK TRANSFER (EUR/USD/HRK)</option>
</select>
</div>
<input type="submit" class="btnRegister" value="Proceed"/>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
There must not be multiple elements in a document that have the same id value.
You can watch for change event of select element and on Change, manipulate the Action URL.
let buyoption = document.getElementById('buyoption');
let selloption = document.getElementById('selloption');
let postForm = document.getElementById('postForm');
let action = postForm.getAttribute('action');
let replacerFn = function() {
let temp = action;
if (buyoption.value) {
temp = temp.replace('buyoption', buyoption.value);
}
if (selloption.value) {
temp = temp.replace('selloption', selloption.value);
}
postForm.setAttribute('action', temp);
console.log('Action Attribute=> ', postForm.getAttribute('action'));
};
buyoption.addEventListener('change', function() {
replacerFn();
});
selloption.addEventListener('change', function() {
replacerFn();
});
<form action="/trades/trade/buyoption/selloption" method="POST" id="postForm">
<div class="row">
<div class="col-md-9 register-right">
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<h3 class="register-heading">TRADE</h3>
<div class="row register-form">
<div class="col-md-12"> <label for="buy"><b>YOU SEND:</b></label>
<div class="form-group">
<select class="form-control" name="buyoption" id="buyoption">
<option class="hidden" selected disabled value="">Please select what you want to sell</option>
<option value="btc">Bitcoin (BTC)</option>
<option value="ltc">LITECOIN (LTC)</option>
<option value="eur">NATIONAL BANK TRANSFER (EUR/USD/HRK)</option>
</select>
</div><label for="buy"><b>YOU RECEIVE:</b></label>
<div class="form-group">
<select class="form-control" name="selloption" id="selloption">
<option class="hidden" selected disabled value="">Please select what you want receive</option>
<option value="btc">Bitcoin (BTC)</option>
<option value="ltc">Litecoin (LTC)</option>
<option value="eur">NATIONAL BANK TRANSFER (EUR/USD/HRK)</option>
</select>
</div><input type="submit" class="btnRegister" value="Proceed" /> </div>
</div>
</div>
</div>
</div>
</div>
</form>

how to filter and validate select option in form using PHP

$batch = htmlentities($_POST['batch']);
if($batch === 'NULL'){
echo "Batch Failed";
} else {
echo "$bacth";
}
<div class="col-md-12">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-globe"></i></span>
<select class="form-control" name="batch" id="select">
<option selected disabled>Batch Preference</option>
<option value="Weekends">Weekends</option>
<option value="Holidays">Holidays</option>
</select>
</div>
</div>
</div>
How to select those select options in form using php, how i can validate them...
Thank You

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);
}
*/

Code Igniter - Select tag option depending on another select tag value

I'm new in CodeIgniter and still junior in programming, so don't be angry at me.
PS: don't be angry for my English too. ;)
I am creating small CMS for one project. Inside that project its few select tags, first ones have just a few values but the next ones, have many values, which depends to the one of the values, from select tag that i mention before.
Its everything ok with my database tables dependencies, etc. but I dont know how to make dependencies to show up in select tags. I tried to make it with Ajax, but I am still not make it, and I read that I can make it with codeIgniter libraries, is it true? Can any body can help me?
This is my view file:
<?php echo validation_errors('<p class="alert alert-dismissable alert-danger">'); ?>
<form method="post" action="<?php echo base_url(); ?>admin/engines/add">
<div class="row">
<div class="col-lg-6">
<h1>Engine Type</h1>
</div>
<div class="col-lg-6">
<div class="btn-group pull-right">
<input type="submit" name="submit" class="btn btn-success" value="Save">
Close
</div>
</div>
</div><!-- /.row -->
<div class="row">
<div class="col-lg-12">
<ol class="breadcrumb">
<li><i class="fa fa-dashboard"></i>Dashboard</li>
<li><i class="fa fa-pencil"></i>Variklio Tipai</li>
<li class="active"><i class="fa fa-plus-square-o"></i>Pridėti Variklio tipą</li>
</ol>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<h2>Engine type One</h2>
<div class="form-group">
<label>Engine Name</label>
<input class="form-control" type="text" name="engine" value="<?php echo set_value('engine'); ?>" placeholder="Engine Type" >
</div>
<div class="form-group">
<label>Category</label>
<select name="brand" class="form-control">
<option value="category">Choose category</option>
<?php foreach($categories as $category) : ?>
<option value="<?php echo $category->id; ?>"><?php echo $category->category_name; ?></option>
<?php endforeach; ?>
</select>
</div>
<!-- END Category-->
<!-- *** Brand *** -->
<div class="form-group">
<label>Brand</label>
<select name="brand" class="form-control">
<option value="">Choose Brand</option>
<?php foreach ($brands as $brand) : ?>
<option value="<?php echo $brand->id; ?>"><?php echo $brand->v_brand_name; ?></option>
<?php endforeach; ?>
</select>
</div>
<!-- **** END Brand ***-->
<!-- *** Model *** -->
<div class="form-group">
<label>Model</label>
<select name="model" class="form-control">
<option value="">Choose model</option>
<?php foreach ($models as $model) : ?>
<option value="<?php echo $model->id; ?>"><?php echo $model->v_model_name; ?></option>
<?php endforeach; ?>
</select>
</div>
<!-- **** END MODEL ***-->
</div>
</div>
</div>
</form>
for example this is my view
<select name='list1' id='list1'>
<option value='1'>id 1</option>
<option value='2'>id 2</option>
<option value='3'>id 3</option>
<option value='4'>id 4</option>
<option value='5'>id 5</option>
</select>
this is 2nd dropdown list
<select name='list2' id='list2'>
</select>
now jquery code is
$("#list1").on("change",function(){
var value = $(this).val();
$.ajax({
url : "example.com/welcome/get_data",
type: "post",
data: {"value":value},
success : function(data){
$("#list2").html(data);
},
});
});
now my controller function is
public function get_data()
{
$value = $this->input->post("value");
$data = $this->mymodal->get_data($value);
$option ="";
foreach($data as $d)
{
$option .= "<option value='".$d->id."' >".$d->name."</option>";
}
echo $option;
}
my model function is
public function get_data($value)
{
$this->db->where("id",$value);
return $this->db->get("table_name")->result();
}

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