Check if select option selected - php

I have a form where I post some options to a mysql query. When it returns, it a draw a table with some data.
How to set the select box's option to be selected if my form returned?
I tried this, but not working:
<form method="post" id="partnersearchform">
<input type="hidden" name="formaction" value="partnersearch">
Partner Típus<br>
<select onchange="document.getElementById('partnersearchform').submit();" size="" name="ceg">
<option value="">(mind)</option>
<option value="1" if($ceg==1){ print 'selected'; }>Magánszemélyek</option>
<option value="2" if($ceg==2){ print 'selected'; }>Cégek</option>
</select>
</form>

You forgot to open and close php tags:
<form method="post" id="partnersearchform">
<input type="hidden" name="formaction" value="partnersearch">
Partner Típus<br>
<select onchange="document.getElementById('partnersearchform').submit();" size="" name="ceg">
<option value="">(mind)</option>
<option value="1"<?php if($ceg==1){ print ' selected'; }?>>Magánszemélyek</option>
<option value="2"<?php if($ceg==2){ print ' selected'; }?>>Cégek</option>
</select>
</form>

Try it like this:
<?php if($ceg==1){ echo 'selected="selected"'; } ?>
Also make sure what you get in $ceg var.
<option value="<?php echo $ceg;?>"
<?php if($ceg==1){ echo 'selected="selected"'; }?>>Magánszemélyek</option>

<option value="1" <?php if($ceg==1){ print 'selected'; }?> >Magánszemélyek</option>

Related

Changing HTML <select> Element Using Only HTML/PHP

I'm trying to retrieve data from a SQL DB when the user types a category ID in to search with. It should then autofill (populate) the html form data with the retrieved data from the row's columns (upon pressing the submit button).
Everything is working properly, but I don't know how to change a dropdown menu
For example:
<input name="prodname" type="text" value="<?php echo $pName; ?>"/>
Fills the form value successfully after I press the search(submit) button.
This following code does not change the drop down values at all (<select> vs <input>)
<select name="supplierID" value="<?php $suppID; ?>">
How can I change the drop down values of a while only using HTML/PHP?
Thank you.
To select a option in <select> tag you need to set selected attribute to that option. The following code may help you to understand.
<select name="supplierID">
<option value="1" <?php if($suppID == 1) echo 'selected'; ?> >1</option>
<option value="2" <?php if($suppID == 2) echo 'selected'; ?> >2</option>
<option value="3" <?php if($suppID == 3) echo 'selected'; ?> >3</option>
<option value="4" <?php if($suppID == 4) echo 'selected'; ?> >4</option>
</select>
Hope this helps.
To select the values in drop down, the selected attribute is required. You may check with the code below:
<select name="supplierID">
<option value="1" <?php if($id == 1) echo "selected"; ?>> A </option>
<option value="2" <?php if($id == 2) echo "selected"; ?>> B </option>
<option value="3" <?php if($id == 3) echo "selected"; ?>> C </option>
</select>

How to display the multiple data on sumoselect dropdown from the database?

I am using sumoselect (http://hemantnegi.github.io/jquery.sumoselect/sumoselect_demo.html) and CodeIgniter. I am displaying data from the database. I am inserting product type multiple data in the database like
producttype
1
1+2
1+2+3
I am displaying like this on HTML page
<select name="producttype[]" class="form-control multipleselect event_stop" id="producttype" multiple="multiple">
<option value="" selected disabled>Select</option>
<option value="1" <?php if($post->producttype =="1") echo 'selected'; ?>>One</option>
<option value="2" <?php if($post->producttype =="2") echo 'selected'; ?>>Two</option>
<option value="3" <?php if($post->producttype =="3") echo 'selected'; ?>>Three</option>
<option value="4" <?php if($post->producttype =="4") echo 'selected'; ?>>Four</option>
</select>
If I am getting 1 from the database then select dropdown displaying One but sometimes I am getting 1+2 or 1+2+3 then how can I show in the select dropdown?
Would you help me out in this?
I don't have any knowledge of sumoselect but it may help you to show selected multiple options as
$producttype_array = explode("+","1+2+3");
<select name="producttype[]" class="form-control multipleselect event_stop" id="producttype" multiple="multiple">
<option value="1" <?php if(in_array(1,$producttype_array)) echo 'selected'; ?>>One</option>
<option value="2" <?php if(in_array(2,$producttype_array)) echo 'selected'; ?>>Two</option>
<option value="3" <?php if(in_array(3,$producttype_array)) echo 'selected'; ?>>Three</option>
<option value="4" <?php if(in_array(4,$producttype_array)) echo 'selected'; ?>>Four</option>
</select>

Get value of drop down menu and save as variable in PHP

I want to get the selected value of a drop down menu and save it as a variable. I tried the following as documented in another post, but it seems not working: echo $selected = $_POST['<?php $as->my_name(); ?>'];
Drop down:
<select name="<?php $as->my_name(); ?>">
<option value="">Select this</option>
<option value="91"<?php $mb->state('91'); ?>>91</option>
<option value="90"<?php $mb->state('90'); ?>>90</option>
<option value="89"<?php $mb->state('89'); ?>>89</option>
<option value="88"<?php $mb->state('88'); ?>>88</option>
<option value="87"<?php $mb->state('87'); ?>>87</option>
<option value="86"<?php $mb->state('86'); ?>>86</option>
<option value="85"<?php $mb->state('85'); ?>>85</option>
<option value="84"<?php $mb->state('84'); ?>>84</option>
<option value="83"<?php $mb->state('83'); ?>>83</option>
<option value="82"<?php $mb->state('82'); ?>>82</option>
</select>
Post the form:
You have to put the select option within form tags
<form method="post">
<select name="example">
<option></option>
</select>
<input type="submit" />
</form>
Get the posted value:
To get the value of this example you can use:
<?php
$selected = $_POST['example'];
echo $selected;
?>
To get the value in your case:
<select name="<?php echo $as->my_name(); ?>">
<option value="test">test</option>
</select>
<?php
$selected = $_POST[$as->my_name()];
echo $selected;
?>

Set a PHP variable as the value of user selected dropdown form

I'm trying to create a form with text fields and dropdowns. With the text fields, I'm using this code:
<label for="name">Name</label>
<input type="text" id="name" name="name" value="<?php echo $user->name;?>" style="width:95%;"/>
The form then checks if the user has filled out the "name" field with this:
$name = $input->get('name', null);
if($name == null)
return false;
This works fine for the multiple text fields that the form uses, but I don't know how to check for the user input on the dropdowns. How can I send the option selected by the user, similar to the way I did it with the text field, so that I can check to make sure the user selected something? Example:
<label for="department">Department</label>
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<option value="1"><?php echo $params->get('department1');?></option>
<option value="2"><?php echo $params->get('department2');?></option>
<option value="3"><?php echo $params->get('department3');?></option>
<option value="4"><?php echo $params->get('department4');?></option>
<option value="5"><?php echo $params->get('department5');?></option>
<option value="6"><?php echo $params->get('department6');?></option>
<option value="7"><?php echo $params->get('department7');?></option>
<option value="8"><?php echo $params->get('department8');?></option>
</select>
$department1 = $input->get('department1', null);
$department2 = $input->get('department2', null);
Etc........
This is set up the exact same way as the text fields as far as I know, but doesn't work and seems like a bad way to do it anyways.
if you want to verify and pre-select one option you should do something like this:
<option value="6" <?php if($params->get('department6')==true){ echo 'selected'; } ?>><?php echo $params->get('department6');?></option>
First of all, instead of having 8 lines like that for your departments, you could use a for loop.
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<?php for($i=1; $i<=8; $i++): ?>
<option value="<?php echo $i; ?>"><?php echo $params->get('department' . $i);?></option>
<?php endfor; ?>
</select>
Now, inside your for loop, you can add a check if the $input->get is true.
<?php if($input->get('department' . $i, null)) echo 'selected'; ?>
So if you mix both together, the result would be
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<?php for($i=1; $i<=8; $i++): ?>
<option value="<?php echo $i; ?>" <?php if($input->get('department' . $i, null)) echo 'selected'; ?>><?php echo $params->get('department' . $i);?></option>
<?php endfor; ?>
</select>
And if you want to a "pre selected" option use "selected" in the option you want:
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<option value="1"><?php echo $params->get('department1');?></option>
<option value="2"><?php echo $params->get('department2');?></option>
<option value="3"><?php echo $params->get('department3');?></option>
<option value="4" selected><?php echo $params->get('department4');?></option>
<option value="5"><?php echo $params->get('department5');?></option>
<option value="6"><?php echo $params->get('department6');?></option>
<option value="7"><?php echo $params->get('department7');?></option>
<option value="8"><?php echo $params->get('department8');?></option>
</select>
As explained by W3CSchools.

drodown list php

I have a profile page in php for the user to set up his profile. The profile page prompts the user to set the gender.This is the code:
<form action='database.php' method=post>
<select name="myvalue">
<option value=" ">--select--</option>
<option value="male">male</option>
<option value="female">female</option>
</select>
<input type=submit>
</form>
after the user selects the gender, the following code gets the selection
$myvalue=$_POST['myvalue'];
and then by using a function stores value in database.
Next time that the user will visit his profile page, I want the dropdown list to show the value that selected before. For example lets say that the user selects as gender "male" the first time created his profile. If he visits again the profile page, the dropdown list must have automated as selected value, the value "male".
Try this :
<?php
$gender = /// fetch value from database
?>
<form action='database.php' method=post>
<select name="myvalue">
<option value=" ">--select--</option>
<option value="male" <?php if($gender == "male"){?> selected="selected" <?php }?>>male</option>
<option value="female" <?php if($gender == "female"){?> selected="selected" <?php }?>>female</option>
</select>
<input type=submit>
</form>
To set a option field as default selected you can use the selected keyword
<select name="myvalue">
<option value=" ">--select--</option>
<option value="male" selected>male</option>
<option value="female">female</option>
</select>
if you use xml document type you have to use
<select name="myvalue">
<option value=" ">--select--</option>
<option value="male" selected="selected">male</option>
<option value="female">female</option>
</select>
it's up to you to load values from cookie or database for that user and then perform checks to echo the "selected" value into that option field
Just check agains value and show selected="selected" in html
<?php
$value = $_POST['myvalue'];
?>
<form action='database.php' method=post>
<select name="myvalue">
<option value=" " <?php echo $value==' ' ? 'selected="selected"' : ''; ?> >--select--</option>
<option value="male" <?php echo $value=='male' ? 'selected="selected"' : ''; ?> >male</option>
<option value="female" <?php echo $value=='female' ? 'selected="selected"' : ''; ?> >female</option>
</select>
<input type=submit>
</form>
Something like this:
<form action='database.php' method=post>
<select name="myvalue">
<option value=" ">--select--</option>
<option value="male" <?php echo $gender=="male"?"selected":""; ?>>male</option>
<option value="female" <?php echo $gender=="female"?"selected":""; ?>>female</option>
</select>
<input type=submit>
</form>
But better would be to use a decent template system.
add selected attribute to the option
<select name="myvalue">
<option value=" ">--select--</option>
<?php
$selected = 'selected="selected" ';
foreach(array('male', 'female') as $v){
echo '<option '.(($v==$dbgender)? $selected : '').'value="'.$v.'">'.$v.'</option>';
}
?>
</select>
$dbgender is the value that stored for that user.
Do validation like this.
$val = value_from db;
if($val == "male") {
echo '<option value="male" selected="selected">male</option>';
} elseif($val == "female") {
echo '<option value="female" selected="selected">female</option>';
}

Categories