SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to explode $_POST values

    Hi.I want to do something like following code, but the check boxes appearing from given code are not checked after form submission.Here is my code:
    PHP Code:
    $arr $_POST['cust_occup'];
    $cust_occup_array explode(",",$arr);

    $chkAr = array("Employed""Self""Business""Professional""House""Others");

            for(
    $i=0$i count($chkAr); $i++){
                echo 
    '<input type="checkbox" name="cust_occup[]" id="cust_occup" value="'.$chkAr[$i].'"'
                    foreach(
    $cust_occup_array as $arry){
                        if(
    $chkAr[$i]==$arry){ echo 'checked="checked"'; }
                        else{ echo 
    ''; } 
                    }
                echo 
    ' > '.$chkAr[$i].'&nbsp;';
            } 
    Can anyone tell me how to explode $_POST string values to compare with an array?Any guidance will be very helpful for me.
    It's easy once you know how...

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    This is the basics:
    PHP Code:
    $chkAr = array("Employed""Self""Business""Professional""House""Others"); 

    //spoofing your POST vars
    $_POST['cust_occup']["Employed"] = 'on' ;
    $_POST['cust_occup']["Self"] = 'on' ;

    foreach( 
    $chkAr as $item){
        if( 
    array_key_exists($item$_POST['cust_occup']))
            echo 
    'selected = selected';

    You can also make your chkAr work as a security white list too:
    PHP Code:
    $chkAr = array("Employed""Self""Business""Professional""House""Others"); 

    $_POST['cust_occup']["Employed"] = 'on' ;
    $_POST['cust_occup']["Self"] = 'on' ;

    // uncomment this row to see the script die
    //$_POST['cust_occup']["Nasty_HACK"] = 'on' ;

    $posted_keys array_keys($_POST['cust_occup']);

    if( 
    array_diff($posted_keys$chkAr)){
     exit(
    'Someone has been tampering with your values');
    }

    foreach( 
    $chkAr as $item){
      if( 
    in_array($item$posted_keys)){
            echo 
    $item' selected = selected' PHP_EOL;
        }else{
           echo 
    $item PHP_EOL;
        }

    ps UNTESTED with real POST values btw ...

  3. #3
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply Cups. I tried to implement your code in mine. But things dont seem work.May be I am going wrong somewhere.
    Here is my new code:
    PHP Code:
    <?php         
    $chkAr 
    = array("Employed""Self""Business""Professional""House""Others");
    $_POST['cust_occup']["Employed"] = 'on' 
    $_POST['cust_occup']["Self"] = 'on' 
    $_POST['cust_occup']["Business"] = 'on' 
    $_POST['cust_occup']["Professional"] = 'on' 
    $_POST['cust_occup']["House"] = 'on' 
    $_POST['cust_occup']["Others"] = 'on' 

    for(
    $i=0$i count($chkAr); $i++){
    echo 
    '<input type="checkbox" name="cust_occup[]" id="cust_occup" value="'.$chkAr[$i].'"';
    if( 
    array_key_exists($chkAr[$i], $_POST['cust_occup'])) 
            echo 
    'selected = selected'
    echo 
    '> '.$chkAr[$i].'&nbsp;';
    }

    ?>
    It's easy once you know how...

  4. #4
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    anita_86, checkboxes need checked="checked"
    http://reference.sitepoint.com/html/input/checked

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    foreach( $chkAr as $item){
    echo 
    '<input type=checkbox name=cust_occup['.$item.'] id=cust_occup value="' .$item .'" ' ;
     if( 
    in_array($item$posted_keys) )  echo  " checked = 'checked'";
    echo 
    ' />' $item PHP_EOL ;
    }
    // gives me

    <input type=checkbox name=cust_occup[Employedid=cust_occup value="Employed"  checked 'checked' />Employed
    <input type=checkbox name=cust_occup[Selfid=cust_occup value="Self"  checked 'checked' />Self
    <input type=checkbox name=cust_occup[Businessid=cust_occup value="Business"  />Business
    <input type=checkbox name=cust_occup[Professionalid=cust_occup value="Professional"  />Professional
    <input type=checkbox name=cust_occup[Houseid=cust_occup value="House"  />House
    <input type=checkbox name=cust_occup[Othersid=cust_occup value="Others"  />Others 
    Correction: as pointed out by @centered effect ; SB "checked"

  6. #6
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your valuable reply centered effect and Cups.I will do these changes and see if it works
    It's easy once you know how...


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
  •