Satish
Satish

Reputation: 41

How to store value from dropdown to use in other php page without action

I have this code -

<html>
<head>
</head>
<body>
<select name="feature" id="Feature" onchange="myFunction()">
        <?php
        ?>
        <option value > Select Feature</option>
        <?php
        foreach($newFeature as $feat)
        {
            ?>
        <option value="<?php echo $feat;?>"><?php echo $feat;?></option>
            <?php
       }
         ?>
</select>
</form>
</body>
</html>


    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script type="text/javascript">
    function myFunction()
    {
    var x=document.getElementById("Feature");

}
</script>

here how can I use cookies or session variable to store selected value and use it in other php page ?

please guide

Upvotes: 1

Views: 2014

Answers (6)

Louis Loo
Louis Loo

Reputation: 741

u can submit ur form data to php for process via ajax, and u can do use php to create cookie or session

EXAMPLE:

$('#Feature').change(function(){
   $.ajax({
     type:'POST',
     data: $(this).serialize(),
     url: '/process',
     success:function(data,textStatus, jqXHR){
         //do something
     }
    });
});

PHP:

$_SESSION['feature'] = $_POST['feature'];

Upvotes: 0

Yingding Wang
Yingding Wang

Reputation: 500

You can use javascript to write to your browser localstorage. But it still need to be send als request parameter to your other php pages. The thing is that php is server side. And the session parameters are hold by your php server. You can store the data als string locally, but you still need to reload it into your new page from the client side.

Just an example for using localStorage
// save in the localStorage
if (window.localStorage) {
    var value = "hallo";
    localStorage.setItem("value", value);
}
// load the localStorage 
var value=localStorage.getItem("value");

You can now us that in your client

Upvotes: 1

Mohammad Arshi
Mohammad Arshi

Reputation: 386

Use javascript cookies ,set the cookies in your function and get this cookie in other page.

here is example http://www.w3schools.com/js/js_cookies.asp

Upvotes: 1

sshet
sshet

Reputation: 1160

Without having submit button you can store selected value in jquery cookie as

$.cookie('key', 'value');

and in next php page u can get this value by using $_COOKIE variable

Upvotes: 1

SagarPPanchal
SagarPPanchal

Reputation: 10121

You have to use this.value in select box or replace

<select name="feature" id="Feature" onchange="myFunction(this.value)">

add this alert(x) after var x=document.getElementById("Feature"); for check

Upvotes: 0

Amarnath R Shenoy
Amarnath R Shenoy

Reputation: 5201

in this context it is best that you use cookie

I did this for someone else, hop this help you to get an idea of cookie usage. What this do is keep the selected value set after page refresh by fetching it from cookie, you can fetch it in your desired page instead

HTML

   <select id="selectCurrency">
        <option value="1">US</option>
        <option value="98">RS</option>
        <option value="61">Ind</option>
    </select>

Jquery

//Fetching cookie you can do this in any other page 
$(document).ready(function(e){
var name = "Currency="; //Name of the cookie
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++)
  {
  var c = ca[i].trim();
  if (c.indexOf(name)==0) $('#selectCurrency').val(c.substring(name.length,c.length));
  }
});



//Setting Cookie
$('#selectCurrency').change(function(e)
{
    var cookieVal = "Currency="+$(this).val();
document.cookie = cookieVal ;

});

http://jsfiddle.net/AmarnathRShenoy/HM3Zj/9/

For more check this

http://www.w3schools.com/js/js_cookies.asp

Upvotes: 1

Related Questions