title: jQuery AJAX process not working (Updating div with JSON results)
body: <p>I am trying to fetch some results from a database, then convert them to JSON and display the results in a div that is updated through AJAX and triggered by the Go! (form submit) button.</p>
  
  <p>The code is not working, and I don't know why. I already checked my database connection, with other simple examples, and I am able to connect and even fetch results from the same database, but whenever I try to use it with AJAX/jQuery it does not work.</p>
  
  <p>NOTE: Whenever I try to see the JSON object in the console, I get nothing but this error:</p>
  
  <blockquote>
    <p>Uncaught SyntaxError: Unexpected identifier index.php:20</p>
  </blockquote>
  
  <p>index.php:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Page&lt;/title&gt;
      &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form method="post"&gt;
          Username:
          &lt;input type="text" name="username" id="usrnfield"&gt;&lt;br&gt;
          &lt;input type="submit" value="Go!" id="subForm"&gt;
      &lt;/form&gt;
      &lt;div id="result"&gt;
          &lt;p&gt;Here are the usernames:&lt;/p&gt;
      &lt;/div&gt;
      &lt;script type="text/javascript"&gt;
          $("#subForm").click(function(){
              $.ajax({
                  type: "POST"                                      
                  url: 'fetchuser.php',                          
                  data: "",                        
                  dataType: 'json',     
                  success: function(data)
                  {
                      var id = data[0];
                      var username = data[1];
                      var firstname = data[2];
                      var lastname = data[3];
                      var email = data[4];
  
                      $("#result").html("
                          &lt;b&gt;id: &lt;/b&gt;"+id+"&lt;br&gt;
                          &lt;b&gt;Username: &lt;/b&gt;"+username+"&lt;br&gt;
                          &lt;b&gt;First Name: &lt;/b&gt;"+firstname+"&lt;br&gt;
                          &lt;b&gt;Last Name: &lt;/b&gt;"+lastname+"&lt;br&gt;
                          &lt;b&gt;Email: &lt;/b&gt;"+email+"&lt;br&gt;
                      "); 
                  } 
              });
          }); 
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>fetchuser.php:</p>
  
  <pre><code>&lt;?php    
    $dbhost = "localhost";    
    $dbname = "database";    
    $dbuser = "root";    
    $dbpass = "root";    
  
    $pdo = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
    $rows = array();
  
    if(isset($_POST['username'])) {
      $stmt = $pdo-&gt;query("SELECT * FROM userlist WHERE username = ?");
      $stmt-&gt;execute(array($_POST['username']));
      $rows = $stmt-&gt;setFetchMode(PDO::FETCH_ASSOC); 
      echo json_encode($rows);
    }
  ?&gt;
  </code></pre>