how to use the same drop down list in different web pages

I have the following drop down list and I would like to use it in several pages

<form action = "Unser Team.php" method = "post" name = "test">
    <div class = "custom-select">
    <div class = "select">
        <select id ="custom-select" name = "custom-select" style="width:200px;" onchange = " this.form.submit();">
            <option <?php if ($_POST['custom-select'] == 'Abteilung auswählen') print 'selected '; ?>value="Abteilung auswählen">Abteilung auswählen</option>
            <option <?php if ($_POST['custom-select'] == 'TL-311') print 'selected '; ?> value="TL-311">TL-311</option>
            <option <?php if ($_POST['custom-select'] == 'TP-271') print 'selected '; ?> value="TP-271">TP-271</option>
            <option <?php if ($_POST['custom-select'] == 'TP-310') print 'selected '; ?> value="TP-310">TP-310</option>
            <option <?php if ($_POST['custom-select'] == 'TP-270') print 'selected '; ?> value="TP-270">TP-270</option>
        </select>
    </div>
    </div>

when the user selects an option I save it and I would like to see in the next page the option that has been selected is preselected and for its use the following code in the home page:

var file;

function myFunction()
{
    var mylist = document.getElementById("custom-select");
    var mytext = mylist.options[mylist.selectedIndex].text;
    window.file = mytext;
    return window.file;
}

window.onload = function() {
    var getInput = window.file;
    localStorage.setItem("files",getInput);
}

for the other pages:

var myFile = localStorage.getItem("files");

window.onload = function() {
    var ddValue= localStorage.getItem('files');
    var dd= document.getElementById('custom-select');
    for(var i = 0;i < dd.options.length;i++){
        if(dd.options[i].value == ddValue ){
            dd.options[i].selected = true;
            break;
        }
    }
}

with this the selected value in the home page is preselected in the next pages but the problem is that with this code I can not change the selected value i the drop down list in other pages. can please somebody help me? I am new in web programming.

Answer

First of all, welcome to Stackoverflow!

As pointed out by misorude, it might be wise to utilize the PHP session. When you switch from one page to another, without a form’s submit, you will lose the $_POST variable. Which I believe is your problem here?

What happens when you you use sessions however, is that the client saves an ID as cookie. Every time it communicates with your server, it supplies this “session_id”. This way the server will be able to grab the right session variables for that client.

Using sessions is fairly easy, call session_start(); at the top of your script. From there on you can use $_SESSION["var_name"] = "value";

For more details on the topic, you might want to read this article on w3Schools.

Leave a Reply

Your email address will not be published. Required fields are marked *