Question

This is my HTML Code

<input type="checkbox" value="fotoğraf" name="materyal[]"> Fotoğraf
<input type="checkbox" value="resim" name="materyal[]">Resim
<input type="checkbox" value="çizelge" name="materyal[]">Çizelge
<input type="checkbox" value="katlanabilir harita" name="materyal[]"> Katlanabilir Harita

There are checkbox and i'll take values with $_POST['materyal'] and then i want to write screen values with comma if values more than one. if checkbox values empty i don't write anything screen.

if (isset($_POST['materyal']) && !empty($_POST['materyal'])) {
        $materyal = $_POST['materyal'];
        echo "İçerdiği extra materyaller ; ";

        foreach ($materyal as $materyallist) {
            foreach ($materyallist as $yenimateryal){
                array_push($sonmateryal, $yenimateryal);
            }
        }
        echo implode(", ", $sonmateryal);
    }

This is my code. when i want to use implode in if contiditon, I take mistake. How can i do

Was it helpful?

Solution

It seems like you just want to show the contents?

$sonmateryal = array();
if (isset($_POST['materyal']) && !empty($_POST['materyal'])) {
    echo "İçerdiği extra materyaller ; ";
    foreach ($_POST['materyal'] as $materyallist) {
        foreach ($materyallist as $yenimateryal) {
            $sonmateryal[] = $yenimateryal;
        }
    }
}
echo implode(", ", $sonmateryal);

update according to given HTML

if (!empty($_POST['materyal'])) {
    echo "İçerdiği extra materyaller ; ".htmlentities(implode(',', $_POST['materyal']));
}

Since you're posting the inputs as simple array's this will implode the results with a comma if the post-ed value isn't empty..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top