SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Get value from checkbox and echo it

    hi all

    How can i get value from checkbox and echo that value

    i have this below checkbox code for all my products in the row
    PHP Code:
    <?
    echo  "<input name='availability[]' type='checkbox' onclick='this.form.submit();' 

    value='savl=Store&pidd=310' id='availability[]' />Store"

    ?>
    Onclick the form gets submit but i dont get its value;

    PHP Code:
    <?
    if(isset($_REQUEST['availability']))
    {
        
    $savl=$_REQUEST['savl'];
        
    $pidd=$_REQUEST['pidd'];
        
        echo 
    $savl/* no value is echo */
        
    echo $pidd/* no value is echo */
    }    
    ?>
    am i doing something wrong

    vineet

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    I would use $_GET instead of $_REQUEST (to be sure you're using the correct values).

    To know why your code isn't working, do a print_r of $_REQUEST and see what it contains.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi guido

    after doing print_r also i dont get any values for $savl and $pidd
    Code:
    Array
    (
        [dealer_id] => 9
        [category_id] => 1
        [sub_catg] => 6
        [availability] => Array
            (
                [0] => savl=Store&pidd=340
            )
    
        [PHPSESSID] => 2622124c72fa9bd4507a38e0a558dac0
    )
    Is there any other method of passing values to checkbox and then use them

    vineet

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The values are been past as you can see in your print_r statement, the reason why they are not part of the $_GET superglobal is because they are a value of the checkbox and not of the form action. You can use something simple like the below example to access the values.

    PHP Code:
    if (isset($_GET['availability']) && sizeof($_GET['availability'])) {
        foreach (
    $_GET['availability'] as $availability) {
            
    $split explode('&'$availability);

            foreach (
    $split as $v) {
                
    $temp explode('='$v);

                echo 
    'Key: ' $temp[0] . '<br>';
                echo 
    'Value: ' $temp[1] . '<br><br>';
            }
        }


  5. #5
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thanks chris

    your solution works great

    vineet

    Quote Originally Posted by chris.upjohn View Post
    The values are been past as you can see in your print_r statement, the reason why they are not part of the $_GET superglobal is because they are a value of the checkbox and not of the form action. You can use something simple like the below example to access the values.

    PHP Code:
    if (isset($_GET['availability']) && sizeof($_GET['availability'])) {
        foreach (
    $_GET['availability'] as $availability) {
            
    $split explode('&'$availability);

            foreach (
    $split as $v) {
                
    $temp explode('='$v);

                echo 
    'Key: ' $temp[0] . '<br>';
                echo 
    'Value: ' $temp[1] . '<br><br>';
            }
        }



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •