title: Insert PHP PDO via AJAX
body: <p>I'm hittin' the wall for some days already, and can't seem to resolve this simple problem.</p>
  
  <p>I want to make a simple insert via ajax. As i'm new to PHP, I've seen many examples, read the POST stuff for http and php, read te jquery.ajax documentation, and I'm aware of the PDO documentation aswell.
  Still, can't find why it's not working.</p>
  
  <p>Here goes the codes, really simple:</p>
  
  <p>Index</p>
  
  <pre><code>&lt;body&gt;
      &lt;form id="myForm" method="post" action="" &gt;
          Nome:
          &lt;input type="text" name="nome" required /&gt;
          &lt;br/&gt;
          Tipo:
          &lt;input type="text" name="tipo" required /&gt;
          &lt;br/&gt;
  
  
          &lt;input type="submit" name="submit" value="save" id="sub"/&gt; 
  
  
      &lt;/form&gt;
  
      &lt;script src="script/jquery-2.1.0.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script src="script/ajaxInclude.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>Ajax call</p>
  
  <pre><code>$(document).ready(function(){
      $('#myForm').submit(function(){
          var data = $(this).serialize();
  
          $.ajax({
              url: "DAO/insert.php",
              type: "POST",
              data: data,
              success: function( data )
              {
                  alert( data );
              },
              error: function(){
                  alert('ERRO');
              }
          });
  
          return false;
      });
  });
  </code></pre>
  
  <p>Config :</p>
  
  <pre><code>&lt;?php
  
  define('HOST', '127.0.0.1');
  define('DB_NAME','test');
  define('PORT', '3306');
  define('USER','root');
  define('PASS','');
  
  $dsn = 'mysql:host='.HOST.'; port='.PORT.'; dbname='.DB_NAME;
  
  try {
  $bd = new PDO($dsn, USER, PASS);
  //  $bd-&gt;setAttribute(PDO::ATT_ERRMODE, PDO::ERRMODE_EXCEPTION);
  } catch (PDOException $e) {
  echo 'Houve algum erro no Banco de Dados';
  }
  
  ?&gt;
  </code></pre>
  
  <p>Insert:</p>
  
  <pre><code>&lt;?php
  require_once('config.php');
  
  if(isset($_POST['submit'])){
  
      $nome = $_POST['nome'];
      $tipo = $_POST['tipo'];
  
      $sql =  'INSERT INTO produto(id, nome, tipo, criado_em) ';
      $sql .= ' VALUES (NULL, :nome, :tipo, NOW())';
  
      try {
          $query = $bd-&gt;prepare($sql);
          $query-&gt;bindValue(':nome', $nome, PDO::PARAM_STR);
          $query-&gt;bindValue(':tipo', $tipo, PDO::PARAM_STR);
  
          if($query-&gt;execute()){
              echo "Dados inseridos com sucesso";
          }else{
              echo "Falha na inser��o de dados";
          }
  
      } catch (Exception $e) {
          echo $e-&gt;getMessage();
      }
  }
  ?&gt;
  </code></pre>
  
  <p>I've changed it a million times, and still, I can't even get the returns from the insert, I just get the alert from the sucess from ajax.</p>
  
  <p>Sorry for so simple question, but I'm having some bad time with it. I'm trying to do all with the best practices I've found, and haven't found a solid example of this.</p>