Jump to content

Load/unload Function


hahaitwork

Recommended Posts

 

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 ?

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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++

Link to comment
Share on other sites

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 by Barand
Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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. :P

 

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

What about me???? sessions were my idea......

 

:suicide:

 

 

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.