title: Posting form data to mysql database with ajax and php
body: <p>My index.php:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;form name="form1" action="submit.php" method='POST'&gt;
  &lt;select id="dropdown1" name="country" onchange="window.getStates()"&gt;
  &lt;option&gt; Select Country&lt;/option&gt;
  &lt;option value="1"&gt;Pakistan&lt;/option&gt;
  &lt;option value="2"&gt;India&lt;/option&gt;
  &lt;option value="3"&gt;USA&lt;/option&gt;
  &lt;option value="4"&gt;UK&lt;/option&gt;
  &lt;/select&gt;
  &lt;input type="text" id="area" style="display: none;" size="16" placeholder=" Enter value"&gt;&lt;/input&gt;
  &lt;input type="submit" id="submit" style="display: none" name="submit" value="submit" onclick="submit()"&gt;
  &lt;/form&gt;
  &lt;script type="text/javascript"&gt;
  function show() {
  { document.getElementById('area').style.display = 'inline-block';
    document.getElementById('submit').style.display = 'inline-block';}
  }
  function getStates()
  {
  var xmlhttp;
  try{
      xmlhttp = new XMLHttpRequest;
  }catch(e)
  {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (xmlhttp)
  {
      var form = document['form1'];
      var country = form['country'].value;
  
      xmlhttp.open("GET","http://localhost/getStates.php?country="+country,true);
      xmlhttp.onreadystatechange = function()
      {
          if(this.readyState == 4)
          {
              var s = document.createElement("select");
              s.onchange=show;
              s.id="dropdown2";
              s.name="state";
              s.innerHTML = this.responseText;
  
              if(form['state'])
              {
                  form.replaceChild(s, form['state']);
              }
              else
                  form.insertBefore(s,form['submit']);
          }
      }
      xmlhttp.send(null);
  }
  }
  
  function submit() {
      var table = document.getElementById("dropdown1").value;
      var parameter = document.getElementById("dropdown2").value;
      var value = document.getElementById("area").value;
      $.ajaxSetup({
             url: "http://localhost/database.php",
           type: "POST",
      });
       $.ajax({
          data: 'table='+table+'&amp;parameter='+parameter+'&amp;value='+value+,       
          success: function (msg) {
          alert (msg);},
          error: function (XMLHttpRequest, textStatus, errorThrown)
          {   
          alert('Error submitting request.'); 
          }
  
          }); 
  }
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>my getStates.php code:</p>
  
  <pre><code>&lt;?php
  
  $states=array(
  "1" =&gt; array("NWFP","Sindh","Bala","Punjab","Select"),
  "2" =&gt; array("gujrat","goa","U.P.","Select"),
  "3" =&gt; array("bgjs","hhtrs","Bhtrshts","Utah","Select"),
  "4" =&gt; array("England","Scotland","Bahwgla","Punthwthjab","Select")
  );
  
  if(isset($_GET['country']))
  {
  $c = $_GET['country'];
  if(isset($states[$c]))
  {
      for($i = count($states[$c]) -1; $i&gt;=0; $i--)
      {
          echo "&lt;option value='".$states[$c][$i]."'&gt;".$states[$c][$i]."&lt;/option&gt;";
      }
  }
  }
  
  ?&gt;
  </code></pre>
  
  <p>database.php code:</p>
  
  <pre><code>&lt;?php
  header('Content-type: text/html; charset=ISO-8859-1');
  try
  {
  if(isset($_POST['table']) &amp;&amp; isset($_POST['parameter']) &amp;&amp; isset($_POST['value'])){
  $table = ($_POST['table']);
  $parameter = ($_POST['parameter']);
  $value = ($_POST['value']);
  $db = mysql_connect(localhost, root, "");
  $select = mysql_select_db(records, $db);
  $query="INSERT INTO $_POST['table'] (Parameter,Value)
         VALUES ('".$_POST['parameter']."','".$_POST['value']."');";
  }
  mysql_query($query,$connection);}
  catch(Exception $e)
  {
  echo 'Erreur : '.$e-&gt;getMessage().'&lt;br /&gt;';
  echo 'N° : '.$e-&gt;getCode();
  }
  ?&gt;
  </code></pre>
  
  <p>Also, the submit button has a onclick() and an action tag. When the submit button is clicked, i want the submit() function to be executed, so what should i do for that? When i press submit, the parameter and value values are not being inputted into my database called records with 4 tables named 1,2,3 and 4. Thanks! </p>
  
  <p>I think there is some probllem with this line:</p>
  
  <pre><code>$query="INSERT INTO $_POST['table'] (Parameter,Value)
         VALUES ('".$_POST['parameter']."','".$_POST['value']."');";
  </code></pre>