0

How can I get an array like this

index.php?jenis=11,12,13

or like this without remove the previous value?

index.php?jenis=11&jenis=12&jenis=13

In my code wherever I do a click or an action it just get the next $_GET value but not keep the previous $_GET value. The question is how I can keep both previous $_GET and next $_GET into one?

Here is my code:

HTML:

<form id="filter">
<label>Pertama </label>
<ul>
<li><input type="checkbox" onclick="jeniss();" name="jenis" value="11" <?php if ($jenis == "11")  { echo "checked"; } ?> > <a> 11 </a> </li>
<li><input type="checkbox" onclick="jeniss();" name="jenis" value="12" <?php if ($jenis == "12")  { echo "checked"; } ?> > <a> 12 </a> </li>
<li><input type="checkbox" onclick="jeniss();" name="jenis" value="13" <?php if ($jenis == "13")  { echo "checked"; } ?> > <a> 13 </a> </li>
</ul>
</form>

PHP:

if (!isset($_GET['jenis'])) {
    $jenis =  "";       
} else {
    $jenis = $_GET['jenis'];                            
}

Javascript:

<script>
        function jeniss(){
        document.getElementById('filter').submit(); 
        }           
</script>

Note : I'm using javascript so every action (check or uncheck) will redirect into new page which is my hope result. Thank you for helping.

Davit Zeynalyan
  • 8,418
  • 5
  • 30
  • 55
newbie41
  • 45
  • 1
  • 5
  • Typing “php get array” into Google was all it took to find this duplicate right away. Please make more of an actual research effort next time. – CBroe Jul 10 '18 at 07:05

2 Answers2

2

Name your inputs with [] to append multiple values

name="jenis[]"

then you should be able to acces the array using a loop

foreach($_GET['jenis'] as $jenis) {echo $jenis}

or in your case use the in_array() function

<li><input type="checkbox" onclick="jeniss();" name="jenis[]" value="11" <?php if (in_array("11",$_POST['jenis'])  { echo "checked"; } ?> > <a> 11 </a> </li>
<li><input type="checkbox" onclick="jeniss();" name="jenis[]" value="12" <?php if (in_array("12",$_POST['jenis'])  { echo "checked"; } ?> > <a> 12 </a> </li>
<li><input type="checkbox" onclick="jeniss();" name="jenis[]" value="13" <?php if (in_array("13",$_POST['jenis'])  { echo "checked"; } ?> > <a> 13 </a> </li>
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
  • srry for disturbing. can u tell me which part of my php should change or add with the foreach ?. Or can u edit ur answer and add ur own php code, i'm just beginner and little confuse. Thx for helping – newbie41 Jul 10 '18 at 06:39
  • use the last snippet in my answer and you should be set – madalinivascu Jul 10 '18 at 06:43
1

You have to set your input's value as previous + actual value of your GET parameter.

<li><input type="checkbox" onclick="jeniss();" name="jenis" value="<?php echo $jenis.',11'?>" <?php if ($jenis == "11")  { echo "checked"; } ?> > <a> 11 </a> </li>
<li><input type="checkbox" onclick="jeniss();" name="jenis" value="<?php echo $jenis.',12'?>" <?php if ($jenis == "12")  { echo "checked"; } ?> > <a> 13 </a> </li>
<li><input type="checkbox" onclick="jeniss();" name="jenis" value="<?php echo $jenis.',13'?>" <?php if ($jenis == "13")  { echo "checked"; } ?> > <a> 12 </a> </li>

This will output as you first said in your post: ,11,12,13 (first comma is when $jenis is empty. You can ignore it when explodin array for commas)

ClaudiusDan
  • 243
  • 1
  • 12