I have a huge number of records in a drop-down box(approximately 27000 or more).
HTML -
<select name="manufacturer" id="manufacturer">
<option value="">Select a Builder</option>
<option value="1-hull">#1 Hull</option>
<option value="49er">49er</option>
<option value="a-m-f">A M F</option>
<option value="a-m-manufacturing">A&M Manufacturing</option>
<option value="a1-custom-trailers">A1 Custom Trailers</option>
- - - - - -
- - - - - -
<option value="zuma">Zuma</option>
</select>
Above drop-down I collect this data from different website. I want to put in my own website with dynamic content. I have tried to insert one by one it is not possible to insert all record in single handed. So I want to any shortcut method for insert all these text in my MySQL database table. I need option inner HTML text will inserting into my database table.
If I will get all the option text in an Array then easily I can manage it. But I am confused how to get all text in Array by using PHP code.
Here's a solution with preg_match_all
<?php
$string = '<option value="">Select a Builder</option>
<option value="1-hull">#1 Hull</option>
<option value="49er">49er</option>
<option value="a-m-f">A M F</option>
<option value="a-m-manufacturing">A&M Manufacturing</option>
<option value="a1-custom-trailers">A1 Custom Trailers</option>
<option value="zuma">Zuma</option>
';
preg_match_all('/>(.*?)<\//',$string,$matches);
var_dump($matches[1]);
?>
DOM Document is your buddy,
$str = '<select name="manufacturer" id="manufacturer">
<option value="">Select a Builder</option>
<option value="1-hull">#1 Hull</option>
<option value="49er">49er</option>
<option value="a-m-f">A M F</option>
<option value="a-m-manufacturing">A&M Manufacturing</option>
<option value="a1-custom-trailers">A1 Custom Trailers</option>
<option value="zuma">Zuma</option>
</select>';
$doc = new DOMDocument();
$doc->loadHTML($str);
$options = $doc->getElementsByTagName('option');
foreach ($options as $option) {
$value[] = $option->nodeValue;
}
$value; // will contain array with all option values
Why you should not parse HTML with regex?
Related
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];
I want to save value as Hsn/product code and option text as product.
i defined value for hsn because i want to show it on my active webpage as Selected option Hsn code.
but problem is that when i submit form, then i recieve same value in productName & ProductHSN in our Database
And i want to store productName as saree, lehga, etc
And ProductHSN as W1, M1,etc .
plase help
<select class="form-control" id="colTwo2" name="colTwo[]" data-
type="productName" required="">
<option value=""> --SELECT-- </option>
<option value="W1">Saree</option>
<option value="W2">Lehga</option>
<option value="G1">Kurti</option>
<option value="G2">Salwar Suit</option>
<option value="G3">Lagi</option>
<option value="G4">Hairam</option>
<option value="G5">Long Suit</option>
<option value="W3">Goun</option>
<option value="W10">Chunri</option>
<option value="M1">Suiting</option>
<option value="M2">Shirting</option>
<option value="G6">Jeans</option>
<option value="M4">T-Shirt</option>
</select>
You are getting same value because the value in option value="" is only sent. Not the option text.
You could use PHP explode function.
HTML
<select class="form-control" id="product" name="product" required>
<option value=""> --SELECT-- </option>
<option value="W1|Saree">Saree</option>
<option value="W2|Lehga">Lehga</option>
<option value="G1|Kurti">Kurti</option>
<option value="G2|Salwar Suit">Salwar Suit</option>
</select>
PHP
<?php
$result = $_POST['product'];
$result_explode = explode('|', $result);
$producthsn = $result_explode[0];
$productname = $result_explode[1];
?>
In the select box we are giving 2 options with a '|' separator to split the values. In action page result is exploded into an array. You can then insert each one separately in the database.
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 . ', ';
}
}
i'm having the following html markup:
<select id=gender>
<option value=''>Please select</option>
<option value='m'>male</option>
<option value='f'>female</option>
</select>
i want to set the value using the simple html dom parser - here's my code - which doesn't work:
$combo = $el->find("#gender",0);
$combo->value = "m";
i also tried $combo->setAttribute('value', 'm'); without success
any ideas?
thanks
<select> has no value attribute. What you need to do is find the option you want selected (e.g. something like #gender/option[value='m']) and set the selected attribute on that.
In order to set the selected option out of a <select>, you need to set selected on the corresponding <option>:
<select id=gender>
<option value=''>Please select</option>
<option value='m' selected>male</option>
<option value='f'>female</option>
</select>
I have following code in a html form
<select name="category" class="input" onchange="ShowTB(this,'suggest');">
<option value="0" selected="selected">
[choose yours]
</option>
<optgroup label="Item">
<option value="SubItem1"SubItem1</option>
<option value="SubItem2">SubItem2</option>
</optgroup>
<option value="Item2">Item2</option>
<optgroup label="Item3">
<option value="SubItem4"SubItem4</option>
<option value="SubItem5">SubItem5</option>
</optgroup>
<option value="Item4">Item4</option>
<option value="Item5">Item5</option>
<option value="Item6">Item6</option>
<option value="Item7">Item7</option>
</select>
in php i get the value of field selected with:
$category = $_POST['category'];
in this mode if i select in the form ie: SubItem1 , in php i get value SubItem1 but i want also get associated label ie: Item or if i select SubItem5 i get SubItem5 but i want also get associated label ie: Item3
How to ?
Indeed, you only get the value. If you need more, just encode whatever you want into the value, for example:
<option value="Item3.SubItem5">SubItem5</option>
Alternatively, you could use javascript to catch onChange events on the select field and update a hidden input field with the desired label.
you could make the values arrays e.g.
<option value="Item3[SubItem5]">SubItem5</option>
so then your $_POST['category'] should return an array