Is it possible to assign multiple values to select option drop down lists? I need to retrieve multiple pieces of data from each drop down when they are selected and I have only been able to get the "name" and an "id". The name is displayed for the user to select however it is the ID that is passed to be processed. Here is my code:
<?php
$att_1 = $att_1;
mysql_connect("xx.xx.xx.xx","xxxxxx","xxxxxx");
mysql_select_db("dezanjow_cf");
$sql=mysql_query("select id, name from model");
if(mysql_num_rows($sql)){
$select= '<select name="model">';
$select.='<option value="default">Select Model</option>';
while($rs=mysql_fetch_array($sql)){
$select.='<option value="'.$rs['id'].'">'.$rs['name'].'</option>';
}
}
$select.='</select>';
echo $select;
?>
This produces html that looks like:
<select name="model">
<option value="default">Select Model</option>
<option value="5">GH20</option>
<option value="6">GH21</option>
<option value="7">GH22</option>
</select>
I wish to display them like this (as example):
<select name="model">
<option value="default">Select Model</option>
<option value="5","abc">GH20</option>
<option value="6","def">GH21</option>
<option value="7","ghi">GH22</option>
</select>
Thus when the data is passed onto the next php script, I can use both data "5" and "abc" when the user selects "GH20". I have not seen much on Google about this and don't even know if this is possible. Please let me know if I am asking for the impossible! Many Thanks, Nick
Hey you cannot have multiple values like that because post or get will return just first value which in this case is 5
<option value="5","abc">GH20</option>
but you can make your value looks like this
<option value="5,abc">GH20</option>
and then in php script you can separate values by using explode function
explode(',', $_POST['model'])
which will return array that you can use
array( 0=> 5, 1=>abc)
Its better if you serialize your data then at next php script unserialize it.
$data=array($rs['id'], "abc"); //data to be stored
$serialized_data=serialize($data); //serialize data
$select.='<option value="'.$serialized_data.'">'.$rs['name'].'</option>';
IMPORTANT
//unserialize information
$unserialized_data=unserialize($serialized_data);
this will output the same array array('SOMEID', "abc")
Related
I have HTML form with multiple select element to let the users select multiple options from the list for submission. The form method is POST and it posts the form to PHP file. I figured it out how to echo multiple selected options through out reading this How to get multiple selected values of select box in php? but I am unable to write those values to a variable as string, and having space between each option-value. Is it possible to achieve this without using array?
Here is my HTML code:
<select name="place[]" id="place" multiple>
<optgroup label="Ottawa">
<option value="London">London</option>
<option value="Toronto">Toronto</option>
<option value="Windsor">Windsor</option>
</optgroup>
<optgroup label="Alberta">
<option value="Brooks">Brooks</option>
<option value="Calgary">Calgary</option>
<option value="Cold Lake">Cold Lake</option>
</optgroup>
</select>
Here is the PHP code:
<?php
foreach ($_GET['place'] as $selectedOption)
echo $selectedOption."\n";
?>
The above code only echo the values but not assigning it to variable as string. Can I simply use $locations = instead of echo?
Note: There are many Questions and Answers that shows how to assign select option value to PHP variable but not for multiple option.
Since you said you were using a post request, $_GET['place'] won't work. So using $_POST['place'] instead, you can glue the values of the array together using implode():
$locations = implode(' ', $_POST['place']);
I have 2 select lists, one naming products and another for quantities.
<select name="fish" id="fish">
<option value="blueFish">Blue Fish</option>
<option value="redFish">Red Fish</option>
<option value="pinkFish">Pink Fish</option>
<option value="greenFish">Green Fish</option>
</select>
<select name="numFish" id="numFish">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
I would like it so when a product is chosen, the corresponding quantity from the database will be set accordingly.
So if there was a record that customer Billy had bought 5 Pink Fish, when I select Pink Fish, the quantity select list will change to 5.
This would be for use in a PHP form using a MySQL database.
Is such functionality possible, and if so how would I go about doing it?
You might want to google for ajax requests. What it does is detecting a change (in your case) through javascript, send the value you selected to another php script which should do a sql query to return the quantity. The ajax request will catch the returned value and through javascript again change the value from the select dropdown.
All this would happen in the background and your site wouldn't refresh.
If you are not very used to javascript you can have a look at the jquery framework, which makes this task a bit easier, and with a lot of examples documented.
I didn't paste any code because assume you are not familiar with javascript/jquery/ajax. You might want to read a bit of documentation and play around a bit, and come back when you have a concrete problem, that would be the normal workflow here in Stackoverflow.
Edit: (some code as requested by OP)
Javascript:
$('#fish').change(function(){
var fishType = $('#fish select option:selected');
$.ajax("getQuantity.php", {fish: fishType}, function(data){
data = JSON.parse(data);
if(data.status == "OK"){
var quantity = data.quantity;
$('#numFish option[value='+quantity+']').prop("selected", true);
} else {
alert("error");// or console.log(), whatever you prefer
}
}
});
php (getQuantity.php):
<?php
$fish = $_POST['fish']; //getting the fish type
$sql = "your SQL to select the fish quantity for that type";
$result = mysqli_query($mysqli, $sql);
if(mysqli_num_rows($result)>0){
$row = mysqli_fetch_assoc($result);
$data = array("status" => "OK", "quantity" => $row['quantity']); // you can just output a string with the number if you want, but this way you have more control and better debugging.
echo json_encode($data);
}
?>
It's a basic code, it still would need to catch some errors for the database or return a different status. But basically that's it. I didn't even test that code so use it as a guideline only.
Change <select name="fish" id="fish"> to <select name="fish" id="fish" onchange="getQuantity(this.value);">
Declare the following function in javascript:
function getQuantity( o ) {
// get the quantity from the database using ajax,
// and set the quantity dropdown here.
}
Let's say, that i have users stored in a database, identified by an id (an integer primary key). All the users have a multiple choice attribute, for example privileges on a site (Admin, Member, Administrator etc...). I would like to create a php admin page, where i can select for each user his/her privileges, and all users at one page (or at least more users at one page):
Tom [_privileges_v_]
Ed [_privileges_v_]
Lisa [_privileges_v_]
...
How would it efficient to do?
if i print the dropdowns with the name attribute set as the id of the record, then how could i access them through php?
<select name="12323">
<option value="admin">admin</option>
<option value="user">user</option>
...
</select>
<select name="4323">
<option value="admin">admin</option>
<option value="user">user</option>
...
</select>
Then after i post this form, my $_POST array will contain a $_POST["12323"] and a $_POST["4323"] member.
How could i make something like this, so i could iterate through the $_POST values?
Thanks for any kind of help!
You can output a select box like this
<select name="user_4323">
<option value="admin">admin</option>
<option value="user">user</option>
...
</select>
<input name="otherinp" value=""/>
in case your form has another input.... then in php code you can do
foreach($_POST AS $name=>$value) {
if (strpos($name, "user_")===0){
$userid = str_replace("user_","",$name);
//Do st with userid
}
}
//Work with orther input $_POST['']
Either fetch all the users again and loop through their IDs, then check if there is a post value linked to it, or loop through all the post values.
foreach($_POST AS $name=>$value) {
echo "Selection box with name {$name} has value {$value}";
}
This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed last year.
I have a select tag with multiple="multiple". And a user can select more than one value.
<label for="aoi">Area of Interest:</label>
<select id="sel_aoi" name="aoi" multiple="multiple">
<option value="hr-executive">HR Executives</option>
<option value="sr-manager">Sr. Manager</option>
<option value="service-advisor">Service Advisor</option>
<option value="production">Production Engineer</option>
<option value="mechanical">Mechanical Engineer</option>
</select>
How I can store these more than one selected values in single field of MySQL database?
Store it to an array and save the values to database using comma seprated.
<label for="aoi">Area of Interest:</label>
<select id="sel_aoi" name="aoi[]" multiple="multiple">
<option value="hr-executive">HR Executives</option>
<option value="sr-manager">Sr. Manager</option>
<option value="service-advisor">Service Advisor</option>
<option value="production">Production Engineer</option>
<option value="mechanical">Mechanical Engineer</option>
</select>
process.php
$aoi = implode(',', $_POST['aoi']);
When you get your values from your $_POST insert them in with a mysql insert multiple values like this:
insert into myTable
(someColumn, someOtherColumn)
values
(someValue1, someOtherValue1),
(someValue2, someOtherValue2),
(someValue3, someOtherValue3)
Edit: If you want the in one row use something like PHP implode():
$qry="insert into mytable (someColumn) values (".implode(',',$myArray).")
Not that you want to make sure that the $myArray variable is clean of anything that could SQJ Injection attack you database. A prepared statement would be a suggestion.
Django models come with JSONField() which you could use to serialize the list of integer values and later deserialize it into a list.
Another appproach is: if you have limited number of options, you could also try creating separate column for each option in your database instead of saving them in single field separated by comma.
E.g
interest_executive: Boolean
interest_manager: Boolean
interest_service_advisor: Boolean
Finally, another approach is to create a separate table and have foreign key relationship between user_id and interest_name_id but this would be overkill if you have limited number of interests.
Let's say I have this:
<select name="exposure">
<option value='1,0.005'>Micro</option>
<option value='2,0.007'>Mini</option>
</select>
<select name="clicks">
<option value="2500">2500 - Clicks</option >
<option value="500">500 - Clicks</option >
</select>
<label>Price:</label>
<div id="price"></div>
How can I do, so whenever I select something from select="exposure", it will automatically change the OPTIONS of select="clicks", and at last, take the value from select="clicks" and then multiply it with the second value of select="exposure".
Example
I have selected
<option value='2,0.007'>Mini</option>
And with this option, the only click package that is available is:
<option value="2500">2500 - Clicks</option >
Price: 0.007*2500 = 17,5
Is that possible?
Try this
$("select[name=clicks]").change(function(){
$("label").text( parseFloat($(this).val()) * parseFloat($("select[name=clicks]:option:eq(1)")[0].value) );
});
To "select value of select":
$('select[name="exposure"]').val('2,0.007');
To make only some options available:
$('select[name="clicks"] option[value="500"]').remove();
To extract data from value:
var splitResult = $('select[name="exposure"]').val().split(',');
alert(splitResult[0] * splitResult[1]);
But I personally suggest to not pass additional information via the attribute "value"; better solution would be to create a new attribute to such needs.