Getting multi Values from a dropdown list in PHP - php

This is my drop down list.
<p align="center"><select size="1" name="bo_chose" id="boID">
<option selected value="Select...">Select...</option>
<?php
while ($list_bo = mysql_fetch_array($select_brof)) {
echo "<option value=\"$list_bo[bo_name] $list_bo[bo_code]\">$list_bo[bo_code],$list_bo[bo_name]</option>"; }
?>
</select></p>
So the drop down will show first "select..."
and then will retrieve data andlist the bo_name, bo_code in <option>
It works well.
The problem is, I want to carry the value to another PHP page which will delete the
selected option in the drop down.
Of course the MySQL and PHP will complain that it does not exist ...why?
Its taking the new value $bo_chose (name of the dropdown list) as a new value
as (bo_name, bo_code) — as one value not as a split values.
So if the dropdown list is (george Mike GM)
the data will complain that there is no value called "George Mike GM"
when I want it to carry only "GM" which is the bo_code.
Can anyone help?

Just split the value with space as delimiter and pick the first value. May I know why do you
need to carry both name and id in first place.

You should just add $list_bo['bo_code'] as option value instead of both.

If I understand you correctly you have this select inside a POST form or you are sending it by ajax to the script you want to parse the value. Use a separator to merge these two variables. The separator should be a char that you are sure, it will never be in either of two variables. So, you can for example use ; for the separator and write $list_bo[bo_name].';'.$list_bo[bo_code]. In the recieving script you just explode the value by separator and you will get both values as array. If you are unsure which chars will be in both variables, you can either json_encode or serialize it before puting it into html.

Related

echoing document.getElementById() with php variable as id

Hi I have a list of buttons with different id's like b_1, b_2, b_3 and so on. After that I have PHP code to interact with the database and get my results.
Then I have created a variable that receives the identity of the rows that it finds in the table. I would like to use the result to change the background color of the different buttons. It works fine if I write the literal identity "b_1" or "b_2" and so on but not in the form of a variable ($trans_boton).
I have tried in many ways and I can't find the key. Thanks for lighting.
$trans_boton = 'b_'.$data['numero'];
if($facta == $respuesta){
echo "<script>"; echo "document.getElementById('b_1').style.backgroundColor='red';"; echo "</script>";}
You just need to put $trans_boton in quotes, ie:
echo "document.getElementById('$trans_boton')" ;

Get value from dropdown using PHP

How can get the circled valued with PHP?
How can get a value from drop down tag of HTML with PHP? Not the value of value tag.
The data of the label in the option field is not passed to the server, so you cannot get that data without modifying the HTML too.
You could change the form simply though:
<option value="first">first</option>
<option value="second">second</option>
etc.
Put the values and texts in an array. Then retrieve the needed text by using the value of the chosen option as array key.
Try:
$q_array = array(
'1'=>'First',
'2'=>'Second',
'3'=>'Third',
'4'=>'Fouth',
'5'=>'Fifth'
);
echo $q_array[$_POST['quantity']];

PHP make daynamic variables and queries

So I hava a problem. On client side users insert theris data in textbox, radio in textarea. All number of input is stored in hidden type, sove php script on my server side knows how many input does it has. Sometimes there is just 20 inputs, sometimes 25 or 30, so the holl stuf is daynamic.
I have two questions:
1. How on server side dynamic generate variables and use them as $input1, $input2 and os on.
2. Let's say that I have somehow managde first problem so my second question is how to make query which sometimes uses only 20 parameters, sometimes 25 and so on. I don't wanna use arrays and tables;
I stareted php code:
for($i=1;$i<=$num; $i++){ //I get num from a hidden type
${"question".$i}="j";
if(isset($_POST["${"question".$i}"])){
${"question".$i}=$_POST[${"question".$i}];
echo question1; //this doesn't work but I want make created variables
//to use like this
}
else
{
echo "You have error with reading ".$i." question";
}
}
Change echo question1; by echo $question1; (append $ symbol before your var name)
Or in dynamic way:
echo ${"question" . $i}
Why would you like to use the variables like this?
If the input is dynamic use it like an array! -> Easier and cleaner.
There is good example how to handle a dynamic array input: Post array from html to php

Delete duplicates in dropdown list

I have hard coded and added items to dropdownlist ie teamsize as 1,2,3 like that.
When i load this dropdownlist for edit/update i get duplicate values like this
1
1
2
3
4... How do i eliminate this duplicate values?
please find the code below
<select name="anesthesia" id="selectAnesthesiaVal" style="width:25%;" class="required safe" AppendDataBoundItems = "false">
<option value="<?php echo isset($event)?$event->proc_anesthesia_type:"" ;?>"><?php echo isset($event)?$event->proc_anesthesia_type:"" ;?></option><option value="General">General</option>
<option value="Mac">Mac</option>
<option value="Spinal/Epidural">Spinal/Epidural</option>
<option value="Regional">Regional</option>
<option value="Local">Local</option>
<option value="Other">Other</option>
</select>
If you do not want to use jQuery for this then I would advise placing all of the possible values into an array and looping through them in PHP. Then if the value exists, only place it once.
In addition, if you would like to use jQuery and the PHP is not possible in your circumstances then let me know and I will post up some jQuery.
UPDATE
This will do the trick. I have clearly laid out comments to explain what is going on step by step. Hope this helps.
Please note that it would be much more efficient to do this in PHP
// Set the present object
var present = {};
$('#selectAnesthesiaVal option').each(function(){
// Get the text of the current option
var text = $(this).text();
// Test if the text is already present in the object
if(present[text]){
// If it is then remove it
$(this).remove();
}else{
// Otherwise, place it in the object
present[text] = true;
}
});
I suspect that line
<option value="<?php echo isset($event)?$event->proc_anesthesia_type:"" ;?>"><?php echo isset($event)?$event->proc_anesthesia_type:"" ;?></option>
add option which correspond to option choosen from select control, i e on first load you see coded list as this line returns empty option, but when you choose actual option this first one gets populated with choosen value
in this case you need to do 2 things
1 remove this line
2 add conditions to each line of hardcoded option and set it to select depending on the value of $event->proc_anesthesia_type
and because of 2nd tank you will end up with 6 almost identical conditional statements putting selected='selected' to each option
so in order to make the overall code looks pretty i recomend instead of hardcodding options add values to list or even better dictionary and check this condition in a loop

Using Apostrophe in MySQL ENUM value that will populate HTML combo box for database search

I am having trouble first creating an ENUM value in my MySQL column with an apostrophe in it, then using the value to populate an HTML combo box via PHP, and then using said value as a selection to search the database.
Here is the SQL I use to create the ENUM value (I'm only showing the value in question, while usually there will be several values):
ALTER TABLE `primary_images` CHANGE `imgClass` `imgClass` ENUM( 'Robin\'s' ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL
Yet here is what I end up with in my database column:
`imgClass` enum('Robin''s') DEFAULT NULL
Thus my first question of why my escape backslash and single quote have turned into a double quote? (I'm using phpmyadmin to administer my database btw)
Here is the PHP I am using to create my combo box options:
$imgClass_query = "SELECT DISTINCT imgClass FROM primary_images ";
...
while ($imgClass_row = mysql_fetch_array($imgClass_result))
// Add a new option to the combo-box
echo "<option value='".$imgClass_row[imgClass]."'>".htmlspecialchars($imgClass_row[imgClass])."</option>\n";
As you can see above, I place the imgClass value as both the html option attribute value, as well as the displayed value. It is the attribute value that I subsequently use to search the database.
And here is the generated HTML source:
<option value="Robin" s'="">Robin's</option>
So obviously when I use the value attribute to search the database, it looks for Robin which does not exist.
How should I be entering the ENUM values, and how should I be creating the combo box to maintain the integrity of the original ENUM value?
Also, I'm not sure why there is the ="" after the s' in the generated HTML, and why the displayed value is perfect. Any thoughts?
Part 1 might be quite easy: 'a''b' is an alternative for 'a\'b' and thus means the same. UI am sure that in queries the correct string will be returned, as opposed to the table definition.
For Part 2, Nedret Recep has given you the correct answer. Plus, I wonder how
echo "<option value='".$imgClass_row[imgClass]."'>"
could ever result in
<option value="Robin" s'="">
. I would rather expect
<option value='Robin's'="">
which is clearly wrong, but can be solved with htmlspecialchars().
EDIT: I have found a possible explanation: the HTML parser of the browser could be to be blamed here. With
<select id="s" onchange="alert(document.getElementById('s').innerHTML + ' value:' + document.getElementById('s').value)">
<option value='Robin&apos;s'>a</option>
<option value='Robin's'>b</option>
</select>
I get with Firefox
<option value="Robin's">a</option>
<option value="Robin" s="">b</option>
value:Robin's
with option a and
<option value="Robin's">a</option>
<option value="Robin" s="">b</option>
value:Robin
with option b. But it is after parsing and re-assembling and is clearly not the PHP-generated code. And even then, where is en axcess ' in <option value="Robin" s'="">.
Here's a bit on how to deal with special characters in MySQL MySQL Manual
I think this is the easiest way for you to do it.
A “'” inside a string quoted with “"” needs no special treatment and need not be doubled or escaped. In the same way, “"” inside a string quoted with “'” needs no special treatment.
So instead of using single quotes for delimiting that specific string, use double quotes and then you don't need to escape the single quote inside of it.
On the HTML side, I think the single quote inside the string is causing the problem; you may have use HTML special character code (see HTML codes). You can do that easily in PHP with this function: PHP htmlspecialchar
You just need to use htmlspecialchars() also for the value attribute:
echo "<option value='".htmlspecialchars($imgClass_row[imgClass])."'>".htmlspecialchars($imgClass_row[imgClass])."</option>\n";
everything else is correct.
Also, I'm not sure why there is the ="" after the s'
The only possible answer is: you are editing the wrong file.

Categories