Aizen06 Posted July 14, 2014 Share Posted July 14, 2014 Hi guys, I want to find the most efficient way of setting a php variable through a link. What I'm trying to achieve is to allow the user to select a category and their location and store the two variables to update my database results. I first used the GET method, but since the page refreshes when the user selects any other Get variable, so it ends up reseting the variables. Currently I have the category and locations as drop down menu with links to each of the options. I'll appreciate any help. Thank you. :] Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 14, 2014 Share Posted July 14, 2014 So you're trying to retain their selection while they visit all other pages? Just put their selection in a $_SESSION var and echo it from there. If you're trying to do something else, you'll need to explain a bit better cause that didn't make much sense. 1 Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 15, 2014 Share Posted July 15, 2014 Pretty much what fastsol said -- this is what session variables are made for. When the user selects those drop downs, you set them up as session variables. Then have your subsequent scripts include an include file that has simple code like this at the top: session_start(); $category = isset($_SESSION['category']) ? $_SESSION['category'] : 'Some default'; Quote Link to comment Share on other sites More sharing options...
Aizen06 Posted July 15, 2014 Author Share Posted July 15, 2014 basically, is there a way I can set php variable through a link. <a href src="">stuff</a> I have created drop down list for Category and Location that user can hover their mouse over and it shows the options. Is there a way to set a variable for the option they select. e.g Location -> New York which will set the location varible to $location = 'new york'; Quote Link to comment Share on other sites More sharing options...
Aizen06 Posted July 15, 2014 Author Share Posted July 15, 2014 Pretty much what fastsol said -- this is what session variables are made for. When the user selects those drop downs, you set them up as session variables. Then have your subsequent scripts include an include file that has simple code like this at the top: session_start(); $category = isset($_SESSION['category']) ? $_SESSION['category'] : 'Some default'; I'll give it a try. Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 15, 2014 Share Posted July 15, 2014 You can pass the variables as get parameters, but they will already be hard coded into the returned html. At that point it is too late to change them other than to use javascript. You also have the same problem of retaining those settings across pages (user sets his location, but now he's on different page -- and it forgot his location. Session variables are the answer. What is it about the info we provided that you don't understand? Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 15, 2014 Share Posted July 15, 2014 Ok I see you just replied -- try out sessions and let us know if you have issues. Quote Link to comment Share on other sites More sharing options...
Aizen06 Posted July 15, 2014 Author Share Posted July 15, 2014 I've looked into sessions, and that will work for saving the variables, but how exactly am I suppose to set the variable when one of the options are selected? They're all links btw. e.g <a href src="#">location</a> Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 15, 2014 Share Posted July 15, 2014 You don't need to worry about the links: just read the session variable in your serverside script as in the example I provided. The code I didn't provide is the one that sets the variable when the user uses your form to change it. In a nutshell it's no harder than: $_SESSION['category'] = $_POST['category']; I don't want to muddy the picture, but like all routines that accept user input, you should validate the input, so I omitted that for simplicity purposes. In summary, once you are using sessions, you no longer want/need to pass the variables as url parameters. This is cleaner and nicer for your users anyways. Quote Link to comment Share on other sites More sharing options...
Aizen06 Posted July 15, 2014 Author Share Posted July 15, 2014 (edited) thanks for answering, but I don't think you quite get what I'm trying to say here. I completely understand how sessions work, but how exactly is the varriable 'category' set when the user clicks one of the categories? If I'm a user and I click on a category, then what exactly will trigger this line of code? I've created a drop down menu using list and css for both location and category. I could use drop down box for categories, but for location it can get a little messy since it will have sub items. For example, Province will be Ontario then City names and so on. To make things more clear, I'm using a list that shows sub items when user hover mouse over. Location Ontario - > Toronto (hover shows this option) That's something I can't do in drop down list <select>, right? If it's not too much to ask, then can you please show me in few lines how you're going to save the category once it is selected. Edited July 15, 2014 by Aizen06 Quote Link to comment Share on other sites More sharing options...
maxxd Posted July 15, 2014 Share Posted July 15, 2014 If you're going to load all the options at page load, you can output a select with <optgroup> tags. If you want to dynamically fill the drop-downs as your user makes selections (for instance, select 'Ontario' from the 'province' drop-down, then the separate 'cities' drop-down populates with the cities in Ontario - I've always called it cascading selection, but not sure if a Google search on that will result in anything as I may be the only person to refer to this setup that way...) you'll want to look at AJAX. Which may actually be easier as you don't have to store each individual selection between page loads because the page is only going to load once. So you just need to read the values of the form once the user fills out and submits the entire form. Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 15, 2014 Share Posted July 15, 2014 First you write a simple script that takes the category as a url parameter. Something like this: //setcategory.php <?php session_start(); if (isset($_GET['category']) { $_SESSION['category'] = $_GET['category']; } Then in your client page use a bit of jquery to make an ajax call when someone clicks on the relevant item. I'm not going to go into that, because it's a javascript question, and nothing to do with your initial question. Suffice it to say it's up to you to figure out the javascript for how to get the clicked or selected item and pass it through to the ajax call. I made a jsfiddle to give you a leg up on the ajax and javascript: http://jsfiddle.net/VL727/ Something along these lines: <select id="categories" name="categories"> <option value="" selected="selected">Select a Category</option> <option value="red">Red</option> <option value="black">Black</option> <option value="yellow">Yellow</option> </select> jquery document ready code $(function() { $("#categories").change(function() { var c = $('#categories option:selected').val(); //alert('Value:' + c); $.get('setcategory.php', { category: c }); }); }); The specifics of getting events and selecting your popup menu are going to depend on the nature of the menu. If you can't figure it out, make a separate question with your client markup and javascript in the javascript forum. Obviously jsfiddle is a good way to test out things. Quote Link to comment Share on other sites More sharing options...
Solution Aizen06 Posted July 17, 2014 Author Solution Share Posted July 17, 2014 I started learning java script last night, and I've figured it out. Here's the solution to what I was trying to achieve. http://liveweave.com/RGpVcU The select solution does work, but I needed something that I can add sub items to, and since this is something that will be on the nav bar, I just wanted to give it a clean look as oppose to creating more select drop down list after selecting an item. Thanks for all the help guys, I really appreciate it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.