MusicProgrammer
MusicProgrammer

Reputation: 1

make an option from select tag stays the same after reload or change the page, PHP

So im having problems on figuring out how to make option tag so when is pressed it will make the action on all the pages This is what i have done:

index1.php
 include 'select.php'
<title>Ultra 2014 <?php if (isset($title)) {echo $title;} else{echo "Miami";}?> Area</title>

index2.php
 include 'select.php'
<title>Ultra 2014 <?php if (isset($title)) {echo $title;} else{echo "Miami";}?> Area</title>

select.php

<div id="regContainer">
    <form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
        <select id="selectedRegion" name="selectedRegion" size="1" autocomplete="off"> 
            <option  value="miami">miami</option>
            <option  value="new york">ny</option>
        </select>  
        <input type="submit" value="Enter Region" />
    </form>  
</div> 


process.php
<?php if(!isset($_SESSION)){ session_start();}


    if(isset($_POST['selectedRegion'])){

    $region = $_POST['selectedRegion'];

    if($region == "miami"){
    echo "miami selected";

    $title = "miami";

    }

    if($region == "new york"){
    echo "new york selected";

    $title = "new york";

     }


    }
?>

when I press the first option, it changes the title normally on the index1.php, but when i go to the index2.php... it does not change the title of that page, I have to press the first option button again to make the change. Is there anyway to just press the first option from the selection and then change the title of all the pages without having to press the button again?

Any suggestion will help. Thanks

Upvotes: 0

Views: 1493

Answers (2)

Ryan
Ryan

Reputation: 3582

You need to change

if(!isset($_SESSION)){ session_start();}

To just

session_start();

What you're saying currently is if you have a session set, don't load the session library to then use that session data.

Upvotes: 0

Kevin Lynch
Kevin Lynch

Reputation: 24703

Using the code you currently have you could do the following

<form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
        <select id="selectedRegion" name="selectedRegion" size="1" autocomplete="off"> 
            <option <?php if($_REQUEST['selectedRegion'] == 'miami') echo "selected"; ?>  value="miami">miami</option>
            <option <?php if($_REQUEST['selectedRegion'] == 'newyork') echo "selected"; ?>  value="newyork">ny</option>
        </select>  
        <input type="submit" value="Enter Region" />
    </form>

This will ensure the option is selected on page refresh. If you want something that also acts on page change then you will need to use a cookie

if(isset($_REQUEST['selectedRegion'])){
    setcookie("region", $_REQUEST['selectedRegion'], time()+3600);
}

<form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
            <select id="selectedRegion" name="selectedRegion" size="1" autocomplete="off"> 
                <option <?php if($_COOKIE["region"] == 'miami') echo "selected"; ?>  value="miami">miami</option>
                <option <?php if($_COOKIE["region"] == 'newyork') echo "selected"; ?>  value="newyork">ny</option>
            </select>  
            <input type="submit" value="Enter Region" />
        </form>

Upvotes: 1

Related Questions