Change selected option in select (form) - php

I have an intial group of options like these:
<select name="item[type]">
<option value="0" class="dr">First</option>
<option value="1" class="dr">Second</option>
<option value="2" class="dr">Third</option>
<option value="3" class="dr">Fourth</option>
</select>
I want to check if a variable isset($test) is defined. If it is, then I want to change the option selected where the value is equal of $test. Something like this <OPTION SELECTED>
For example. $test = 3; so, the option selected should be fourth. If $test is empty or not defined, then the first should be the option that is selected.

One way:
<select name="item[type]">
<option <?=$test==0?'selected="selected"':'';?> value="0" class="dr">First</option>
<option <?=$test==1?'selected="selected"':'';?> value="1" class="dr">Second</option>
<option <?=$test==2?'selected="selected"':'';?> value="2" class="dr">Third</option>
<option <?=$test==3?'selected="selected"':'';?> value="3" class="dr">Fourth</option>
</select>
Another:
<? $selected[$test] = 'selected="selected"'; ?>
<select name="item[type]">
<option <?=$selected[0];?> value="0" class="dr">First</option>
<option <?=$selected[1];?> value="1" class="dr">Second</option>
<option <?=$selected[2];?> value="2" class="dr">Third</option>
<option <?=$selected[3];?> value="3" class="dr">Fourth</option>
</select>

<select name="item[type]" id="selectBoxId">
<option value="0" class="dr">First</option>
<option value="1" class="dr">Second</option>
<option value="2" class="dr">Third</option>
<option value="3" class="dr">Fourth</option>
</select>
<script type="text/javascript">
var test = "<?= $test; ?>";
if (test != '' && parseInt(test)) {
document.getElementById('selectBoxId').selectedIndex = test;
}
</script>

Remove "[type]" from select name, make it simple to "item".
Then execute this code.
$test = isset($_POST['item']) ? $_POST['item'] : "0";

// assuming you are using a loop: in the loop where you create the options
$selected_html = $test == $loop_var ? ' selected="selected" ' : '';
echo "<option value=\"$loop_var\" class=\"dr\"$selected_html>$text</option>";

Related

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>

php, phpMyAdmin - dropdown values not inserting into database

I'm trying to add the values, that I pick from the dropdown menus (see HTML), to a database table.
Yet this is not working out like I planned.
I'm I doing something wrong?
At the moment the columns for the dropdown values are set as INT in the table of the database.
Code below:
HTML:
<head>
<meta charset ="UTF-8">
<title>El Gusto - Sigaren toevoegen</title>
</head>
<body>
<form action="handler.php" method="post">
<span class="tab"><b>Sensatie in de mond:</b><br>
<span class="tab"><span class="tab">Lip:
<select>
<option value="1" name="sensatie_lip_1">1</option>
<option value="2" name="sensatie_lip_2">2</option>
<option value="3" name="sensatie_lip_3">3</option>
<option value="4" name="sensatie_lip_4">4</option>
</select><br>
<span class="tab"><span class="tab">Tong:
<select>
<option value="1" name="sensatie_tong_1">1</option>
<option value="2" name="sensatie_tong_2">2</option>
<option value="3" name="sensatie_tong_3">3</option>
<option value="4" name="sensatie_tong_4">4</option>
</select><br>
<span class="tab"><span class="tab">Wangen:
<select>
<option value="1" name="sensatie_wangen_1">1</option>
<option value="2" name="sensatie_wangen_2">2</option>
<option value="3" name="sensatie_wangen_3">3</option>
<option value="4" name="sensatie_wangen_4">4</option>
</select><br>
<span class="tab"><span class="tab">Keel:
<select>
<option value="1" name="sensatie_keel_1">1</option>
<option value="2" name="sensatie_keel_2">2</option>
<option value="3" name="sensatie_keel_3">3</option>
<option value="4" name="sensatie_keel_4">4</option>
</select><br>
</span><br>
<b>PRESENTATIE (SIGARENBAND, DOOS, ...)</b>
<select>
<option value="1" name="presentatie_1">1</option>
<option value="2" name="presentatie_2">2</option>
<option value="3" name="presentatie_3">3</option>
<option value="4" name="presentatie_4">4</option>
<option value="5" name="presentatie_5">5</option>
</select><br>
<br>
<b>KWOTERING</b>
<select>
<option value="1" name="kwotering_1">1</option>
<option value="2" name="kwotering_2">2</option>
<option value="3" name="kwotering_3">3</option>
<option value="4" name="kwotering_4">4</option>
<option value="5" name="kwotering_5">5</option>
</select><br>
<br>
<input type='submit' name="Toevoegen" value='Toevoegen'>
</form>
</body>
PHP:
//WARM - SENSATIE IN DE MOND (DROPDOWN)
//LIP
$sensatie_lip_1 = isset($_POST['sensatie_lip_1']);
$sensatie_lip_2 = isset($_POST['sensatie_lip_2']);
$sensatie_lip_3 = isset($_POST['sensatie_lip_3']);
$sensatie_lip_4 = isset($_POST['sensatie_lip_4']);
//TONG:
$sensatie_tong_1 = $_POST['sensatie_tong_1'];
$sensatie_tong_2 = $_POST['sensatie_tong_2'];
$sensatie_tong_3 = $_POST['sensatie_tong_3'];
$sensatie_tong_4 = $_POST['sensatie_tong_4'];
//WANGEN:
$sensatie_wangen_1 = $_POST['sensatie_wangen_1'];
$sensatie_wangen_2 = $_POST['sensatie_wangen_2'];
$sensatie_wangen_3 = $_POST['sensatie_wangen_3'];
$sensatie_wangen_4 = $_POST['sensatie_wangen_4'];
//KEEL:
$sensatie_keel_1 = $_POST['sensatie_keel_1'];
$sensatie_keel_2 = $_POST['sensatie_keel_2'];
$sensatie_keel_3 = $_POST['sensatie_keel_3'];
$sensatie_keel_4 = $_POST['sensatie_keel_4'];
//PRESENATIE (DROPDOWN)
$presentatie_1 = $_POST['presentatie_1'];
$presentatie_2 = $_POST['presentatie_2'];
$presentatie_3 = $_POST['presentatie_3'];
$presentatie_4 = $_POST['presentatie_4'];
$presentatie_5 = $_POST['presentatie_5'];
//KWOTERING (DROPDOWN)
$kwotering_1 = $_POST['kwotering_1'];
$kwotering_2 = $_POST['kwotering_2'];
$kwotering_3 = $_POST['kwotering_3'];
$kwotering_4 = $_POST['kwotering_4'];
$kwotering_5 = $_POST['kwotering_5'];
//DATABASE
$connect = mysql_connect("localhost", "root", "") or die ("Couldn't connect to the database!");
mysql_select_db("el_gusto") or die ("Couldn't find db!");
$query = mysql_query("INSERT INTO sigaren(sensatie_lip_1, sensatie_lip_2, sensatie_lip_3, sensatie_lip_4, sensatie_tong_1, sensatie_tong_2, sensatie_tong_3, sensatie_tong_4, sensatie_wangen_1, sensatie_wangen_2, sensatie_wangen_3, sensatie_wangen_4, sensatie_keel_1, sensatie_keel_2, sensatie_keel_3, sensatie_keel_4, presentatie_1, presentatie_2, presentatie_3, presentatie_4, presentatie_5, kwotering_1, kwotering_2, kwotering_3, kwotering_4, kwotering_5) VALUES
('$sensatie_lip_1',
'$sensatie_lip_2',
'$sensatie_lip_3',
'$sensatie_lip_4',
'$sensatie_tong_1',
'$sensatie_tong_2',
'$sensatie_tong_3',
'$sensatie_tong_1',
'$sensatie_wangen_1',
'$sensatie_wangen_2',
'$sensatie_wangen_3',
'$sensatie_wangen_4',
'$sensatie_keel_1',
'$sensatie_keel_2',
'$sensatie_keel_3',
'$sensatie_keel_4',
'$presentatie_1',
'$presentatie_2',
'$presentatie_3',
'$presentatie_4',
'$presentatie_5',
'$kwotering_1',
'$kwotering_2',
'$kwotering_3',
'$kwotering_4',
'$kwotering_5')");
if($query) {
echo "Toevoeging gelukt!";
}
else{
echo mysql_error();
}
?>
HTML:
<select name="sensatie_lip">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
PHP:
$sensatie_lip = $_POST['sensatie_lip']; // you will get your selected value
instead of giving name to option tag give name to select tag.
Currently you are not getting the values of dropdown after submitting the form
Rightly said by Disha and Jasbir,Select way used is incorrect and try changing it as suggested and see that to work
<select name="sensatie" >
<option value="1">sensatie_wangen_1</option>
<option value="2">sensatie_wangen_2</option>
</select>
So it will display 2 drop down as sesatie_wangen_1, 2 and on save db will have value 1, or 2. It is a hint and you need to adjust code to make it work based on this

Show current database record

For the 'wachtwoord' input field, the value is the current database record. But how can I show the current record for the 'Jaar' select field? It's currently showing one, but it has to be two.. Anyone know how to solve this?
<label for="password">Wachtwoord</label>
<input type="password" id="password" name="password" value="<?php echo $t['gids_password'] ?>"/>
<br/>
<label for="jaar">Jaar</label>
<select name="jaar" id="jaar">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
You would need to compare the result from the database with the value of the options. If it matches, you need to echo 'selected'; Like so:
<option value="1" <?php if($t['gids_jaar']==1){ echo 'selected'; } ?> >1</option>
<option value="2" <?php if($t['gids_jaar']==2){ echo 'selected'; } ?> >2</option>
<option value="3" <?php if($t['gids_jaar']==3){ echo 'selected'; } ?> >3</option>
NOTE:
$t['gids_jaar'] would be the value of the database. Dont forget to put space between the closing php tag and the end of the option open tag. So this part has to have a space:
?> >3</option>
there has to be a space between the ?> and >
The answer of Daan works as well but I just wanted to add some additional information.
You need to add a selected to one of your option tags, most easy is with a ternary operator:
<option value="1" <?php echo ($t['myvalfromdb'] == 1) ? 'selected' : ''; ?> >1</option>
<option value="2" <?php echo ($t['myvalfromdb'] == 2) ? 'selected' : ''; ?> >1</option>
<option value="3" <?php echo ($t['myvalfromdb'] == 3) ? 'selected' : ''; ?> >1</option>

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

Setting the selectedindex property of an HTML dropdown using PHP

From the database I pull back the user information and based on this I can determine the correct index value using a case statement:
<?php
$genderIndex = 0;
switch ($displayProperties['gender'])
{
case "":
$genderIndex = 0;
break;
case "Male":
$genderIndex = 1;
break;
case "Female":
$genderIndex = 2;
break;
case "Other":
$genderIndex = 3;
break;
}
?>
On the form I have an HTML dropdown (select):
<select name="gender" selectedIndex="<?php echo $genderIndex; ?>">
<option value="0"> </option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select><br/>
The problem is it never works and always loads the page at index=0 and I made sure to verify if $genderIndex actually has the expected value (it does).
What am I doing wrong?
selectedindex property is not used in this way, for appropriate use of the selectedindex property you can view http://www.w3schools.com/jsref/prop_select_selectedindex.asp.
Instead what you are after is applying the "selected" attribute to the appropriate option. The code below runs some shorthand if/else checks and will apply the "selected" attribute where appropriate, based on your $genderIndex you created in your function given.
<select name="gender">
<option value="0" <?php echo ($genderIndex==0)?('selected'):(''); ?>"> </option>
<option value="Male" <?php echo ($genderIndex==1)?('selected'):(''); ?>>Male</option>
<option value="Female" <?php echo ($genderIndex==2)?('selected'):(''); ?>>Female</option>
<option value="Other" <?php echo ($genderIndex==3)?('selected'):(''); ?>>Other</option>
</select><br/>
The reason this is not working is because selectedIndex is not a valid attribute on the HTML <select/> element. (See http://www.w3.org/TR/html4/interact/forms.html#h-17.6)
To pre-select an option in the <select/> element (other than the first one, in this an empty value), you need to have your HTML output look something like this:
<select name="gender">
<option value="0"> </option>
<option value="Male">Male</option>
<option value="Female" selected="true">Female</option>
<option value="Other">Other</option>
</select>
Notice the addition of the selected="true" on the second <option/> tag. Something like the following will get the job done but is hardly elegant.
<select name="gender">
<?php
if(0 == $genderIndex)
echo '<option value="0" selected="true"> </option>';
else
echo '<option value="0"> </option>';
if(1 == $genderIndex)
echo '<option value="Male" selected="true">Male</option>';
else
echo '<option value="Male">Male</option>';
if(2 == $genderIndex)
echo '<option value="Female" selected="true">Female</option>';
else
echo '<option value="Female">Female</option>';
if(3 == $genderIndex)
echo '<option value="Other" selected="true">Other</option>';
else
echo '<option value="Other">Other</option>';
?>
</select>
A good web framework can be helpful here. I know that in Java, the Struts framework assists with echoing a <select/> tag to the output with the appropriate <option/> selected.
That's not how you specify the selected <option> in HTML. You need to have the selected attribute on the <option> itself, like so:
<select name="gender">
<option value="0"> </option>
<option value="Male" selected>Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select><br/>

Categories