Multiple selections into input box - php

I have a selection of countries that a user can choose from. They are displayed in a dropdown box. At the moment I can select one country and it will populate it in the input box, however i want to know if there is a way of selecting more than one and it puts them in the same box.
The data will be populated from a mysql database
Hope im making my self clear.
Here is my code so far:
JAVASCRIPT:
<script language="JavaScript" type="text/javascript">
<!--
function showSelected()
{
var selObj = document.getElementById('countries');
var txtTextObj = document.getElementById('txtText');
var selIndex = selObj.selectedIndex;
txtTextObj.value = selObj.options[selIndex].text;
}
//-->
</script>
HTML:
<form>
<p>
</form><select id="countries">
<option value="val0">Australia</option>
<option value="val1">England</option>
<option value="val2">France</option>
<option value="val3">Italy</option>
<option value="val4">South Africa</option>
</select>
<input type="button" value="Show Index" onclick="showSelected();" />
</p>
<p>
<input type="text" id="txtText" />
<br />
</p>
</form>
So what i would want is if you choose Australia, it shows Australia in the box, then go back to the dropdown and choose South Africa it puts it into the same box as: Australia, South Africa.
Thank you Guys

why no just append new selections to the text already in the inputbox.
txtTextObj.value += selObj.options[selIndex].text +', ';

Use Multiple select box and name as array:
<select id="countries" multiple="multiple" name="countries[]">
<option value="val0">Australia</option>
<option value="val1">England</option>
<option value="val2">France</option>
<option value="val3">Italy</option>
<option value="val4">South Africa</option>
</select>
Now the select will treat as array. Use javascript code to fetch the values selected and convert it to comma separated list and put in the input box.

<select id="countries" multiple="multiple">
<option value="val0">Australia</option>
<option value="val1">England</option>
<option value="val2">France</option>
<option value="val3">Italy</option>
<option value="val4">South Africa</option>
</select>
Use multiple="multiple" in your select box to make multiple selections.

Related

Make the value from option select auto-entered to the textarea

Is there any way that the value from select option to be auto-inserted into a textarea? (in php)
Option Select:
<select id="Branch">
<option value="MCG">MCG</option>
<option value="KB">KB</option>
<option value="KT">KT</option>
</select>
TextArea
<input name="street" type=text value=>
Sorry in advance if I didn't ask the question correctly
It is not possible using PHP at a time on change select but,
You can select option to be auto-inserted into a textarea using javascript like blow example
function updateTextareaValue(selectVal){
document.getElementById("street").value = selectVal;
}
<html>
<body>
<select id="Branch" onchange="updateTextareaValue(this.value);">
<option value="MCG">MCG</option>
<option value="KB">KB</option>
<option value="KT">KT</option>
<option value="XX">XX</option>
<option value="YY">YY</option>
</select>
<textarea id="street"></textarea>
</body>
</html>

How to put the selected options in an array depends on user selection?

I want to get the value of selected options and create an array using AJAX. I have this code,
<select class="form-control" name="cols[]" multiple="multiple" id="cols[]" required="required">
<option value="val1">uploaded to webgis</option>
<option value="val2">Remarks on upload</option>
<option value="val3">Level of Approval</option>
<option value="val4">Date Approved</option>
<option value="val5">Year Approved</option>
</select>
how am I going to put it in an array using AJAX? for example, the user selected the val4 then next is val5 then next is val1. what i want to achieve is to sort them depends on user selection, array(val4, val5, val1).
Use .change() to capture the event of change of the select after the change happens get all the selected option value then use .map() to translate all items in an array or object to new array of items.
$('.form-control').change(function() {
var optionvalue = $("option:selected", this).map(function() {
return this.value
}).get().join(", ");
console.log(optionvalue)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select class="form-control" name="cols[]" multiple="multiple" id="cols[]" required="required">
<option value="val1">uploaded to webgis</option>
<option value="val2">Remarks on upload</option>
<option value="val3">Level of Approval</option>
<option value="val4">Date Approved</option>
<option value="val5">Year Approved</option>
</select>
Try this way

PHP URL that's identified by the ID?

I'm still learning to use PHP with MySQL tables and I'm sorry if this is a novice question but how would I change the dropdown code below to be able to insert normal a href links (with an image or text) that link to the playerMenu.php page? Here's the dropdown menu code I have:
<form action="playerMenu.php" method="get">
<select id="players" name="selectvalue" onchange="showMe(this.value);">
<option value="">Select</option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>
<input type="submit" value="Submit">
<form>
Submit
Thanks in advance.
Here's how using JQuery. It works! Try it out!
<!-- Your Form -->
<form>
<select id="players" name="selectvalue" onchange="showMe(this.value);">
<option value="">Select</option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>
<input id="button" type="button" value="Submit">
<form>
<!-- Include JQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
var sub_link = "playerMenu.php?electvalue="
$("#button").click(function() {
sub_link = sub_link + $("#players").find('option:selected').text();
// Here's your link
alert(sub_link);
// And now we do a javascript redirect
window.location.replace(sub_link);
});
</script>
From the PHP side, once you submit that form then the url will be
playerMenu.php?players=1&submit=Submit
If the option A was chosen. You can remove the onchange and onclick javascript, if this is what you want to achieve.
From there you have passed in the value of the players select box. You would want to then likely save this as a variable and what you want with the code. If you are doing something with your database then make sure to santize the variable as well.

how to show textarea based on select dropdown using jquery?

i want to display 2x dropdown menus both will be pre populated (2nd menu "mainToon" will contain over 200 names but for the example i have shown just a few.
<select id="category" name="Category">
<option value=" "></option>
<option value=" ">-----------------</option>
<option value="Main Toon">Main Toon</option>
<option value="Alt Toon">Alt Toon</option>
<option value="Cyno Toon">Cyno Toon</option>
<option value="Super Toon">Super Toon</option>
<option value="Dust Toon">Dust Toon</option>
</select>
<select id="mainToon" name="mainToon">
<option value=" "></option>
<option value=" ">-----------------</option>
<option value="Agmar">Agmar</option>
<option value="S Tein">S Tein</option>
<option value="Karades">Karades</option>
<option value="Bad Kharma">Bad Kharma</option>
<option value="Ed jeni">Ed Jeni</option>
</select>
by default the first dropdown will show blank and i want the "mainToon" dropdown to be hidden untill any of the following are selected:
"Alt Toon",
"Cyno Toon",
"Super Toon",
"Dust Toon"
Then the form will be visable.
Can i do this by applying a .hidden css code to the dropdown and changing the class dynamically when other options are selected?
many thanks for the help.
First hide your dropdown :-
<select id="mainToon" name="mainToon" style="display:none;">
</select>
And use Jquery show hide function like this :-
$(document).ready(function(){
$("#category").change(function(){
var value = $(this).val();
if(value=="Alt Toon" || value=="Cyno Toon")
{
$("#mainToon").show();
}
else
{
$("#mainToon").hide();
}
});
});
Below show the DEMO how to show DIV based on selection value ,
$(function() {
$('#colorselector').change(function(){
$('.colors').hide();
$('#' + $(this).val()).show();
});
});
And Here is working demo.

Two drop down lists in Ajax and MySQL

I'm trying to learn some Ajax and MySQL...
Basically, the code that I have right now is similar to: http://www.w3schools.com/PHP/php_ajax_database.asp
I was wondering how should I modify those codes (both html, javascript and php) to create two drop down lists. The first drop list would be used for choosing the Lastname (eg. Swanson) and the second drop list would be used for choosing the Hometown (eg. Quahog). Then the user would click "Submit" button and the query would return the matching results (eg. all the Swansons living in Quahog).
I'd be very thankful for all the ideas!
The only major difference to the html page will be the addition of another dropdown, addition of the submit button, and removal of the onchange event (assuming you want this removed since you're relying on the submit button). So instead of:
<form>
Select a User:
<select name="users" onchange="showUser(this.value)">
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
You now have:
<form>
Select a User:
<select name="lastname" id="lastname">
<option value="Griffin">Griffin</option>
<option value="Quagmire">Quagmire</option>
<option value="Swanson">Swanson</option>
</select>
<br>
Select a Hometown:
<select name="hometown" id="hometown">
<option value="Quahog">Quahog</option>
<option value="Newport">Newport</option>
</select>
<input type="submit" value="Submit" onclick="showUser(Document.getElementById('lastname').value, Document.getElementById('hometown'))">
</form>
The only change you need to the javascript is to change the showUser function to take and process the two inputs. So the declaration becomes:
function showUser(lastname, hometown)
and instead of the line:
url=url+"?q="+str;
you need:
url=url+"?lname="+lastname+"&town="+hometown;
Then in the PHP, you need only change the variable assignments and the query that is being executed. So:
$q=$_GET["q"];
Becomes:
$lname=$_GET["lname"]; $town=$_GET["town"];
And:
$sql="SELECT * FROM user WHERE id = '".$q."'";
Becomes:
$sql="SELECT * FROM user WHERE lastname = '".$lname."' AND hometown = '".$town."'";

Categories