This question already has answers here:
Keep values selected after form submission
(12 answers)
Closed 4 months ago.
I have the selection menu which takes the value from the txt file. I want the selected value to remain selected even after the form submission.
<?php
$filename = 'select.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
?>
<form action="#" method="post">
<select id="toolchain" name="toolchain" onchange='this.form.submit()'>
<option selected value="base">Please Select</option>
<?php foreach($eachlines as $lines){
echo "<option value='".$lines."'>$lines</option>";
}?>
</select>
</form>
For keeping the value selected,I tried this:
<?php foreach($eachlines as $lines){
echo "<option value='".$lines."'" if($_POST['$lines']) echo $_POST['$lines'];">$lines</option>";
}?>
But it is not working, may be I am using echo inside the echo. Please correct me.
You can use this code. Although I haven't tested the code but let me share my logic with you to make you a better understanding of it. I have added a condition that if the form is submitted then it should display the form with $_POST['toolchain'] selected otherwise it should display it in normal ways.
<?php
$filename = 'select.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
if(isset($_POST['toolchain'])
{
?>
<form action="#" method="post">
<select id="toolchain" name="toolchain" onchange='this.form.submit()'>
<option selected value="<?php echo $_POST['toolchain']; ?>"><?php echo $_POST['toolchain']; ?></option>
<?php foreach($eachlines as $lines){
if($lines!=$_POST['toolchain'])
echo "<option value='".$lines."'>$lines</option>";
}?>
</select>
</form>
<?php }
else{
?>
<form action="#" method="post">
<select id="toolchain" name="toolchain" onchange='this.form.submit()'>
<option selected value="base">Please Select</option>
<?php foreach($eachlines as $lines){
echo "<option value='".$lines."'>$lines</option>";
}?>
</select>
</form>
<?php }
?>
It's a normal but a lengthy way. You can also add a condition inside form. If the form is submitted mean if(isset($_POST['toolchain'])) then you can make the option selected for the $_POST['toolchain'] inside foreach loop
Update
I have put the condition inside the form. Kindly update if you face any error as I haven't tested the code
<?php
$filename = 'select.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
?>
<form action="#" method="post">
<select id="toolchain" name="toolchain" onchange='this.form.submit()'>
<?php if(isset($_POST['toolchain']))
{
?>
<option value="base">Please Select</option>
<?php foreach($eachlines as $lines){
if($_POST['toolchain']==$lines))
{
echo "<option selected value='".$lines."'>$lines</option>";
}
else {
echo "<option value='".$lines."'>$lines</option>";
}
}
}
else {
?>
<option selected value="base">Please Select</option>
<?php foreach($eachlines as $lines){
echo "<option value='".$lines."'>$lines</option>";
}
}
?>
</select>
</form>
Related
I'm storing a multiple selection on page 1 PHP and would like to get the selected values in cookies/session.
<select multiple="multiple" name="subjects" size=3 multiple>
<option value="math">Mathematics
<option value="sci">Science
<option value="his">History
</select>
<?php
setcookie('subjects', $subjects);
?>
I would like to get the selected values in a page 2 PHP and print out some links:
<html>
<body>
<?php
if(isset($_COOKIE["subjects"])){
if ($_POST['subjects.value = math']) {
echo "https://en.wikipedia.org/wiki/Mathematics, https://www.niu.edu/mathmatters/everyday-life/index.shtml, https://en.wikipedia.org/wiki/Areas_of_mathematics <br />";
}
if ($_POST['subjects.value = sci']) {
echo "https://en.wikipedia.org/wiki/Science, https://en.wikipedia.org/wiki/Biology, https://en.wikipedia.org/wiki/Chemistry <br />";
}
if ($_POST['subjects.value = his']) {
echo "https://en.wikipedia.org/wiki/History, https://en.wikipedia.org/wiki/History_of_Macau, https://en.wikipedia.org/wiki/History_of_Malaysia <br />";
}
}
?>
</body>
</html>
<select multiple="multiple" name="subjects" size=3 multiple>
<option value="math">Mathematics
<option value="sci">Science
<option value="his">History
</select>
<?php
setcookie('subjects', $subjects);
foreach ($_GET['subjects'] as $selectedsubject)
echo $selectedsubject."\n";
?>
try that above but also you can check here for reference
How to get multiple selected values of select box in php?
I'm having a hard time to fix and how can make my codes work well.
My textbox echo correctly while my dropdown box is not.
Can anyone help me and also clean my codes?
I wanna know how did you do it and can u please explain it to me.
Thank you so much.
index.php
<?php include 'test.php' ?>
<form method="post" action="index.php">
Textbox: <input type="text" name="txt1" value="<?php echo $txt1;?>">
Dropdown: <select name="drpdown1" value="<?php echo $drpdown1;?>">
<option></option>
<option value="1">Mark</option>
<option value="2">Extreme</option>
</select>
<input type="submit" name="btn1">
</form>
test.php
<?php
$txt1 = "";
$drpdown1 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$txt1 = $_POST["txt1"];
$drpdown1 = $_POST["drpdown1"];
}
?>
You're not echoing the value of $drpdown1 correctly:
// this is wrong for a select:
<select name="drpdown1" value="<?php echo $drpdown1;?>">
// etc.
If you want to select automatically the previously selected value, you need to add the selected attribute:
<select name="drpdown1">
<option value="1" <?php if ($drpdown1 === '1') { echo "selected='selected'"; } ?>>Mark</option>
<option value="2" <?php if ($drpdown1 === '2') { echo "selected='selected'"; } ?>>Extreme</option>
// etc.
you have to know more about the dropdown box because you can not put the value inside the
<select value="<?php echo $drpdown1;?>">
you have to compare the value inside the option directly. example
<select name="drpdown1">
<?php
if($drpdown1 == ""){
?>
<option selected></option>
<option value="1">Mark</option>
<option value="2">Extreme</option>
<?php
}else if($drpdown1 == "1"){
?>
<option></option>
<option value="1" selected>Mark</option>
<option value="2">Extreme</option>
<?php
}
?>
</select>
On form submit I want all my input fields to be remembered, input and textarea's already work but I can't get this selection to work properly.
This is my selection
<select name="signalering">
<option value="Bezoek" selected>Bezoek</option>
<option value="Meerwerk">Meerwerk</option>
<option value="Stelpost">Stelpost</option>
<option value="Verrekenpost">Verrekenpost</option>
<option value="Levering">Levering</option>
<option value="Aandachtspunt">Aandachtspunt</option>
<option value="Tekortkoming">Tekortkoming</option>
<option value="Opname werk">Opname werk</option>
<option value="Overig">Overig</option>
</select>
If anyone knows a simple sollution to remember this dropdown selection I would be sooo happy :)
You could just check upon submission on those tag. Check is submitted value is equal to the value, then echo selected attribute:
Rough example:
<?php $options = array('Bezoek', 'Meerwerk', 'Stelpost', 'Verrekenpost', 'Levering', 'Aandachtspunt', 'Tekortkoming', 'Opname werk', 'Overig'); ?>
<select name="signalering" onchange="this.form.submit()">
<?php foreach($options as $option): ?>
<option value="<?php echo $option; ?>" <?php echo (isset($_POST['signalering']) && $_POST['signalering'] == $option) ? 'selected' : ''; ?>>
<?php echo $option; ?>
</option>
<?php endforeach; ?>
</select>
Sample Output
Sidenote: This is just an example. You do not need onchange="this.form.submit()" on the select tag.
im guessing your posting something, just stick a script at the top of the page which checks if the value exists and loop through it accordingly
if( isset($_POST['value']) )
{
//do loop here
}else{
//output default select code
}
<form id="form" name="form" action="" method="post" enctype="multipart/form-data">
<?php
$signalering = addslashes(trim($_POST['signalering']));
$options = array('Bezoek', 'Meerwerk', 'Stelpost', 'Verrekenpost', 'Levering', 'Aandachtspunt', 'Tekortkoming', 'Opname werk', 'Overig');
$sel_output = '<select name="signalering" onChange="this.form.submit()">';
$sel_output .= '<option value="">Select</option>';
foreach($options as $option)
{
if($signalering == $option){$selcted = 'selected';}else{$selcted = '';}
$sel_output .= '<option value="'.$option.'" '.$selcted.'>'.$option.'</option>';
}
$sel_output .= '</select>';
echo $sel_output;
?>
</form>
I have limited JavaScript knowledge and am trying to change a form so it autochanges when the drop down is selected without having to click a go button.
How would I amend the following code? There are 3 separate drop downs in there. I've tried inserting onselect in there but it didnt seem to work?
<select name="season">
<option value="0"><?= $txt_all ?></option>
<?php
while($data = mysql_fetch_array($get_seasons))
{
if($data['SeasonID'] == $defaultseasonid)
echo "<option value=\"$data[SeasonID]\" SELECTED>$data[SeasonName]</option>\n";
else
echo "<option value=\"$data[SeasonID]\">$data[SeasonName]</option>\n";
}
mysql_free_result($get_seasons);
?>
</select>
<select name="matchtype">
<option value="0"><?= $txt_all ?></option>
<?php
while($data = mysql_fetch_array($get_types))
{
if($data['MatchTypeID'] == $defaultmatchtypeid)
echo "<option value=\"$data[MatchTypeID]\" SELECTED>$data[MatchTypeName]</option>\n";
else
echo "<option value=\"$data[MatchTypeID]\">$data[MatchTypeName]</option>\n";
}
mysql_free_result($get_types);
?>
</select> <input type="submit" name="submit" value="Go"><td bgcolor="<?php echo $inside_c ?>" align="center">
Other Stats <br><select name="changeto">
<option value="1"><?= $txt_match_statistics ?></option>
<option value="2"><?= $txt_recordbook ?></option>
<option value="5"><?= $txt_opponent_list ?></option>
<option value="6"><?= $txt_this_day ?></option>
</select> <input type="submit" name="changepage" value="Go">
With jQuery you can listen your drop down list when it changes.
$('select').change(function(){
// Do stuff
});
If you want to submit your form immediately after changing drop down value you can do this:
$('select').change(function(){
$('form').submit();
});
In right context you can see example from here(and mess around with it!): http://jsfiddle.net/EB635/
Im trying to implement the search feature in my website.
when the search keyword is entered in the textbox, and the category combo is selected, the form will be Posted and the result will be shown on the same page.
what i want is to keep the selected category of the combo by default in the form after posted
For eg., If i select the category 'Automobiles' in the combo and click search, after form submit, the combo should show the automobiles as default selected option. Please help me. Any help will be appreciated
I assume you get categories from database.
you should try:
<?php
$categories = $rows; //array from database
foreach($rows as $row){
if($row['name'] == $_POST['category']){
$isSelected = ' selected="selected"'; // if the option submited in form is as same as this row we add the selected tag
} else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$row['id']."'".$isSelected.">".$row['name']."</option>";
}
?>
Assuming that by "combo" you mean "A regular select element rendering as a drop down menu or list box" and not "A combobox that is a combination of a drop down menu and free text input":
When outputting the <option> elements, check the value against the submitted data in $_POST / $_GET and output selected (in HTML) or selected="selected" (in XHTML) as an attribute of the option element.
Here is the JQuery way I am using.
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
$("#name").val("<?php echo $_POST['name'];?>");
</script>
But this is only if you have jquery included in your webpage.
Regards
<?php
$example = $_POST["friend"];
?>
<form method="POST">
<select name="friend">
<option value="tom" <?php if (isset($example) && $example=="tom") echo ' selected';?>>Thomas Finnegan</option>
<option value="anna" <?php if (isset($example) && $example=="anna") echo ' selected';?>>Anna Karenina</option>
</select>
<br><br>
<input type="submit">
</form>
This solved my problem.
This Solved my Problem. Thanks for all those answered
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
document.getElementById('name').value = "<?php echo $_GET['name'];?>";
</script>
$countries_uid = $_POST['countries_uid'];
while($row = mysql_fetch_array($result)){
$uid = $row['uid'];
$country = $row['country_name'];
$isSelected = null;
if(!empty($countries_uid)){
foreach($countries_uid as $country_uid){//cycle through country_uid
if($row['uid'] == $country_uid){
$isSelected = 'selected="selected"'; // if the option submited in form is as same as this row we add the selected
}
}
}else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$uid."'".$isSelected.">".$country."</option>";
}
this is my solutions of multiple select dropdown box after modifying Mihai Iorga codes
After trying al this "solves" nothing work. Did some research on w3school before and remember there was explanation of keeping values about radio. But it also works for Select option. See here an example. Just try it out and play with it.
<?php
$example = $_POST["example"];
?>
<form method="post">
<select name="example">
<option <?php if (isset($example) && $example=="a") echo "selected";?>>a</option>
<option <?php if (isset($example) && $example=="b") echo "selected";?>>b</option>
<option <?php if (isset($example) && $example=="c") echo "selected";?>>c</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
Easy solution:
If select box values fetched from DB then to keep selected value after form submit OR form POST
<select name="country" id="country">
<?php $countries = $wpdb->get_results( 'SELECT * FROM countries' ); ?>
<option value="">
<?php if(isset($_POST['country'])){echo htmlentities($_POST['country']); } else { echo "Select Country *"; }?>
</option>
<?php foreach($countries as $country){ ?>
<option <?php echo ($_POST['country'] == $country->country_name ? 'selected="selected"':''); ?> value="<?php echo $country->country_name; ?>"><?php echo $country->country_name; ?>
</option>
<?php } ?>
</select>