How to assign a variable value to a CHECKBOX ?

Discussion in 'PHP' started by chiplonkar, Dec 10, 2006.

  1. #1
    I have a form generated by taking some selected records from the database.
    Against each record I have added a checkbox for user to select for deletion.

    If I can assign the serial number of the record to each checkbox, I will know which records have to be deleted. How do I add serial number , which is available in the database, to the following HTML line ?

    <INPUT TYPE="CHECKBOX" NAME="check[]" value=( here I want the serial number picked up from the database ) >

    Is there a standard simple method in PHP ?

    I have tried the following which does not work.

    <INPUT TYPE="CHECKBOX" NAME="check[]" value=<?php $_SESSION["xyz"]
    ?>>
     
    chiplonkar, Dec 10, 2006 IP
  2. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Assuming $_SESSION["xyz"] contains your serial number, use
    <?php echo $_SESSION["xyz"]; ?>
    PHP:
    echo displays whatever is in the varaible $_SESSION["xyz"].
     
    matthewrobertbell, Dec 10, 2006 IP
  3. chiplonkar

    chiplonkar Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you Mr.MathewRobertBell,

    that is perfectly okay. But my question was about assigning "value" to the checkbox in the Line, < INPUT TYPE=CHECKBOX NAME="delete" VALUE=?>

    In place of the ? I want a variable to be inserted as value to the check box so that I can read it in another php file. This value is supposed to come from a table in the database.

    Can you help ?
     
    chiplonkar, Dec 11, 2006 IP
  4. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #4
    Try:
    <INPUT TYPE="CHECKBOX" NAME="check[]" value="<?php echo $_SESSION["xyz"]; ?>">
     
    papa_face, Dec 11, 2006 IP
  5. kashem

    kashem Banned

    Messages:
    1,250
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #5
    <?php
    var $theVal;

    $theVal=1;
    echo "< INPUT TYPE=CHECKBOX NAME='delete' VALUE=$theVal>";


    ?>
     
    kashem, Dec 11, 2006 IP
  6. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You need to get the data from the database into a variable, then you can insert it into your html using the code i mentioned earlier.
     
    matthewrobertbell, Dec 11, 2006 IP
  7. chiplonkar

    chiplonkar Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I Thank you all for very quick and precise solution. Declaring value using echo ( which I was missing ) has worked like magic!

    Looking forward for further help on my journey to learning php !

    Chiplonkar
     
    chiplonkar, Dec 12, 2006 IP
  8. chiplonkar

    chiplonkar Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Your suggestion of inserting echo statement inside the value for assigning the value to any check box worked fine !. But, now I have another difficulty.

    The reading back of assigned values in another php page worked okay till the assigned values were less than "10" i.e. single digit. After the assigned value( which is a serial number of my record ) becomes more than " 9 ", the reading back gives me only first digit.

    The code is as under . These three lines are supposed to extract checkbox array value and show it on the screen. ( for diagnosis of fault )

    foreach($_POST["checkbox"] as $val){

    echo "value of selected box is =".$val;

    }

    Where is the mistake ?

    The checkbox is declared as an array checkbox[]. Therefore, I am trying to read all checked boxes by "foreach" statement.

    Thanks,
    Chiplonkar
     
    chiplonkar, Jan 4, 2007 IP