Handling a lots of selects (dropdown lists) - php

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}";
}

Related

handling select dropdown menu in php

I put a first option in my select element just to tell the user what they are selecting, I think it's advisable to put a null first option, but I want to put an error message that the user didnt select anything if he is just selecting the first option which is the null option here's my code
<select class = "strand" name = "strand">
<option >=====Strand=====</option>
<option value ="GAS">GAS</option>
<option value ="ICT">ICT</option>
<option value ="STEM">STEM</option>
<option value ="HUMSS">HUMMSS</option>
<option value ="ABM">ABM</option>
</select>

Get the right value of select box and display it in php

I have a form with a select box where the user can select his gender. I succeed in getting the selected value and displaying this. But I want to use another term for the selected value.
For example, there are 2 values in the select box, named "man" and "vrouw".
<select name="geslacht" class="formulier-input">
<option value="" disabled selected hidden>Kies uw geslacht</option>
<option value="man">Man</option>
<option value="vrouw">Vrouw</option>
</select>
But I want to display Dhr. as the user selects for the value "man" and Mevr. as the user selects for the value "vrouw".
I guess I can do this in an if statement with:
if($geslacht === "the value"){ }
I don't know how I can finish this and I don't know what to put in the place of "the value".
How can I do this?

PHP Multiple Hidden Values Select Option

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")

How to store multiple selected values from <select> tag in a single field [duplicate]

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.

mysql update query with form array

Multiple posts but I'm still stuck...I'm missing something fundamental here. I have a form with a select:
<select name="camera_status[]">
<option <?php echo $enabled_option; ?>>Enabled</option>
<option <?php echo $disabled_option; ?>>Disabled</option>
</select>
This form is built with a loop to give a list of all camera settings. So you would have multiple cameras and their corresponding camera_status. Also I have a hidden input field with the camera_id:
The camera_id is processed with some javascript. Then I process that with:
$camera_id = $_POST['camera_id'];
if (is_array($_POST['camera_status']))
{
foreach ($_POST['camera_status'] as $camera_status) {
$query_status = 'UPDATE `#__cameras` SET `camera_status` ="'.$camera_status.'" WHERE `camera_id`='.$camera_id;
$db->setQuery($query_status);
$db->query();
}
}
If I echo the camera_id it is correct. But my foreach runs an update query for the full list of cameras instead of just the one selected. So it updates only the last camera in the list. Let me know if it makes sense to update the full code. Obviously I'm going about this all wrong...
EDIT: Well if you have single selection then it is simpler than that:
HTML:
<select name="camera_status">
<option value="Enabled">Enabled</option>
<option value="Disabled">Disabled</option>
</select>
And PHP:
$camera_id = (int) $_POST['camera_id']; //Here you had SQL injection.
$camera_status = mysql_real_escape_string($_POST['camera_status']); //Neither that was protected.
$query_status = 'UPDATE `#__cameras` SET `camera_status` ="'.$camera_status.'" WHERE `camera_id`='.$camera_id;
$db->setQuery($query_status);
$db->query();
You have a fundamental flaw in your thought process for the page. When you output more than one <select name="camera_status[]"> elements, you're going to get that many results back. With two of them, you'll get two values in the array, and so on.
What it sounds like you're doing is outputting a list of cameras, having the user select a camera to modify, and then, from then on, all of the camera settings now only apply to that one specific camera. If this is the case, then you don't need to use arrays for the camera settings, including camera_status. Just remove the array portion and stop outputting more than one HTML element for each camera setting (since you know that once a camera is selected, those values apply to that specific camera).
However, if your page that displays the multiple cameras allows the user to modify every camera and its settings, you'll need to accommodate for the user's input.
If the latter is the case, here's a neat trick - Modify your <select> so it looks like this when you're outputting your camera form:
<select name="camera_status[<?php echo $row['camera_id']; ?>]">
<option value="1">Enabled</option>
<option value="0">Disabled</option>
</select>
Now, when you grab $_POST['camera_status'], it'll be an array with the camera IDs as the keys and their selected value as the value. So now, you can do this:
if( is_array($_POST['camera_status']))
{
foreach ($_POST['camera_status'] as $camera_id => $camera_status) {
$camera_status = intval( $camera_status); // Be wary of SQL injection
$camera_id = intval( $camera_id);
$query_status = 'UPDATE `#__cameras` SET `camera_status` ="'.$camera_status.'" WHERE `camera_id`='.$camera_id;
$db->setQuery($query_status);
$db->query();
}
}
Now this will update every camera with the correct value chosen.
Easy enough. Add value attributes to the <option> tags so that each of them will have a value
<select name="camera_status">
<option value="1" <?php echo $enabled_option; ?>>Enabled</option>
<option value="0" <?php echo $disabled_option; ?>>Disabled</option>
</select>
Then, in your php, look for that value
foreach ($_POST['camera_status'] as $camera_status) {
if($camera_status == 1) {
$query_status = 'UPDATE `#__cameras` SET `camera_status` ="'.$camera_status.'" WHERE `camera_id`='.$camera_id;
$db->setQuery($query_status);
$db->query();
}
}

Categories