two select at a time in combo box - how to solve it? - php

Here I'm using combo box for searching working fine fetch from MySQL.
here is the code
<select name="location" class="styled">
<option selected="selected" value=''>Any</option>
<?php
while ($loc_row = mysql_fetch_array($loc_result))
{
echo "<option value='$loc_row[job_location]'";
if($loc_row['job_location'] == $location)
{
echo 'selected="selected"';
}
echo ">$loc_row[job_location] </option>";
}
?>
</select>
when click on search get string becoming as selected $location=isset($_GET['location'])?$_GET['location']:''; during this time there are two selected value.
could any one can help me to be one select value please

Remove the selected="selected" from the default "Any" option. The first element is selected by default.

Related

I have trouble in set_select in codeigniter

i have worked in a codeigniter framework and i have a problem in select tag.
when i want to achieve the value of option that i have selected.
i used the method as when i select the option value then i pressed the search button and after what i need the result its showing and then i want to again the value of the selected option with second submit button and the whole data with selected option value must be go to data base. the code is as follow.
<select id="faculty" data-rel="chosen" name='stdid'>
<option>select student</option>
<?php
foreach ($student as $s) {
echo '<option set_select("student","'.$s['stdid'].'","true") value="'.$s['stdid'].'">'.$s['stdname'].' ' .$s['stdid'].'</option>';
}?>
</select>
You are confusing PHP with HTML. Change the code to this:
<select id="faculty" data-rel="chosen" name="stdid">
<option>select student</option>
<?php
foreach ($student as $s) {
echo '<option'.set_select("stdid", $s['stdid'], true).' value="'.$s['stdid'].'">'.$s['stdname'].' '.$s['stdid'].'</option>';
}?>
</select>
Note that setting all the option elements to set_select("stdid", $s['stdid'], true) will become selected="selected". Read more about set_select.

PHP: How to make a select box work and how to stop drop down menu selection from resetting to first option on button click

I'm still relatively new to PHP and I am having some issues.
I have a paramaterized query set up that uses a selected Region and selected Subsystem to print a report. The user selects the Region from a select box with 5 regions hard coded. Then they select a Subsystem from a drop down menu that queries rows from a database.
I have not copied all the code below, just the relevant parts.
Here is my code on the functions page for the listbox:
<?php
function listbox($query, $name, $previous, $db){
$result=mysqli_query($db,$query) or die("Query ($query) is incorrect!");
print "<select name=\"$name\">\n";
while ($row = mysqli_fetch_row($result)){ //Table body
if($row[0] == $previous){
print "\t<option value=\"$row[0]\" selected=\"selected\">$row[0]</option>\n";
}else{
print "\t<option value=\"$row[0]\">$row[0]</option>\n";
}
}
print "</select>";
}
?>
Here is my code on the index page that calls the function page:
<?php
$default = "";
$previous = "";
$name4 = "region";
$name1 = "sub";
?>
<select name="<?php print $name4 ?>" size="5">
<option value="Option 1" selected>West</option>
<option value="Option 2">North Central</option>
<option value="Option 3">South Gulf</option>
<option value="Option 4">North East</option>
<option value="Option 5">South Atlantic</option>
</select>
<?php listbox($SubsystemListbox_strSQL, $name1, $previous, $db) ?>
if(isset($_POST['Submit'])){
$ByRegionCap_strSQL = "CALL `unit_costs_db`.`Subsystem_Average_Capital_Costs`('{$_POST[$name4]}', '{$_POST[$name1]}');";
}
as of right now, the region select box doesn't work at all, so I have been using code similar to the subsystem listbox:
listbox($RegionListbox_strSQL, $name4, $previous, $db)
Also, right now after the submit button is clicked, both selected option values reset to the first value in the dropdown. I would like to have both boxes remain showing their previously selected values after the submit button has been clicked.
If anyone has any ideas on how to help, please let me know!!
thanks in advance :)
When you reload your page, all your HTML elements go back to the initial state.
You have to get the content of the select via HTTP POST and set it the right option to "selected"
do you submit the form to the same script?
then you could do smth like:
<select name="<?php print $name4 ?>" size="5">
<option value="Option 1" <?=$_POST[$name4] == 'West' ? 'selected' : ''?>>West</option>
<option value="Option 2" <?=$_POST[$name4] == 'North Central' ? 'selected' : ''?>>North Central</option>
<option value="Option 3" <?=$_POST[$name4] == 'South Gulf' ? 'selected' : ''?>>South Gulf</option>
...
</select>
if it is another script you may save the data in the session and access over $_SESSION instead of $_POST.
Might not be relevant, but the first thing that I see is the way you print your options:
print "\t<option value=\"$row[0]\">$row[0]</option>\n";
PHP won`t let you do this for an array. You need to break your string:
print "\t<option value=\"".$row[0]."\">".$row[0]."</option>\n";
Or use curly braces:
print "\t<option value=\"{$row[0]}\">{$row[0]}</option>\n";
To preserver the selected values you need to keep track of them and manually select the needed values. In your case - check if the $previous variable is properly filled with data.

pre-selected values for a dynamic drop-down box

Here is my problem, I have dropdown which used to save the selected option to a SQL database. Now I have an Edit option where the same dropdown is created dynamically to give the user to select and alternate option and save the edit. When the edit page launches I want the dropdown to be pre-selected the value already saved in the database.
I use following code to get done the similar thing with a textbox but strugling to put the same value attribute to the dropdown.
<input name='routename' type='text' value='".htmlentities($row['route'])."'> // This is working for the textbox
can someone tell me how to do this with a dropdown box? Thanks
It's a little more complicated but can be achieved with this:
<select name="something">
<option value="1"<?=($row['something'] == 1)? ' selected="selected"':''?>>Option 1</option>
<option value="2"<?=($row['something'] == 2)? ' selected="selected"':''?>>Option 2</option>
</select>
You will have to use conditional code as you generate your <option> nodes like this:
<?php
echo '<option ';
if ($value == $selected_value)
echo 'selected="selected"';
echo 'value="'.htmlspecialchars($value).'" />';
?>
while($row=mysql_fetch_row($rs)){
if($row['id']==$value){
$selected='selected';
}else{
$selected='';
}
echo '<option value="$row['id']" $selected >$row['value']</option>';
}

How can I create a Dropdown Box for quantity where min. is 1 and max is set by a variable? (PHP)

was wondering if anyone had any idea if this was possible.
For an e-commerce site, there is a variable for max quantity allowed, let's just say its $maxqty.
On the checkout page, we have an input box for quantity. We want to change this to a dropdown box, but each item has a different limit ($maxqty).
Is there a way to create a dropdown box with a minimum value of 1 and a maximum value of $maxqty.
Specific Example:
Item #1, Max Qty = 5
Item #1 at Checkout Box, Quantity Drop Down would show choices for 1,2,3,4,5
Thanks for reading!
select tags in html are used to create dropdown list, which on checkout/submission of form, will give you the selected value in php.
Example:
<select>
<?php
for($i=1;$i<=5;$i++) {
echo "<option value='$i'>$i</option>";
} ?>
</select>
should be easy, you can place max qty in a variable , and then loop from 1 to that max qty, creating an option each time like e.g.
<? php
for($i=1;$i<=$item_max;$i++)
{
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
Assuming you are getting the max from querystring. It could also be assigned by other ways
<?php
$maxQty = (int) $_GET['max_qty'];
?>
<select name="quantity">
<?php
for($i=1; $i<=$maxQty; $i++)
echo "<option value='$i'>$i</option>";
?>
</select>
OUTPUT
<select name="quantity">
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
</select>
This really depends a lot on your setup, that is how your form elements are generated.
You can just generate the select options through a for loop
<select name="quantity>
<?php for($i=1;$i<=$maxqty;$i++) {?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<? } ?>
</select>
Note that this intermixing of php in your html code is generally ill-advised and should be preferably separated

select dropdown value based on session variable

i have a php form which submits data to another php for validation. if any fields are left blank the validator php sends a message back to original php form along with all the pre filled variables using session variables so that user doesn't have to fill them again.
i can use these session variables to fill in text boxes again like this
value="<?php echo $_SESSION['fname'] ?>"
but how to go about populating drop downs, radio buttons and check boxes?
thanks
For a select, checkbox or radio box you would need to check whether the values match. Something like:
<select name="fname">
<option value="1" <?php if($_SESSION['fname'] == 1) echo 'selected'; ?>>1</option>
<option value="2" <?php if($_SESSION['fname'] == 2) echo 'selected'; ?>>2</option>
</select>
It's easier if you are iterating through the values for the option field:
<select name="fname">
<?php foreach($array AS $key => $value) { ?>
<option value="<?php echo $value; ?>" <?php if($_SESSION['fname'] == $value) echo 'selected'; ?>><?php echo $value; ?></option>
<?php } ?>
</select>
A <select> element's selected <option> is not determined by it's value= attribute. In order to mark an option selected, the <option> element must have the selected attribute set on it, as follows:
<select>
<option>1</option>
<option selected>2</option>
<option>3</option>
</select>
In order to do this programmatically, you need to iterate through the options, and manually test for the correct option, and add selected to that.
... Noticed that your double quote at the end of the statement was misplaced, it should be:
<script>
$("#fname").val("<?php echo $_SESSION['fname'] ?>");
</script>
There is a far simpler method using JQuery. You can set Selects with .val() so make the most of it.
<script type="text/javascript">
$("#fname").val("<?php echo $_SESSION['fname']; ?>");
</script>
Don't forget to set the id of the select as well as the name to fname.

Categories