title: On Form submit, execute some PHP and then Javascript code
body: <p>This is what I am trying to do:
  When a form is submitted (by clicking a button) </p>
  
  <ol>
  <li>I want to insert some data to MySQL data using PHP</li>
  <li>Based on the success on the insert, I want to run some JavaScript(JS) code. (In reality some more data will be inserted from a JS array using AJAX but in the test code below, I am simply showing alert with the array). Also, I need the PHP to execute first because I need to the autoincremetn ID from MySQL via PHP, using the <code>mysqli_insert_id()</code> function.</li>
  <li>After all the data has been inserted (or both PHP and JS code has executed), I want to return to the same page which initiated the submit, in the test code this page is called <code>test.php</code>.</li>
  </ol>
  
  <p>Below is my test code (in <code>test.php</code> page):</p>
  
  <pre><code>&lt;?php
  
  // Require the configuration which controls controls error reporting:
  require ('config.inc.php');
  
  // Require the database connection:
  require (MYSQL);
  
  /* PAGE CONTENT STARTS HERE! */
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt; 
  &lt;meta charset="utf-8"&gt;
  &lt;!--JQUERY--&gt;
  &lt;script type="text/javascript" src="js/jquery-1.8.2.min.js" &gt;&lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
      var store_categories = []; 
      function init_filtering() {
          store_categories.push('data1');
          alert ("alert1: "+ store_categories);
      }
  &lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;$( init_filtering );&lt;/script&gt;
  
  &lt;/head&gt;  
  &lt;body&gt;
  
  &lt;?php
      if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $q = "INSERT INTO sms_category (sms_updated_id, category) VALUES (1, 'categories')";      
        $r = mysqli_query ($dbc, $q);
      }
  
  if (mysqli_affected_rows($dbc) == 1) {
  ?&gt;
      &lt;script type="text/javascript"&gt;
          alert ("alert2: "+ store_categories);
      &lt;/script&gt;
  &lt;?php
  } // end if
  ?&gt;
  
  &lt;form id="edit_data" action="test.php" method="post"&gt;
  &lt;input type="submit" value="Save"&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>What is happening right now:</p>
  
  <ol>
  <li>Alert1 is properly showing the data in the <code>store_categories</code> array</li>
  <li>But when I click the submit button, Alert2 is still showing blank (or, only "alert2:", i.e. cant show the value in the <code>store_categories</code> array).</li>
  </ol>
  
  <p>So, this is my question:</p>
  
  <ol>
  <li>Why is alert2 not showing the <code>store_categories</code> array?</li>
  <li>What can I do so that <code>store_categories</code> array has its value when I click submit button?</li>
  </ol>
  
  <p>Thanks in advance for any help.</p>