Get multiple select data from Bootstrap Select using PHP - php

I'm using bootrap-select plugin
1: https://github.com/snapappointments/bootstrap-select/ I'm trying to grab data from it. I try so many ways but it doesn't work out. So the input is like this:
<div class="col-9">
<select id="generals" name="generals" class="form-control kt-selectpicker" multiple title="Choose one of the following...">
<optgroup label="Passport">
<option value="passport_number">Number</option>
<option value="passport_date">Date of issuance</option>
<option value="passport_expired">Date of expiry</option>
<option value="passport_office">Issuing Office</option>
</optgroup>
<optgroup label="Personal">
<option value="applicant_id">Applicant Id</option>
<option value="first_name">First Name</option>
<option value="first_name">Middle Name</option>
<option value="last_name">Last Name</option>
<option value="address">Address</option>
<option value="crew_id">Crew Id</option>
<option value="email">Email</option>
<option value="mobile_number">Mobile Number</option>
</optgroup>
</select>
</div>
I try to grab using PHP foreach and for loops, implode or explode but it doesn't work since it said it does not an array. When i see the Header data sent is like this:
so how can i get this data? I'm using Ajax to send data. Here's the Fiddle if you want to see it: https://jsfiddle.net/4u6xc9kd/

It's easy, you just put the input name like this name="generals[]" in your input select and the use foreach like this:
$generals = '';
if (isset($_POST['generals'])){
foreach ($_POST['generals'] as $value){
$generals .= $value . ', ';
}
}

Related

Simple Select Value will not Populate via Jscript if the word is "House"

I have a small Project website that I am learning to code with and in this website there is a Select Dropdown Menu of Music Genres
Here is a Video to help you visualise whats happening.
youtu.be/t4cJ7TCHiN8 (of the Problem)
And another video of how I can Paste "House" into the URL and get different MYSQL results
youtu.be/zf0S9WadQXo
I select a genre post the form and the reload shows me the results.
The value of the selected Option is then automatically selected by Jscript
So the Search area and its Values remain the same as before.
I have Mainly used DocumentGetElementById or jQuery to Select the value, and I am echoing PHP variables into the DocumentGetElementById function.
There is no problems with 99.9% of the values and selectable genres, they all are automatically selected without any problems.
Just 1 Problem, The value "House" Will not select.
No matter how I try it just will not select. I have of course tried other methods, including jQuery and no matter how I alter my code or method of execution.
Nothing seems to make the value "House" populate.
Here is the code. And images to help visualise the problem.
Thank you for any help you may provide.
CODE.
<select id='genregrab' name='genre' class='menusmall'>
<optgroup label='Genre'>
<option value="">All Genres</option>
<option value="EDM">EDM</option>
<option value="DnB">DnB</option>
<option value="Drum">Drum And Bass</option>
<option value="Breakbeat">Breakbeat</option>
<option value="Downtempo">Downtempo</option>
<option value="Dance">Dance</option>
<option value="Pop">Pop</option>
<option value="Disco">Disco</option>
<option value="Nu-Disco">Nu-Disco</option>
<option value="House">House</option>
<option value="Hard">Hard House</option>
<option value="Tech">Tech House</option>
<option value="Techno">Techno</option>
<option value="Deep">Deep House</option>
<option value="Future">Future House</option>
<option value="Tribal">Tribal House</option>
<option value="Tropical">Tropical House</option>
<option value="Progressive">Progressive</option>
<option value="Bass">Future Bass</option>
<option value="Bounce">Future Bounce</option>
<option value="Industrial">Industrial</option>
<option value="Electronic">Electronic</option>
<option value="Psychedelic">Psychedelic</option>
<option value="Trance">Trance</option>
<option value="Psy">Psy Trance</option>
<option value="Minimal">Minimal</option>
<option value="Ambient">Ambient</option>
<option value="Chillout">Chillout</option>
<option value="Synthwave">SynthWave</option>
<option value="Retro">Retro</option>
<option value="Hip">Hip-Hop</option>
<option value="Trip">Trip-Hop</option>
<option value="Glitch">Glitch-Hop</option>
<option value="Rap">Rap</option>
<option value="Afrobeat">AfroBeat</option>
<option value="Grime">Grime</option>
<option value="Trap">Trap</option>
<option value="Trapstep">TrapStep</option>
<option value="Dubstep">DubStep</option>
<option value="Drumstep">DrumStep</option>
<option value="Reggea">Reggea</option>
<option value="RnB">RnB</option>
<option value="Rock">Rock</option>
<option value="Metal">Metal</option>
<option value="Soul">Soul</option>
<option value="Country">Country</option>
<option value="Folk">Folk</option>
<option value="Jazz">Jazz</option>
<option value="Blues">Blues</option>
<option value="Funk">Funk</option>
<option value="World">World</option>
<option value="Gospel">Gospel</option>
<option value="Latin">Latin</option>
<option value="Ethnic">Ethnic</option>
<option value="Bollywood">BollyWood</option>
<option value="Moombahton">Moombahton</option>
<option value="Orchestral">Orchestral</option>
<option value="Classic">Classical</option>
<option value="Cinematic">Cinematic</option>
</optgroup>
</select>
Item Selected & POSTs Picked up By JScript to re-select the option
$(function(){
var genre = document.getElementById('genregrab');
var genredata = "<?php echo $genre ?>";
genre.value=genredata;
});
(Of course i initially tried the following)
document.getElementById("genregrab").value="<?php echo $genre ?>";
However unlike the others , the word "House" will not be selected.
I think its to do with the word or something, as every other word will work fine.
Same process just selecting another genre, and it works fine
As you can see , the Trance genre was selected
Just the word "House" has the problem.
And the Result after the Jscript
However , Select any other word and it works
As you can see in the image below
Any Ideas , Anybody?
I forgot to add the below code , from the GET and the MYSQL Results.
Its possible the sanitizing is effecting the variables.
The GET
if (isset($_GET['genre'])) {
$genre=$_GET['genre'];
$genre = filter_var($genre, FILTER_SANITIZE_STRING);
$genre = strip_tags($genre);
$genre = str_replace(['"',"'"], "", $genre);
} else {$genre="";}
And the MYSQL variable - Which also gets posted to the URL for GET
$genre=$row['genre'];
$genre = str_replace(['"',"'"], "", $genre);
if ($genre == "null" | $genre==" "){$genre="";}
$genresend = explode(',',trim($genre))[0];

Pass an array as a variable in php postgres query [duplicate]

I have this HTML code :
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
and to make user able to select more than one items, I'm using this jQuery plugin : http://harvesthq.github.com/chosen/
but, once it submitted... the PHP script only able to retrieve one of $_POST['cars'] value. the last one. how to make PHP to be able to retrieve ALL of it?
I've found the answer...
<select name="cars[]" multiple="multiple">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
and in the PHP part :
$cars = $_POST['cars'];
print_r ($cars);
you must do the following:
//$_POST or $_GET is the method of your form request
foreach ($_POST['cars'] as $selected_option) {
echo $selected_option;
}
that's it.

CakePHP : Multiselect input data insert into database

My Multiselect input code is
<?php echo $this->Form->input('Dispensary.role.',array('options'=>$dispensary_users,'class'=>'form-control dispensary_users_dd','label'=>false,'style'=>'width:300px;','empty'=>'Select Users'));
?>
and generated html code :
<select name="data[Dispensary][role][]" class="form-control dispensary_users_dd" style="width: 300px; display: none;" id="DispensaryRole">
<option value="">Select Users</option>
<option value="9">Yashobanta</option>
<option value="80">Yash</option>
<option value="83">Ramesh</option>
</select>
But when I select all users, and print_r($this->data);
returns last selected input.
But I want all.
I forgot to add multiple => true.
I don't know how that thing with $this->Form->input(...); works, because it's not important. You can select only one opinion without multiple attribute.
Example by w3schools.org:
<select name="cars" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
For saving the multiple select value in database, store the array using implode function.
In controller:
if (!empty($this->data)) {
$this->data['Category']['name'] = implode(",",$this->data['Category']['name']);
$this->Category->create();
$this->Category->save($this->data);
}
Hope it's help :)

Retain drop-down list values in PHP without using array

I have a drop-down list containing all the countries in the world. The first few countries are:
Country: <select name="Country">
<option value="Afghanistan">Afghanistan</option>
<option value="Aland Islands">Aland Islands</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="American Samoa">American Samoa</option>
<option value="Andorra">Andorra</option>
<option value="Angola">Angola</option>
<option value="Anguilla">Anguilla</option>
</select>
Forming an array would require over 200 elements. Is there a way to retain the drop-down value in PHP after a form post without using an array? Thank you.
You really are best using an array, it won't take any real noticable time to iterate through 200 items
Something like this should keep the selected item after post
<select name="country" method="POST">
<?php
for($i=0;$i<count($countries);$i++)
{
$selected="";
if($countries[$i]==$_POST['country'])
{
$selected="selected";
}
?><option <?php echo $selected;?> value="<?php echo $countries[$i];?>"><?php echo $countries[$i];?></option><?php
}
?>
</select>

How do I extract specific data with preg_match?

I'm looking to extract values from a whole load of html (i've just trimmed down to the relevant data), there are multiple 'select' elements, and only want to extract those who's 'name' element matches the name 'aMembers'. So the resulting values I would like to retrieve are 5,10,25 and 30 (see below) how can I achieve this with preg_match?
<DIV id="searchM" class="search"><select name="aMembers" id="aMembers" tabIndex="2">
<option selected="selected" value="">Data 3</option>
<option value="5">A name</option>
<option value="10">Another name</option>
</select>
</DIV>
<DIV id="searchM" class="search"><select name="bMembers" id="bMembers" tabIndex="2">
<option selected="selected" value="">Data 2</option>
<option value="15">A name</option>
<option value="20">Another name</option>
</select>
</DIV>
<DIV id="searchM" class="search"><select name="aMembers" id="Members" tabIndex="2">
<option selected="selected" value="">Data 1</option>
<option value="25">A name</option>
<option value="30">Another name</option>
</select>
</DIV>
I would try to split this task into 2 steps:
matching needed tags
matching needed values in them
This way would be easier:
$str = 'your HTML code here...';
preg_match_all('|<select name="aMembers".*?</select>|ms', $str, $matches);
foreach ($matches[0] as $select) {
preg_match_all('|value="(.+?)"|', $select, $matches2);
var_dump($matches2[1]);
}

Categories