I have the following code.
<?php
session_start();
?>
<form action = "index.php" method = "post" name = "test">
<!--<div id ="custom-select" style="width:200px;" onchange = "favsports()"> -->
<div class = "custom-select">
<div class = "select">
<select id ="custom-select" name = "custom-select" style="width:200px;" onchange = " this.form.submit(); myFunction();">
<option value="Abteilung auswählen" <?php if(isset($_POST['custom-select']) && $_POST['custom-select'] == 'Abteilung auswählen' || $_SESSION['custom-select'] == 'Abteilung auswählen') echo 'selected="selected" '; ?> >Abteilung auswählen</option>
<option value="TL-311" <?php if(isset($_POST['custom-select']) && $_POST['custom-select'] == 'TL-311' || $_SESSION['custom-select'] == 'TL-311') echo 'selected="selected" '; ?> >TL-311</option>
<option value="TP-271" <?php if(isset($_POST['custom-select']) && $_POST['custom-select'] == 'TP-271' || $_SESSION['custom-select'] == 'TP-271') echo 'selected="selected" '; ?> >TP-271</option>
<option value="TP-310" <?php if (isset($_POST['custom-select']) && $_POST['custom-select'] == 'TP-310' || $_SESSION['custom-select'] =='TP-310') echo 'selected="selected" '; ?> >TP-310</option>
</select>
</div>
</div>
</form>
<?php
if (isset($_POST["custom-select"]))
{
$_SESSION['custom-select'] = $_POST["custom-select"];
//print_r($_SESSION);
}
?>
I am using the same code in two pages because the selected value in the first page schould still be selected when the user go on the next page. I am also using the following Javascript code because the selected value schould be save and useon the Pages.
function myFunction()
{
var optione = '<?php echo $_SESSION['custom-select']; ?>';
if(optione !== '')
{
document.getElementById("custom-select").value = optione;
}
document.getElementById("insert").value = optione;
var mytext = optione;
if (mytext == "Abteilung auswählen")
{
alert("Wählen Sie bitte eine gültige Abteilung!");
mytext = null;
file = "TL-311";
}
else
{
mytext = optione;
window.file = mytext;
}
return window.file;
}
The code is working but my problem is that: I have to select a value twice before setting the selected value and when I want to change the selected value, I must also select the other value twice before setting it as selected. can anyone tell me what I'm doing wrong? or What is actually wrong in my code? Please I am new in PHP and HTML programming.