title: Javascript onclick on submit button store input value in php array
body: <p>I have a select input with another submit input. i want to store selected value in a php array with  every click on submit input without page reload.
  on clicking on add every time it will store the value of select input in an array of php.
  on 1st click the array will have only one value then in 2nd click on add the php array will have 2 value.</p>
  
  <p>since I tried it with ajax, but when i clicking twice then only 2nd value is storing, 1st value is replacing by 2nd one. but i want both the values in the array.
  Here is my code:
  Html:</p>
  
  <pre><code>&lt;select id = "exam-list" name = "exam_name"&gt;
     &lt;option value="1"&gt;Opt 1&lt;/option&gt;
     &lt;option value="2"&gt;Opt 2&lt;/option&gt;
     &lt;option value="3"&gt;Opt 3&lt;/option&gt;
   &lt;/select&gt;
  
  &lt;input class="btn btn-primary" onclick="save_exam()" type="submit" name="submit" value="add"/&gt; 
  </code></pre>
  
  <p>Js Code:</p>
  
  <pre><code>function save_exam() {var val = document.getElementById('exam-list').value;$.ajax({type: "POST",url: "store_exam.php",datatype: "json",data: 'exam_id=' + val,success: function (data) { }});} 
  </code></pre>
  
  <p>PHP code:</p>
  
  <pre><code>&lt;?php $count = count($_POST['exam']);for ($i = 0; $i &lt; $count; $i++)     {print_r($_POST['exam']);}?&gt;
  </code></pre>