I'm posting variables from my form on to another form, all the variables that are being posted on an INPUT field are showing fine. When a variable is needed to post on a SELECT how can I do this?
I tried within the <SELECT> tags to use value="<?php echo $myvariable; ?>" but this returns a blank.
To set the selected option within a <select> tag, use the selected attribute. In this example, "Value 3" is selected:
<select name="myvariable">
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3" selected>Value 3</option>
</select>
We can use PHP with something like this:
<select name="myvariable">
<option value="1" <?php echo ($myvariable == '1' ? 'selected' : '')?>>Value 1</option>
<option value="2" <?php echo ($myvariable == '2' ? 'selected' : '')?>>Value 2</option>
<option value="3" <?php echo ($myvariable == '3' ? 'selected' : '')?>>Value 3</option>
</select>
Hope this helps :) x
Your usage is incorrect. Check select documentation over here.
Example usage
<!-- The second value will be selected initially -->
<select name="select">
<option value="value1">Value 1</option>
<option value="value2" selected>Value 2</option>
<option value="value3">Value 3</option>
</select>
So for your case you should say
<select name="select">
<option value="value1"><?php echo $myvariable; ?></option>
</select>
Related
This question already has answers here:
How can I set the default value for an HTML <select> element?
(34 answers)
Closed 6 years ago.
When will I update my semester that time by default a checkbox to be checked how it possible ?
<select name="semester">
<option value="">Select Semester</option>
<option value="1">Semester 1</option>
<option value="2">Semester 2</option>
<option value="3">Semester 3</option>
</select>
You can use the selected attribute.
<select name="semester">
<option value="">Select Semester</option>
<option value="1" selected="selected">Semester 1</option>
<option value="2">Semester 2</option>
<option value="3">Semester 3</option>
</select>
Please try this
<select name="semester">
<option value="">Select Semester</option>
<option value="1" <?php if($data['semester']==1){echo 'selected';}?>>Semester 1</option>
<option value="2" <?php if($data['semester']==2){echo 'selected';}?>>Semester 2</option>
<option value="3" <?php if($data['semester']==3){echo 'selected';}?>>Semester 3</option>
</select>
If you want to select semester 2 so here you go:
HTML:
<select name="semester">
<option value="">Select Semester</option>
<option value="1">Semester 1</option>
<option value="2" selected>Semester 2</option>
<option value="3">Semester 3</option>
</select>
Jquery:
$("select option").each(function(){
if ($(this).text() == "Semester 2")
$(this).attr("selected","selected");
});
yes,it possible. you have to use selected in <option>
<select name="semester">
<option value="">Select Semester</option>
<option value="1" selected>Semester 1</option>
<option value="2">Semester 2</option>
<option value="3">Semester 3</option>
</select>
DEMO
<option value="1" <?= ($value=='1')?'selected':"" ?>>Semester 1</option>
<option value="2" <?= ($value=='2')?'selected':"" ?>>Semester 2</option>
<option value="3" <?= ($value=='3')?'selected':"" ?>>Semester 3</option>
you can get the value form database or session,
We really need your specific PHP code to help, but perhaps this generic example will show you how to do this when you have your data and your desired value:
<?php
//Your array of data
$myData = array(
'1' => 'Semester 1',
'2' => 'Semester 2',
'3' => 'Semester 3');
//The value you want to be selected
$myValue = "2";
?>
<select name="semester">
<option value="">Select Semester</option>
<?php
foreach($myData as $key => $value){
echo ('<option value="'.$key.'"' );
if( $myValue == $key){
echo( ' selected="selected" ');
}
echo ('>'.$value.'</option');
echo "\n";
}
?>
</select>
I have a form with several <select> that have the same name and an onChange="document.forms['form_mes_reservations'].submit();" on all of them. Therefore, when I select an option from one of them, the page reloads and there is some PHP treatment done.
This is an example :
<form method="post" id="form_process">
<select name="processed" onChange="document.forms['form_process'].submit();">
<option value="1_choose">Please choose...</option>
<option value="1_yes">Yes</option>
<option value="1_no"></option>
</select>
<select name="processed" onChange="document.forms['form_process'].submit();">
<option value="2_choose">Please choose...</option>
<option value="2_yes">Yes</option>
<option value="2_no"></option>
</select>
<select name="processed" onChange="document.forms['form_process'].submit();">
<option value="3_choose">Please choose...</option>
<option value="3_yes">Yes</option>
<option value="3_no"></option>
</select>
<select name="processed" onChange="document.forms['form_process'].submit();">
<option value="4_choose">Please choose...</option>
<option value="4_yes">Yes</option>
<option value="4_no"></option>
</select>
</form>
What I'd like to know is this : is there a way to know which select has triggered the onChange ? Let me explain : in the $_POST['processed'], I get ALL the selected options, so if I have only selected "3_yes", I'll get something like this :
1_choose
2_choose
3_yes
4_choose
Is it possible to only have "3_yes" as a result ? Potentially, this is going to be a very long list of selects and I don't want to check all of them for changes...
Just make all select elements as array and check which is selected:
HTML:
<form method="post" id="form_process" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="processed[]" onChange="document.forms['form_process'].submit();">
<option value="1_choose">Please choose...</option>
<option value="1_yes">Yes</option>
<option value="1_no"></option>
</select>
<select name="processed[]" onChange="document.forms['form_process'].submit();">
<option value="2_choose">Please choose...</option>
<option value="2_yes">Yes</option>
<option value="2_no"></option>
</select>
<select name="processed[]" onChange="document.forms['form_process'].submit();">
<option value="3_choose">Please choose...</option>
<option value="3_yes">Yes</option>
<option value="3_no"></option>
</select>
<select name="processed[]" onChange="document.forms['form_process'].submit();">
<option value="4_choose">Please choose...</option>
<option value="4_yes">Yes</option>
<option value="4_no"></option>
</select>
</form>
PHP:
<?php
foreach($_POST['processed'] AS $key => $value){
if(strpos($value, 'choose') !== false){
unset($_POST[processed][$key]);
}
}
print_r($_POST['processed']);
?>
Output:
Array
(
[1] => 2_yes
)
I have this select button:
<form method="post">
<label>
<select id="label" name="challengeX" style="margin-top:150px;">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
</label>
</form>
In the same page, few lines bellow, I have:
<div id="challenge">
<?php
$Ch = $_POST['challengeX'];
echo "Challenge: ".$Ch;
showSomething($Ch,1,1);
?>
</div>
But when I change the option, it didn't change the post value.
$Ch = $_POST['challengeX'];
won't get value until you submit the form. Add a submit button to your form.
<form method="post" action="">
<label>
<select id="label" name="challengeX" style="margin-top:150px;">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
</label>
<input type="submit" value="Submit">
</form>
And access the form values in the same page only if the form is submitted.
<div id="challenge">
<?php
if(isset($_POST)) // checks whether any value is posted
{
$Ch = $_POST['challengeX'];
echo "Challenge: ".$Ch;
showSomething($Ch,1,1);
}
?>
</div>
you need to submit form on change of dropdown.
Change html like this:
<form method="post">
<label>
<select id="label" name="challengeX" style="margin-top:150px;" onchange="this.form.submit()">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
</label>
</form>
for your php execution you can try like this:
<div id="challenge">
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$Ch = $_POST['challengeX'];
echo "Challenge: ".$Ch;
showSomething($Ch,1,1);
function showSomething($Ch,1,1){
//do your stuff
}
}
?>
</div>
PHP runs on the server and delivers HTML (or other data) to the browser.
If you want to process the form data with PHP then you must include it in a new HTTP request (typically by submitting the form) and have PHP generate a new page using that data.
Alternatively, if you want to process the data on the client then you will need to use a programming language that is supported in the browser. Unless you want to start using plugins (or browser specific technologies), that limits you to JavaScript. You can bind a change event listener to the select element and then manipulate the DOM with the results.
In the way you can use Javascript. If you dont want to use any server like PHP or something
<form method="post">
<label>
<select id="label" name="challengeX" style="margin-top:150px;">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
<input type="submit" /> /* add this line */
</label>
</form>
By Javascript
<select id="label" name="challengeX" style="margin-top:150px;" onChange="getSelect(this)">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
<div id="resultSelect"></div>
<script>
function getSelect(thisValue){
document.getElementById('resultSelect').innerHTML=thisValue.options[thisValue.selectedIndex].text;
}
</script>
i have a simple drop down select menu.
<div id="select">
<select class="select">
<option value="year 1">year 1</option>
<option value="year 2">year 2</option>
<option value="year 3">year 3</option>
</select>
</div>
How do i take the value that the user selects and store it into a php variable?
in somephpfile.php
$selected = $_POST['somename'];
html
<form action="somephpfile.php" method="post">
<div id="select">
<select class="select">
<select name="somename"> <!-- you missed this -->
<option value="year 1">year 1</option>
<option value="year 2">year 2</option>
<option value="year 3">year 3</option>
</select>
</div>
</form>
you missed the "name" attribut in the select and the form tag. Try this:
<HTML><BODY>
<?PHP
$sel_year= $_POST['select'];
echo $sel_year
?>
<FORM method="post" action="...your-php-file-name-here...">
<div id="select">
<select name="select">
<option value="year 1">year 1</option>
<option value="year 2">year 2</option>
<option value="year 3">year 3</option>
</select>
</div>
</FORM>
</BODY></HTML>
<form action="" method="post">
<div id="select">
<select class="select" name="selectoptionname">
<option value="year 1">year 1</option>
<option value="year 2">year 2</option>
<option value="year 3">year 3</option>
</select>
</div>
</form>
when form submits you can get the value of selected option using $_POST['selectoptionname']
put it in a form , then use $_POST['select']
I want to get the value of the dropdown. I am using msDropDown, but i am getting its value as undefined. The html as follows:
<select name="category[]"
id="webmenus_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>"
onchange="showValue(this.value)"
>
<option value="0" selected="selected" title="Please select hotel category"></option>
<option value="5_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/5star.png"></option>
<option value="4_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/4star.png"></option>
<option value="3_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/3star.png"></option>
<option value="2_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/2star.png"></option>
<option value="1_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/1star.png"></option>
</select>
jquery alert($("input[name='category[]']").val()); alerts as undefined.
How can i get the value of the dropdown?
Thanks,
$(":input[name='category[]']").val()
You use select instead of input
Edit your HTML and set the select Tag to mutiple
<select name="test" multiple>
<option value="1">ITEM 1</option>
<option value="2">ITEM 2</option>
<option value="3">ITEM 3</option>
</select>
alert($('select[name="test"]').val());
// outputs 1,2,3 when you select all three options
try
$("class of select ").change(function() {
alert($(this).val());
});