How to Maintain Persistence State of a Dropdown with JavaScript Disabled - PHP
Get the solution ↓↓↓Solution:
I normally do something like the following:
<select id="topic" name="topic">
<option value="Select an option">Select an option</option>
<?php
$topics=Array("Topic A","Topic B","Topic C");
foreach($topics as $topic){
echo '<option value="'.$topic.'"'.($_POST['topic']==$topic?' selected="selected"':'').'>'.$topic.'</option>';
}
?>
</select>
What this does is print out the select box just as you've described, but it adds a selected="selected" attribute to the option that matches your $_POST variable.
Answer
Solution:
<?php
$topic='';
if isset($_POST)
if is_array($_POST)
if isset($_POST['topic'])
$topic=$_POST['topic'];
?>
<select id="topic" name="topic">
<option value="Select an option">Select an option</option>
<option value="Topic A"<?php if($topic=='Topic A') echo ' selected="selected"'; ?>>Topic A</option>
<option value="Topic B"<?php if($topic=='Topic B') echo ' selected="selected"'; ?>>Topic B</option>
<option value="Topic C"<?php if($topic=='Topic C') echo ' selected="selected"'; ?>>Topic C</option>
</select>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: string literal contains an unescaped line break
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.