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.

1
  • 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. Commented Jul 10, 2018 at 7:05

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

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
use the last snippet in my answer and you should be set
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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.