treehouse : what would you like to learn today?
Web Design Web Development iOS Development

php session with checkboxes

  • so i am making checkboxes like this.

    <input type="checkbox" id="lawn" name="checkbox[]" value="Weekly Lawn Maintenance" checked="<?php echo (isset($checked['Weekly Lawn Maintenance'])) ? 'checked' : ' ' ?>"/>


    and then on a process php page i did this:

    $checkbox = $_POST['checkbox'];



    i put $checkbox into a session variable and then i set it to $checked back on the contact page and it won't work?

    help

    i echo'd $checkbox[1] an it gave me 'array'

  • Why did you call this input 'checkbox[]'? Why not 'checkbox[Weekly Lawn Maintenance]'?

    Secondly, better way of checking input is:
    <?php
    if (isset($checked[...]) && $checked[......])
    echo 'checked="checked"';
    ?>
    What is behind '$_SESSION' ?
  • I have done it like this before without any problems. How many checkboxes do you have?
  • i created an array called checkbook[].
    i read that thats how its usually done.

    i have 9 or 10 checkboxes


    after i set
    $checkbox = $_POST['checkbox'];


    i send it back via $_session['returnData'] which held an array if things (name,email, etc...) and one of those things was an array of checkboxes.


    and even if i try
    (isset($checked['1']))


    i don't work
    its always true and the checkmark is always there
  • I'm a little confused where you're getting $checked from as it's not part of your form....


    <?php
    foreach ( $_POST['checkbox'] as $checkbox => $value )
    {
    // Do what you need to do in here....
    }



    If you only have one checkbox on the page though, DON'T name it an array.
  • I have never used $_session, I was using the $_POST and getting it on submit. How are you submitting the form?

    have you tried:



    $check = $checkbox[1];
    echo $check[1];




    Then see what is in that array.
  • $checked was avariable i was using before but i got rid of it and tried making it a bit simpler but i can't get it work!?

    ok
    this is what i have on my process page after you press submit:
    //form data
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $checkbox = $_POST['checkbox'];
    $message = $_POST['message'];



    later on the process page i have this to send it back. (i have never had the php send it to itself I have always had it send to a process page. i never tried doing it the other way... instead I sometimes have ajax do the whole thing and send it to and from the process page)


    $returndata = array(
    'posted_form_data' => array(
    'name' => $name,
    'email' => $email,
    'phone' => $phone,
    'message' => $message,
    'checkbox' => $checkbox), other stuff is here not in the array );


    then i send this array back using a session

    $_SESSION['cf_returndata'] = $returndata;


    on the contact page I have $cf = $_SESSION['cf_returndata'].


    so now it should be in $checkbox but when i then have this in the input of checkbox:

     <input type="checkbox" id="lawn" name="checkbox['Weekly Lawn Maintenance']" value="Weekly Lawn Maintenance" checked="<?php echo ($cf['posted_form_data']['checkbox']['Weekly Lawn Maintenance']) ? 'checked' : ' ' ?>"/>



    i check echo $cf['posted_form_data']['checkbox'] agev back array.
    and echo $cf['posted_form_data']['checkbox'][1] gave back an empty spot.



  • var_dump( $cf['posted_form_data']['checkbox'] ) ;


    Put this in your page and copy into the thread what it dumps out
  • lol

    NULL
  • Try


    <input type="checkbox" id="lawn" name="checkbox['Weekly Lawn Maintenance']" value="Weekly Lawn Maintenance" checked="<?php echo ($cf['posted_form_data']['checkbox'][0]) ? 'checked' : ' ' ?>"/>
  • @shamai A checkbox is considered "checked" if it has the attribute 'checked' independently of the value of the attribute. that means that
    <input type="checkbox" checked/>
    is consider checked, so you should do it like this
    <input type="checkbox" id="lawn" name="checkbox['Weekly Lawn Maintenance']" value="Weekly Lawn Maintenance" <?php echo ($cf['posted_form_data']['checkbox'][0]) ? 'checked="checked"' : ' ' ?>/>
  • How can it return NULL when you said that when you echo $cf['posted_form_data']['checkbox'] it returns Array?!

  • i don't know!?
    its like it lost all its contents?