hahaitwork Posted October 30, 2012 Author Share Posted October 30, 2012 http://forums.phpfre...e-posting-them/ I have read your link, I understood some but not everything.. After trying again I get this error from this line: ("location: cartindex.php?list=$list"); [b]Notice[/b]: Undefined variable: list in [b]C:\xampp\htdocs\xampp\cartindex.php[/b] on line [b]6[/b] I also got this code later in the body-> php script $list = (isset($_GET['list'])) ? $_GET['list'] : 'products'; is this because the header is being readed before the rest ? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 30, 2012 Share Posted October 30, 2012 Headers needs to be sent before any other output, yes. Once you've sent something to the browser, the web server will send all of its headers, and the browser will stop accepting new ones. Same as you can't decide to get into another plane, once you're airborne. Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 30, 2012 Author Share Posted October 30, 2012 Headers needs to be sent before any other output, yes. Once you've sent something to the browser, the web server will send all of its headers, and the browser will stop accepting new ones. Same as you can't decide to get into another plane, once you're airborne. ahh, I see.. but then again, what is the way to fix this problem then ? =o like.. set var $list in header ? Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted October 30, 2012 Share Posted October 30, 2012 quick fix would be to use sessions instead of get. Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 30, 2012 Author Share Posted October 30, 2012 quick fix would be to use sessions instead of get. session_start (); is the only Session line I got in my script. what is the differens if I make it into a session and not a "get" ? Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted October 30, 2012 Share Posted October 30, 2012 Session variables can be assigned without the need to output anything to the html - so before the headers get finalised. Thus you can assign the $list to $_SESSION['list'] and then push the header:location to a new page where you will have access to the $_SESSION['list'] variable. you will need session_start(); at the top of every page that is used. It also makes the url's nicer (and more spider friendly - unless you changed your .htaccess file already) and removes visability of data from the end user Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 30, 2012 Author Share Posted October 30, 2012 Session variables can be assigned without the need to output anything to the html - so before the headers get finalised. Thus you can assign the $list to $_SESSION['list'] and then push the header:location to a new page where you will have access to the $_SESSION['list'] variable. you will need session_start(); at the top of every page that is used. It also makes the url's nicer (and more spider friendly - unless you changed your .htaccess file already) and removes visability of data from the end user hmm, I tried to change $list = (isset($_GET['list'])) ? $_GET['list'] : 'products'; Into session etc without much luck There will also be a error if I start a session in this file because it says: it has already started a session from another file and this will be ignored Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 30, 2012 Share Posted October 30, 2012 Muddy_Funster: Sessions will not make an iota of difference if the headers are already sent. If anything, it can create more problems as session_start () relies upon being able to send headers. And then we haven't even begun discussing the question of how to get the user's input into the session, in which case you'll need to use $_GET. Sessions are not a replacement for GET or POST, but a way to keep certain data available on the server across multiple visits, without having to send them to the browser. hahaitwork: I recommend that you disregard the advice given by Muddy about using sessions, and continue with what you had. As for your question about how to solve the "headers already sent" problem: If you're getting it because Wordpress has sent output before interpreting your code, then it's very little you can do. At least without modifying wordpress itself. The easiest solution might be to use a handler page, like you've done before, but outside of wordpress' control. Then have it redirect back to the wordpress form page, in case of errors or such. You can use the session array to communicate between them. However, don't worry about this until you've made certain it's wordpress itself creating issues for you, and not your own code. Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 30, 2012 Author Share Posted October 30, 2012 Muddy_Funster: Sessions will not make an iota of difference if the headers are already sent. If anything, it can create more problems as session_start () relies upon being able to send headers. And then we haven't even begun discussing the question of how to get the user's input into the session, in which case you'll need to use $_GET. Sessions are not a replacement for GET or POST, but a way to keep certain data available on the server across multiple visits, without having to send them to the browser. hahaitwork: I recommend that you disregard the advice given by Muddy about using sessions, and continue with what you had. As for your question about how to solve the "headers already sent" problem: If you're getting it because Wordpress has sent output before interpreting your code, then it's very little you can do. At least without modifying wordpress itself. The easiest solution might be to use a handler page, like you've done before, but outside of wordpress' control. Then have it redirect back to the wordpress form page, in case of errors or such. You can use the session array to communicate between them. However, don't worry about this until you've made certain it's wordpress itself creating issues for you, and not your own code. Uhm, I do not use wordpress ? I use XAMPP, apache (localhost) etc and write the script in notepad++ Quote Link to comment Share on other sites More sharing options...
Barand Posted October 30, 2012 Share Posted October 30, 2012 (edited) If you have decided to use the session option replace $list = (isset($_GET['list'])) ? $_GET['list'] : 'products'; with if (isset($_GET['list'])) { $list = $_GET['list']; // if change of list (another menu button clicked), use that } elseif (isset($_SESSION['list'])) { $list = $_SESSION['list']; // otherwise use current list } else { $list = 'products'; // if neither use default } $_SESSION['list'] = $list; // save current list Edited October 30, 2012 by Barand Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted October 30, 2012 Share Posted October 30, 2012 that's just a notice, not an error. you can suppress notices by puting a @ symbol at the beginning of the line - it's not a habit I encourage though, if you are getting that notice, and that page isn't being included/required by any other pages feel free to drop the session_start() from this one. I don't see what problems you are inferring there Christian, as the session variables can be assigned before the header is sent, and carried through with the header it seems like a logical solition rather than an aditional problem. I am well aware of the purpose of the session array and it's general usage, and for what is being described it does the job. I also really don't understand why you would say that my advice for using sessions should be disregarded and then go on you give alternate advice that is in fact to use sessions... Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 30, 2012 Share Posted October 30, 2012 Hmm.. I think I might have confused a couple of threads here, if so then I'm sorry about that. Think I might take a few minutes away from the computer now. Sessions can indeed be used for default listing, as Barand gave an example of in his post. hahaitwork: You get that error message because you've already started a session, so no need to start another one. Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 30, 2012 Author Share Posted October 30, 2012 If you have decided to use the session option replace $list = (isset($_GET['list'])) ? $_GET['list'] : 'products'; with <?php if (isset($_GET['list'])) { $list = $_GET['list']; // if change of list (another menu button clicked), use that } elseif (isset($_SESSION['list'])) { $list = $_SESSION['list']; // otherwise use current list } else { $list = 'products'; // if neither use default } $_SESSION['list'] = $list // save current list ?> I tried it, the script would be like this: And the error would be: Parse error: syntax error, unexpected 'switch' (T_SWITCH) in C:\xampp\htdocs\xampp\cartindex.php on line 41 <?php echo "<button id='products'> Servers </button> "; echo "<button id='others'> ? </button>"; echo "<button id='test'> ? </button><br />\n"; if (isset($_GET['list'])) { $list = $_GET['list']; } elseif (isset($_SESSION['list'])) { $list = $_SESSION['list']; } else { $list = 'products'; } $_SESSION['list'] = $list switch ($list) { case 'others': others(); break; case 'products': products(); break; case 'test': test(); break; } ?> <script type="text/javascript"> if (document.getElementById('products')) document.getElementById('products').onclick = function() {location.href='?list=products';} if (document.getElementById('others')) document.getElementById('others').onclick = function() {location.href='?list=others';} if (document.getElementById('test')) document.getElementById('test').onclick = function() {location.href='?list=test';} </script> Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted October 30, 2012 Share Posted October 30, 2012 y'all missed a semi-colon at the end of the $_SESSION['list'] = $list line Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 30, 2012 Author Share Posted October 30, 2012 y'all missed a semi-colon at the end of the $_SESSION['list'] = $list line Worked Thanks for the head up, and thanks again Barand. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted October 30, 2012 Share Posted October 30, 2012 What about me???? sessions were my idea...... hehe j/k glad you get a solution Quote Link to comment Share on other sites More sharing options...
Barand Posted October 30, 2012 Share Posted October 30, 2012 What about me???? sessions were my idea...... hehe j/k glad you get a solution See my option 2 in reply #21 Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted October 30, 2012 Share Posted October 30, 2012 ...I did say I was just kidding..... Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 30, 2012 Author Share Posted October 30, 2012 What about me???? sessions were my idea...... hehe j/k glad you get a solution Of course I'm glad for all the help I could get! Thanks Muddy_Funster! Was starving and could only think about food, so was to lazy to thank everyone that helped The reason why I had to thank Barand is because he have helped me a lot , not only with this problem but also other problems! 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.