How do i get data to autofil form - javascript, php
<p>so this is the code i have so far</p>

<p>here is display file -</p>

<pre><code>    try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  //$DBH-&gt;prepare('SELECT first FROM contacts');
}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e-&gt;getMessage(), FILE_APPEND);   
}  
//get query
$FNresult=$DBH-&gt;query('SELECT first FROM contacts'); 
//set fetch mode
$FNresult-&gt;setFetchMode(PDO::FETCH_ASSOC);

$dropdown = "&lt;select name='contacts' id='contacts' &gt;";

while($row =$FNresult-&gt;fetch()) {

  $dropdown .= "\r\n&lt;option value='{$row['first']}'&gt;{$row['first']}&lt;/option&gt;";
 // echo getLN();

}

$dropdown .= "\r\n&lt;/select&gt;";

echo $dropdown;


//}
/*
//                  Get last name

function getLN(){
    $query = "SELECT last FROM contacts";
    $LNresult=mysql_query($query);

    $last;
    while($row = mysql_fetch_assoc($LNresult)) {

        $last = "{$row['last']}";

    }
    echo $last;
}//end getLN
*/

$DBH = null; 
?&gt;
&lt;script type="text/javascript"
     src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;

&lt;!-- javascript on client-side --&gt;
&lt;script type="text/javascript"&gt;  

var dropdown = $('#dropdown');

dropdown.bind('change', function(){
    $post('backgroundScript.php', 
        { 
            first: dropdown.val() 
        },
        function(response) {
            $('#first').val(response.first);
            $('#last').val(response.last);
            // Repeat for all of your form fields
        },
        'json'
    );
});
&lt;/script&gt;

&lt;script&gt;//$("#dropdown-parent").on('change','#dropdown', function() { // replace dropdown-parent
    /*$("#contacts").on('change', function() {

        $.post("backgroundScript.php", { 
                uid: $(this).val() 
            }, 
             function(data) { 
                 $("#first").val(data.first); 
                 $("#last").val(data.last); 
                 // etc.; 
            }, 'json' 
        ); 
    }); 
    */
/*$.post({
    'backgroundScript.php', // Script your posting to
    { 
        someParam1: someData1, // $_POST['someParam1']
        someParam2: someData2
        // etc etc
    },
    function(response){
        // Do something with JSON response upon successful post
        alert(response);
    },
    'json' // Tells the script that JSON will be returned
});*/&lt;/script&gt;




&lt;form action="insert.php" method="post"&gt;
First Name: &lt;input type="text" id="first" name="first"&gt;&lt;br&gt;
Last Name: &lt;input type="text" id="last"&gt;&lt;br&gt;
Phone: &lt;input type="text" id="phone"&gt;&lt;br&gt;
Mobile: &lt;input type="text" id="mobile"&gt;&lt;br&gt;
Fax: &lt;input type="text" id="fax"&gt;&lt;br&gt;
E-mail: &lt;input type="text" id="email"&gt;&lt;br&gt;
Web: &lt;input type="text" id="web"&gt;&lt;br&gt;
&lt;input type="Submit"&gt;
&lt;/form&gt;
</code></pre>

<p>here is the backgroundScript.php file - </p>

<pre><code>try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  //$DBH-&gt;prepare('SELECT first FROM contacts');
}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e-&gt;getMessage(), FILE_APPEND);   
}  


$sth = $DBH-&gt;prepare('SELECT *
    FROM contacts
    WHERE first = :first');
$sth-&gt;bindParam(':first', $first, PDO::PARAM_STR);
$sth-&gt;execute();

$firstName = $_POST['first'];

// background script

// retrieve data based on $_POST variable, set to $returnArray
$returnArray = $_POST[array(
         'first' =&gt; $firstName,
         'last' =&gt; lastName,
)];

/****************************
 * the structure of returnArray should look something like
     array(
         'first' =&gt; firstName,
         'last' =&gt; lastName,

     )*/
    // echo json_encode(array('first' =&gt; "hello", 'last' =&gt; "Other value"));
echo json_encode($returnArray);
$DBH = null; 
?&gt;
</code></pre>

<p>so what i am trying to do is there is a dropdown menu and in that are names from mysql db and each element in the db i want to fill in the forms blanks if they have values in db</p>

<p>here is my new upated main file ########################### </p>

<pre><code>try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  //$DBH-&gt;prepare('SELECT first FROM contacts');
}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e-&gt;getMessage(), FILE_APPEND);   
}  
//get query
$FNresult=$DBH-&gt;query('SELECT first FROM contacts'); 
//set fetch mode
$FNresult-&gt;setFetchMode(PDO::FETCH_ASSOC);

$dropdown = "&lt;select name='contacts' id='contacts' &gt;";

while($row =$FNresult-&gt;fetch()) {

  $dropdown .= "\r\n&lt;option value='{$row['first']}'&gt;{$row['first']}&lt;/option&gt;";
 // echo getLN();

}

$dropdown .= "\r\n&lt;/select&gt;";

echo $dropdown;


//}
/*
//                  Get last name

function getLN(){
    $query = "SELECT last FROM contacts";
    $LNresult=mysql_query($query);

    $last;
    while($row = mysql_fetch_assoc($LNresult)) {

        $last = "{$row['last']}";

    }
    echo $last;
}//end getLN
*/

$DBH = null; 
?&gt;
&lt;script type="text/javascript"
     src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;

&lt;!-- javascript on client-side --&gt;
&lt;script type="text/javascript"&gt;  

var dropdown = $('#dropdown');

dropdown.bind('change', function(){
    $post('backgroundScript.php', 
        { 
            first: dropdown.val()

        },
        function(response) {
            $('#first').val(response.first);
            $('#last').val(response.last);
            // Repeat for all of your form fields
        },
        'json'
    );
});
&lt;/script&gt;

&lt;script&gt;//$("#dropdown-parent").on('change','#dropdown', function() { // replace dropdown-parent
    /*$("#contacts").on('change', function() {

        $.post("backgroundScript.php", { 
                uid: $(this).val() 
            }, 
             function(data) { 
                 $("#first").val(data.first); 
                 $("#last").val(data.last); 
                 // etc.; 
            }, 'json' 
        ); 
    }); 
    */
/*$.post({
    'backgroundScript.php', // Script your posting to
    { 
        someParam1: someData1, // $_POST['someParam1']
        someParam2: someData2
        // etc etc
    },
    function(response){
        // Do something with JSON response upon successful post
        alert(response);
    },
    'json' // Tells the script that JSON will be returned
});*/&lt;/script&gt;




&lt;form action="insert.php" method="post"&gt;
First Name: &lt;input type="text" id="first" &gt;&lt;br&gt;
Last Name: &lt;input type="text" id="last"&gt;&lt;br&gt;
Phone: &lt;input type="text" id="phone"&gt;&lt;br&gt;
Mobile: &lt;input type="text" id="mobile"&gt;&lt;br&gt;
Fax: &lt;input type="text" id="fax"&gt;&lt;br&gt;
E-mail: &lt;input type="text" id="email"&gt;&lt;br&gt;
Web: &lt;input type="text" id="web"&gt;&lt;br&gt;
&lt;input type="Submit"&gt;
&lt;/form&gt;
</code></pre>

<p>here is my new upated backgroundScript.php file ###########################</p>

<pre><code>try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  //$DBH-&gt;prepare('SELECT first FROM contacts');
}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e-&gt;getMessage(), FILE_APPEND);   
}  

$first= $_POST['first'];
$sth = $DBH-&gt;prepare('SELECT *
    FROM contacts
    WHERE first = :first');
$sth-&gt;bindParam(':first', $first, PDO::PARAM_STR);

$sth-&gt;execute();




// background script

// retrieve data based on $_POST variable, set to $returnArray
while($row = $sth-&gt;fetch()){
  // do something with row
}
$returnArray = array( 
'first' =&gt; $row["firstName"], 
'last' =&gt; $row["lastName"], 
);

echo "&lt;pre&gt;";
print_r($returnArray);
echo "&lt;/pre&gt;";
exit;

/****************************
 * the structure of returnArray should look something like
     array(
         'first' =&gt; firstName,
         'last' =&gt; lastName,

     )*/
    // echo json_encode(array('first' =&gt; "hello", 'last' =&gt; "Other value"));
echo json_encode($returnArray);
$DBH = null; 
</code></pre>
