Retain drop-down list values in PHP without using array - php

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>

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];

PHP Country Option list adding "select" tag with checking String value?

I want to select a target country in html.
And this code should return for example "Turkey" from my db.
<?php echo $_SESSION['user']['www']?>
I have a country list, and I use it here in settings_myinfo.php :
<select id="profile-country" name="profile_country">
<option value="Select your Country">Select your Country</option>
<?php include($root."countries_optionlist.php");?>
</select>
countries_optionlist.php :
<option value="Afganistan">Afghanistan</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>
<option value="Antigua & Barbuda">Antigua & Barbuda</option>
<option value="Argentina">Argentina</option>
<option value="Armenia">Armenia</option>
<option value="Aruba">Aruba</option>
<option value="Australia">Australia</option>
<option value="Austria">Austria</option>
<option value="Azerbaijan">Azerbaijan</option>
<option value="Bahamas">Bahamas</option>
<option value="Bahrain">Bahrain</option>
<option value="Bangladesh">Bangladesh</option>
<option value="Barbados">Barbados</option>
and more...
I want to add selected tag to target country like this:
<option value="Turkey" selected>Turkey</option>
I did some research on Google and StackOverflow but I couldn't find any solution.
Could you provide me with a suggestion on how to achieve this?
In each option, you can check if the session variable matches that country and add the selected attribute.
<?php $user_country = $_SESSION['user']['www']; ?>
<option value="Afganistan" <?php if ($user_country == 'Afghanistan') echo 'selected'; ?>>Afghanistan</option>
<option value="Albania" <?php if ($user_country == 'Albania') echo 'selected'; ?>>Albania</option>
<option value="Algeria" <?php if ($user_country == 'Algeria') echo 'selected'; ?>>Algeria</option>
...
You can simplify this by putting all the country names in an array. Then you can loop like this:
<?php
$user_country = $_SESSION['user']['www'];
foreach ($countries as $country) { ?>
<option value="<?php echo $country; ?>" <?php if ($user_country == $country) echo 'selected'; ?>>
<?php echo htmlspecialchars($country); ?>
</option>
<?php
}

Get multiple select data from Bootstrap Select using 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 . ', ';
}
}

jquery.validationEngine.js dropwdown

Im using jquery.validationEngine.js everything is setup and working fine, however I need to make a change.
I have two dropdown boxes.
Is The Vehicle Imported? [yes, no]
What Is The Country Of Origin? [France, Germany, Japan, USA, UK] etc
What I require is that if the car is imported then to show the country of origin, else display nothing.
At the moment I have both fields working on a live form,
<label>Is The Vehicle Imported?</label>
<select class="validate[required] target" name="strLeadData39" id="strLeadData39">
<option selected="selected" value="">Please Select</option>
<option value="No">No</option>
<option value="Yes">Yes</option>
</select>
<label>What Is The Country Of Origin?</label>
<select class="validate[required] target" name="strLeadData40" id="strLeadData40" >
<option selected="selected" value="">Please Select</option>
<option value="Not Imported">Not Imported</option>
<option value="France">France</option>
<option value="Germany">Germany</option>
<option value="Japan">Japan</option>
<option value="Canada">Canada</option>
<option value="USA">USA</option>
<option value="EU-Other">EU-Other</option>
<option value="Other">Other</option>
</select>
How do ะจ complete this?
Thanks
So you want to show/hide the country select depending on the value of the imported select?
$('#strLeadData39').change(function(){
if ( $(this).val() == "Yes" ){
$('#strLeadData40').show();
} else {
$('#strLeadData40').hide();
}
});
Here is a fiddle with a basic example
The above code has nothing to do with jQuery validationEngine,just hiding/displaying the element. To make the form validate you will probably have to dynamically remove/add the validate[required] class using jQuery removeClass/addClass, something inside the if/else like:
$('#strLeadData40').removeClass("validate[required]");

HTML define what selection option is active

I have a select box with the following (it has been shortened):
<select id="viewSelector" name="when" style="width:92%;">
<option value="USA">USA</option>
<option value="Australia">Australia</option>
<option value="Germany">Germany</option>
</select>
If the user logs into the control panel and wants to change his country, he gets presented with this form. My problem is that everytime USA is the default and I can not change this depending on the users country. For example, the user lives in Australia. He wants to change his country to USA and goes to this page. I want the country that is displayed to be Australia. Let me know if this makes sense.
To preselect a value, just add the selected attribute to the desired option.
<select id="viewSelector" name="when" style="width:92%;">
<option value="USA">USA</option>
<option value="Australia" selected="selected">Australia</option>
<option value="Germany">Germany</option>
</select>
This will preselect Australia for example.
You need to add a selected attribute to the option you want to select, as described in the spec.
<select id="viewSelector" name="when"">
<option value="USA">USA</option>
<option value="Australia" selected="selected">Australia</option>
<option value="Germany">Germany</option>
</select>
In your script, you need to emit this attribute for whatever default you want to display.
Use the selected attribute of the option tag.
<select id="viewSelector" name="when" style="width:92%;">
<option value="USA">USA</option>
<option value="Australia">Australia</option>
<option selected value="Germany">Germany</option>
</select>
Use selected on the <option> tag that names the user's current country:
<option value="Australia" selected="selected">Australia</option>
So in PHP:
<select>
<?php
$countries = array('USA', 'Australia', 'Germany');
$current_country = 'Australia';
foreach($countries as $country) {
if($country == $current_country) {
echo '<option selected="selected">'.$country.'</option>';
} else {
echo '<option>'.$country.'</option>';
}
}
?>
</select>
The html looks like:
<option selected value="Australia">Australia</option>
In the php loop that builds this html, compare the current option name with the user's current country (retrieved from a database or where-ever). When the two match, add selected
A bit shorter:
foreach($paises as $key=>$value)
{
if($key == "PE"){$activo = ' selected="selected" '; }else{$activo = "";}
echo "<option $activo value=\"$key\">$value</option>";
}

Categories