I have a drop-down menu as following:
<select onchange="random_function()" id='choice' >
<option selected="selected" >
Make a choice
</option>
</select>
I populate it through a php request inside the <select> tag as following:
<?php
include 'config.php';
$query = $pdo->query('
SELECT choices
FROM allTheChoices'
);
while ($row = $query->fetch()){
echo "<option>".$row['choices']."</option>";
}
?>
For the moment, my database is uncomplete and I have only 2 options to display but there will be plenty in the future (100+).
My goal is to find a way to let the select only show 10 options and it should display a scroll bar to see all the remaining options.
I already tried using <select size="10"> but the design of the dropdown is completely changed and awful.
Do you have a simple way to do this without altering the design?
Used this bit of code to achieve what I wanted :
<select style="position:absolute;" onmousedown="if(this.options.length>8)
{this.size=8;}" onchange='this.size=0;' onblur="this.size=0;" >
Make sure to use the following to avoid container div to expand when choosing an option :
style="position:absolute;"
and edit the following code with the number of choices you want to display (10 is used in this example) :
onmousedown="if(this.options.length>10){this.size=10;}"
So the whole html code I used is :
<select style="position:absolute;" onmousedown="if(this.options.length>8)
{this.size=8;}" onchange='this.size=0;' onblur="this.size=0;" >
<option selected="selected" >
Make a choice
</option>
</select>
Try using a limit in your SQL statement, like this: "LIMIT 10".
So this is how the code might look:
<?php
include 'config.php';
$query = $pdo->query('
SELECT choices
FROM allTheChoices
LIMIT 10'
);
while ($row = $query->fetch()){
echo "<option>".$row['choices']."</option>";
} ?>
Related
I want to be able to select randomly from the select tag using php,
i tried using rand(),it didnt seem to work
<select name="symbols" class="form-control" multiple="multiple">
<option value="BCH/BTC">BCH/BTC</option>
<option value="BCH/EUR">BCH/EUR</option>
<option value="BCH/GBP">BCH/GBP</option>
<option value="BTC-EOS">BTC/EOS</option>
<?php
$symbols = mysqli_real_escape_string($conn, $_POST['symbols']);
$rand001= shuffle($symbols);
?>
you have to use javascript for this,
because html has been created already before and php cannot change it:
in Jquery Syntax:
let options = $("option");
let toSelect = options[Math.floor(Math.random() * options.length)];
$(toSelect).attr("selected","selected");
I am trying to develop a webpage based on html, css. I am using PHP for server side scripting.
I want a dropdown menu to be displayed with available options, But at the same time I need this drop down list to accept text. so I can choose from dropdown list as well from the text box whatever I want.
I found one solution for the above scenario and working fine, but what extra I want that, once I write something in the text box, which is not an options of the dropdown, from the next time it will auto include it.
e.g. ->
currently my dropdown is having say three options "Samsung", "Sony", "Apple"
<option value="one">Samsung</option>
<option value="two">Sony</option>
<option value="three">Apple</option>
Now, "Lenevo" is not available. For the First time in the text box I will write "Lenevo" as my choice, there after it will include it into the dropdown menu.
<option value="one">Samsung</option>
<option value="two">Sony</option>
<option value="three">Apple</option>
<option value="four">Lenevo</option>
.
.
Like that it will happen.
Thanks for help.. :)
The best solution would be something like select2. (JavaScript)
For examples look here: Link
If you want to stay with PHP only, you need to offer a from to submit text values:
(Disclaimer: This solution is quite bad practice. But it's an example on how to solve it, on a low level.)
1) offer a form
<input type="text" name="addSelection">
2) Read post request
$newOption = $_POST["addSelection"];
3) Persist new option somewhere (here Session, also possible are databases)
$_SESSION["additionalOptions"][] = $newOption;
4) Merge with standard options
$options = ["apple","banana"];
$options = array_merge($options,$_SESSION["additionalOptions"]);
5) Create Options in HTML
<select name="fruits">
<?php
foreach($options as $option){
echo '<option value="'+$option+'">'+$option+'</option>';
}
?>
</select>
Use select2 plugin
https://github.com/select2/select2
<script type="text/javascript" src="/assets/profile/js/select2.min.fda43621.js"></script>
var validateTag = function (term, data) {
var value = term.term;
var re = /^[a-z\d\-_\s]+$/i;
if (re.test(value))) {
return {
id: value,
text: value
};
}
return 'wrong_characters';
};
$("#selectAlt").select2({tags: true, maximumSelectionLength: 6, maximumInputLength: 20, createTag: validateTag});
HTML:
<select name="selectAlt[]" id="selectAlt" multiple="multiple" custom-placeholder="Genre">
<option value="Blues">Blues</option>
<option value="Classic Rock">Classic Rock</option>
<option value="Country">Country</option>
</select>
I want to do like i said on the question.
This i the main part . When i select any options from that select part . i want to make other select blocks' element unselected.
Assume i have more select blocks for search for year, month, day etc.
Can you help e how i can do that.
<select id="makale" size="4"name="formMakale"style="height:7em;width:16em;border:0px;outline:0px;fontsize:16px;padding-left:10px;" >
<?php
$authorsQuery = $hb->authors();
foreach($authorsQuery as $v){
echo '<option value="'.$v->id.'">'.$v->name.' - '.count($hb>aticlesbyauthor($v),1000).' yazi</option>';}
?>
</select>
for example my other select block is:
if this block was selected before the upper select block, once select block with id makale is selected, make the block with id kategoriSec unselected if it is selected as i said before.
Thank you.
<select id="kategoriSec" size="4" name="formCat"style="height:7em;width:16em;border:0px;outline:0px;fontsize:16px;padding-left:10px;" >
<?php
$catQuery = $hb->db->get_results("SELECT * FROM category");
foreach ($catQuery as $v) {
echo '<option value="'.$v->id.'">'.$v->name.'</option>';
}
?>
</select>
You can use jQuery on change event to do this. First check the 2nd select box is empty or not then set the value to null when you select from the first select box.
$("#makale").on('change', function(){
if($('#kategoriSec').val() != ''){
$('#kategoriSec').val(null);
}
});
visit the below link for an example. this example may help you.
https://jsfiddle.net/skpaul82/87h5y0dm/5/
Ok, so this one is a little confusing. I have select dropdowns that are produced by PHP. It can be 4 selects, or 30 select dropdowns. Then there's option values. Here's what I have so far
<?php while($row = mysql_fetch_assoc($notes)){ ?>
<select name="milestone" id="milestone[<?php echo $row['id']; ?>]">
<option value="Enrollment Date">Enrollment Date</option>
<option value="Discharge Date">Discharge Date</option>
<option value="A/C Start">A/C Start</option>
<option value="Completion Date">Completion Date</option>
</select>
<?php } ?>
If I have 4 select boxes, I might have arrays as follows: milestone[2134], milestone[2222], milestone[225], and milestone[1022]
The array number is the id of the mysql table entry I need to update with the value of that specific select dropdown. I was thinking maybe to use milestone[][id] and loop through that?
Any ideas since there might be 20 select dropdowns?
Thanks!
do you want to fetch the values of your options from the database as well as the id ? Then you should to mysql_fetch_array or mysql_fetch_object function instead of mysql_fetch_assoc. This function only returns the number of the results while the two above returns the number as well as values.
Got it!
First, I had to apply the array to the name of the select like so and set the id value:
<select name="milestone[]" class="mstones" id="<?php echo $row['session_id']; ?>">
Then, with jquery, looped through the class and created created an array with the value being something I can "explode" in php:
var milestoneVal = [];
$("select.mstones").each(function(i, selected){
milestoneVal[i] = this.getAttribute('id') + ':' + $(selected).val();
});
Then, simple PHP
foreach($_POST['milestone'] as $v){
$m=explode(':',$v);
//db insert
}
It was tricky, and I'm sure there's a cleaner solution, but it works for me! Sorry for the poor initial explanation, and hope this helps someone else.
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();
}
}