set a post variable without using post

if(isset($_POST['s10'])){
  $_SESSION['s10'] = $_POST['s10'];


this is for a checkbox that I check in an iframe

I want this checkbox to start off 'checked'

so can I write a command
set ($_POST['s10'])
($_POST['s10']) = 10
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark GilbertSenior Performance EngineerCommented:
$_POST['s10'] = 10;

I recommend against setting post variables if they are not being posted to the script because a few months down the line you or another developer will look at your code and be totally confused as to it's purpose. A normal variable should be able to hold the value for you. If at some point you are indeed posting a form variable then simply check for that and set the variable to the value:

if(isset($_POST['s10'])){
$myvar = $_POST['s10'];
} else{
$myvar = 10;
}

Hope this helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
foreverfreshCommented:
first put all checkbox value to 1
when you post form,you can set them "checked"

<input name="s10" type="checkbox" value="1" <?php if($_POST['s10']==1) echo "checked"; ?>>
...
...
<input name="s15" type="checkbox" value="1" <?php if($_POST['s15']==1) echo "checked"; ?>>
Ray PaseurCommented:
I agree with ingwa 100%

One of the major defects in PHP is that the superglobal arrays of external request data are mutable.  So you can just do this:

$_POST["s10"] = 'xyz';

The effect of doing that is obvious when you see it here - one line of code.  But the computer science students will understand that it violates all the common-sense rules of data encapsulation and external data validation.

As a practical matter, you might want to have a reflection array for $_POST and a filtering algorithm for each element of the POST array.  Your reflection array will contain the default values for any missing, empty or insane inputs.  For each element in the external POST array, you will apply your filters, and if the filters pass the external values, you will copy them into the reflection array.  You do this ONCE at the top of your script, and you never touch $_POST again.   A design pattern like this helps you to write code that is easy to understand, secure and maintainable.

As to checkboxes, you probably know that they are missing from the POST array if they are not checked.  Your reflection array and filter algorithm must take this into account.  If the default value for your checkbox is "checked" you just put this into the HTML.  In the PHP action script, you look for a value in that element of the POST array.  If the value is not set, your filter would place a NULL string into the reflection array.
rgb192Author Commented:
thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.