Rahul aman
Rahul aman

Reputation: 379

Get the value of many check boxes and then print them by foreach loop

Hey there I am having a form in a page where there are many check boxes in a form which are added dynamically.So I don't know the number look at the code below:

 <form action="" method="" style="margin:0px;" onsubmit="return sharedocxss();" id="share90_FORMh8">
<?php while($the_DEATA_ARE_90=mysqli_fetch_array($getDOCS_30all)){ ?>
<div class="This_LISy_Lisy678" id="MAINDIV_DELEE<?=$the_DEATA_ARE_90['dcid']?>">
<div class="CHeck_IS_BOC">
<input type="checkbox" name="selecteddocx[]" value="<?=$the_DEATA_ARE_90['dcid']?>x<?=$the_DEATA_ARE_90['name']?>" id="check_docname<?=$the_DEATA_ARE_90['dcid']?>"/>
</div>

There may be check boxes with name as 'selecteddocx' and value like '1222xsome text' on the next page. I want to get all the values of those text boxes and display like this some text,some text2,some text3 ......

some text,some text2,some text3 are the values which we can get by subtracting some characters till the first occurrence if x from the values of check box.

On second page I have code like

 $selecteddocx = (isset($_POST['selecteddocx']) ? $_POST['selecteddocx'] : '');

I think I can do it by using some for each loop

Upvotes: 1

Views: 198

Answers (2)

MazzCris
MazzCris

Reputation: 1840

if( isset($_POST['selecteddocx'] ) ){
    foreach($_POST['selecteddocx'] as $value){
        if (($pos = strstr($value, 'x')) !== false) {
            $value = substr($pos, 1);
        }
        echo $value;
    }
}    

Example to print as a comma separated list:

function preparetext($value){
    if (($pos = strstr($value, 'x')) !== false) {
        $value = substr($pos, 1);
    }
    return $value;
}

if( isset($_POST['selecteddocx'] ) ){
    echo implode(",", array_map("preparetext",$_POST['selecteddocx'] ));
} 

For two separate lists:

if( isset($_POST['selecteddocx'] ) ){
    $a = array();
    $b = array();

    foreach($_POST['selecteddocx'] as $value){
        $str = explode("x",$value,2);
        $a[] = $str[0];
        $b[] = $str[1];
    } 

    echo  implode(",", $a);
    echo  implode(",", $b);
}

Upvotes: 2

FuzzyTree
FuzzyTree

Reputation: 32402

To remove the 1st 'x' and everything that occurs before it use array_map to map the new values onto another array.

$values = array_map(function($v) {
    $pieces = explode('x',$v);
    unset($pieces[0]); //remove everything before 1st 'x'
    return implode('x',$pieces);
},$_POST['selecteddocx']);

To print a comma separated list you can use implode on your new array with a comma as the glue.

print implode(', ',$values);

Upvotes: 0

Related Questions