How can I remember dropdown selection on form submit? - php

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>

Related

Keep the selected value after the form submission [duplicate]

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>

Select element - Post method - return of value / 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>

Adding in if statement for add form to make edit form usable

I have one form that is used to add and edit something. On the form is a select tag. What I'm asking is where do I use an if statement in this to see if its the edit then to preselect a value preset for the form.
Example below is what is needed for general add form:
<select name="item_status" id="select1" class="nostyle" style="width:100%;" placeholder="Select Status">
<option></option>
<?php foreach($something AS $item)
{
echo '<option value="'.$item->item_id.'">'.$item->items_name.'</option>';
}
?>
</select>
<select name="item_status" id="select1" class="nostyle" style="width:100%;" placeholder="Select Status">
<option></option>
<?php foreach($something AS $item)
{
$selected = $_REQUEST['item_status'] === $item->item_id ? ' selected="selected"' : '';
echo '<option value="'.$item->item_id.'"'.$selected.'>'.$item->items_name.'</option>';
}
?>
</select>

make a value selected in drop down when editing

I am adding a feature to edit the ads a user puts. For that I want to make the value selected which is selected by the user when he entered the values . How can I implement this?
<div class="nm">
Category
</div>
<div class="fl">
<select name="category" id = "ad_category" onchange="cangeSubCat(this.value)" >
<option value="">---Select Category---</option>
<option value="real_estate">Real Estate</option>
<option value="hotels_resturants">Hotels and Resturants</option>
<option value="car">Car Rental</option>
</select>
</div>
Add the selected HTML <option> attribute, in XHTML it is selected="selected".
<option value="value" selected>title</option>
^^^^^^^^
Documentation: http://www.w3.org/TR/html4/interact/forms.html#h-17.6.1
I mean how will I know which one should be selected..it will be different for different users . I wnt it to be selected which user selects during adding the ad
That depends on your code. You can do this with an array with all value/title pairs (value => title) and the value which is selected. Then when key (value) equals the selected one, the attribute is added in output. As it's an array it's easy to iterate over it.
$otpions = array(
'a' => 'A',
'b' => 'B',
);
$selected = 'b';
echo '<select>';
foreach ($options as $value => $title)
{
printf('<option value="%s" %s>%s</option>',
$value, $selected == $value ? 'selected' : '', $title);
}
echo '</select>';
okay lets assume you fetch value from db and that select box value is in $select variable then you've to write
<option <?php if($select=="real_estate") { echo "selected='selected'"; } ?> value="real_estate">Real Estate</option>
<option <?php if($select=="hotels_resturants") { echo "selected='selected'"; } ?> value="hotels_resturants">Hotels and Resturants</option>
<option <?php if($select=="car") { echo "selected='selected'"; } ?> value="car">Car Rental</option>
You can use this magic function
function __selectedDb($ctrlValue,$dbValue)
{
if($ctrlValue == $dbValue)
return "selected='selected'";
else
return "";
}
like
<select name="category" id = "ad_category" onchange="cangeSubCat(this.value)" >
<option value="" <?php echo __selectedDb("","$cat")?>>---Select Category---</option>
<option value="real_estate" <?php echo __selectedDb("real_estate","$cat")?>>Real Estate</option>
<option value="hotels_resturants" <?php echo __selectedDb("hotels_resturants","$cat")?>>Hotels and Resturants</option>
<option value="car" <?php echo __selectedDb("car","$cat")?>>Car Rental</option>
</select>
I write in core php please convert php code to smarty

How to Keep the selected value of the select box after Form POST or GET

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>

Categories