PHP onpageload ... well sort of

I’ve seen people asking how to do an “onpageload” with php and there isn’t a direct way like what jscript has:

[php]onload=“MyJScriptFunction();”[/php]

however, my example uses a checkbox and here’s how you can do it:
[php]
if(isset($_POST[“submit”])) // Button submit won’t be set on the first load of the page
{
if(isset($_POST[“checkbox”])) // since submit is set lets check to see if they selected a checkbox
{
$checkbox = “Y”; // the checkbox is checked
}
else
{
$checkbox = “”; // nothing checked so set to nothing
}
}
else // since submit wasn’t set let’s use a cookie or stored value when entering the page
{
$checkbox = $sess->checkbox; // You can use a cookie value here or some sort of default value;
}

// save the state of the checkbox
$sess->SetSessionVariable(“checkbox”, $checkbox);
[/php]
I had a need where I had to store the checkbox state when the client navigated off the site and came back. This works for that as well as radio buttons and defaulting variables.

That could had easily been done with setting a sessions variable - IMO

Since PHP is server side the need for onpageloads are rare and far between, and like I stated can easily been assign a sessions variable. The only place I ever had to use them that I can recall is a html form when the user has enter something wrong in a field. Even then it could had been just as simple to pass the validated variables back to the form.

I guess I wasn’t clear. This is how to retain what the client side has already typed into the form and when “they leave the page and then come back” you can populate the fields with what they had typed when the page loads. I do use a session handler to retain those values so when its the first time in/back it uses what is in the session handler. Look at it a little closer as it does have value. Also, with my own session handler I don’t have to rely on cookie settings. I’d like to see your code for saving a checkbox state when the client navigates off the page and then returns expecting their selection to be there.

I'd like to see your code for saving a checkbox state when the client navigates off the page and then returns expecting their selection to be there.

Here is the right way. To be honest, your code is a cluster F#$%^! (Translation: a disastrously mishandled situation or undertaking.)

[php]<?php
session_start();
if(isset($_POST[‘box’]))
{
$_SESSION[‘box’] = $_POST[‘box’];
}
?>

> [/php]

“your code is a cluster F#$%^!” Excuse me?

session_start() at the top produced an error on the server I am using and I had to make a session handler for my application.

If you uncheck the box then the isset wont do anything with your session variable and the box will stay checked. Oh and see if yours does what you want when you leave the page and then navigate back to it. I don’t see how you are going to be able to retain it being checked.

var_dump($_POST); on first page and it’s empty.

Mine still has value and I see no reason to contribute any more to this post.

session_start() at the top produced an error on the server I am using.
Then you better find out whats wrong with your server. That is EXACTLY where session start should be.
If you uncheck the box then the isset wont do anything with your session variable and the box will stay checked.
Its not production code. It is an [u]EXACT[/u] answer to your specific question to save the checked box. It is a minor issue to unset it.
Oh and see if yours does what you want when you leave the page and then navigate back to it.

It sure does just like it is supposed to. Since you are having some issue with session start would explain why it is not working for you.

The only thing you can contribute at this point is why your server cant handle session start the way it is supposed to. "Contributing’ bad code and not being able to admit its no good doesnt help anyone, including yourself.

Then you better find out whats wrong with your server. Not my job.

Its not production code. Mine is.

Since you are having some issue with session start would explain why it is not working for you. Mine works.

And I’m not asking a question as I am demonstrating how you can perform an onpageload with php by testing the $_POST variables to see if they have been set.

Not your job to know why perfectly good code wont work on your server? You obviously have no desire to learn and improve your skills. Keep using your junk code.

Good luck with your coding “career”.

And I'm not asking a question as I am demonstrating how you can perform an onpageload with php by testing the $_POST variables to see if they have been set.

You are posting bad information and got called out on it and cant handle it. If you cant see what I posted is clearly the right and better way to do it, you are going to have a hard road coding.

*Attn noobs, my code was not a full response to the OP’s posted “Solution”, just an answer to part of it.

You are posting bad information and got called out on it and cant handle it.

It’s not information but working code. I still haven’t seen your working version of saving the state of a checkbox or radio button.

And correct me if I am wrong but this part of the forum is for showing working code for a given situation. Mine happened to be the way to do an on page load with php.

Is there any way Kevin can stop ranting about it?

You were provided a working version of the part that saves the checkbox state. You just refuse to find out why session start is not working for you. I am not going to do a full rewrite of what you did.

Ok, [member=76322]oldSchool[/member],

Here is the complete, CORRECT replacement for your code.

[php]<?php
session_start();

if ($_POST)
{
if (isset($_POST[‘box’]))
{
$_SESSION[‘box’] = true;
}
if (!isset($_POST[‘box’]))
{
$_SESSION[‘box’] = false;
}
}
?>

> [/php]

if ($_POST) this will be null so you won’t know what the checkbox state was when you navigate off the page and then return so you need to else that if ($_POST) with your saved session value.

[php] <?php
session_start();

if ($_POST)
{
  if (isset($_POST['box']))
  {
     $_SESSION['box'] = true;
  }
  if (!isset($_POST['box']))
  {
     $_SESSION['box'] = false;
  }
}
else
{
   // You need to do something here for when they navigate back to the page
   $_SESSION['box'] = <what?>;
}

[/php]
Something is looking oddly familiar here!

Here is a better improvement:

[php]<?php
session_start();

if ($_POST)
{
$_SESSION[‘box’]= isset($_POST[‘box’]) ? true:false;
}
?>

> [/php]

You still need an else for if ($_POST) because that’s where the code will go when the page is loaded for the first time, hence a php on page load.

Now stop arguing with me.

Trying to teach you something. No, you do not need the else. Just test the last code I gave. If it is wrong, show me it’s wrong.

Show me how the rest of the page will not show without an else.
Show me whatever it is you were saying about null post.

I wouldn’t classify this as arguing, it is debating, which happens with programming and is a good way to learn something new or understand why you are doing something the way you are.

As to your issue with session_start(), it generally does go at the top of the page using it, but at a minimum before any output. From the docs:

[b] Note:[/b]
To use cookie-based sessions, session_start() must be called before outputing anything to the browser.</blockquote>

Now, PHP does not need an “onload” function like JavaScript. JavaScript uses it to ensure that the DOM is populated before you start messing with anything that may require it. PHP does its thing before the browser gets it. So, the entire script is effectively an onload event.

The code could handled a number of ways. The more possibilities added ( checking post, get, or session values) makes all the more references to check, complicating very simplistic code.

Now, you two boys be good and go outside and play ball for awhile!

Man! Just because someone does not understand your logic, it is no reason to hammer on them. Please
remember this is a PHP “help” site.

Now, I think Kevin lost his patience because oldSchool does not understand what was explained. First, both
astonecipher and Kevin are correct. You need to use the session_start(); command if you plan to access the
PHP/browser session. You must NOT run that code twice in a row. You might have done that by placing it in
an included file. If you are receiving errors on that line, you need to solve that before going any further.
Next, you normally check if a submit button was pressed by if(isset($_POST[‘button’])), but it does still work
without the “isset” function. All items passed to different pages thru the session is normally set and read
using the $_SESSION[‘some-variable-name’] which is an array and part of the PHP global system variables.
The code that Kevin showed you: $_SESSION[‘box’]= isset($_POST[‘box’]) ? true:false; is two parts in one
line and does not need an ELSE section as it is already there. That is what the “?” is for. Finally, when you
create a checkbox, you normally just need to know if it is checked or not. That value can be saved in any
variable or to pass on to another page a $_SESSION variable. But, it is just a value and you need to check
it as Kevin showed you to display it: <?= !empty($_SESSION['box']) ? 'checked="checked"' : '' ?> If the
value is not null, it displays the needed "checked=‘checked’ " so the HTML can make it appear as checked.

Hope that helps you oldSchool understand that everyone was helping you not arguing with you.

<?php session_start(); if (isset($_POST['box'])){ $_SESSION['box'] = true; }else{ $_SESSION['box'] = false; } ?> >

[php]

<?php session_start(); $_SESSION['box'] = (isset($_POST['box']) ? true : (isset($_SESSION['box']) ? $_SESSION['box'] : false)); [/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service