minimark,
You can use IFRAMEs to complete this. Run the database query in the IFRAME source and have it print its output in the script.
Main Page
PHP Code:
<form id='userinfo' action='userinfo.php' method='post'>
<p><select name='userid' onchange='updateform( this.value )'>
<option value='1'>1</option>
<option value='2'>2</option>
</select></p>
<p><input type='text' name='username'/></p>
<p><input type='text' name='usertitle'/></p>
<p><input type='submit' name='Update User'/></p>
</form>
<p id='errortag'> </p>
<form id='useridform' action='useridquery.php' target='useridframe' method='post'>
<p><input type='hidden' name='userid'/></p>
</form>
<iframe name='useridframe'></iframe>
<script type='text/javascript'>
function updateform( userid )
{
useridform = document.getElementById( 'useridform' );
useridform.elements[ 'userid' ].value = userid;
useridform.submit( );
}
</script>
useridquery.php
PHP Code:
<script type='text/javascript'>
<?php
$connection = mysql_connect( ... );
$connection = mysql_select_db( ... );
$userdataquery = mysql_query( "SELECT `username`, `usertitle` FROM `userdb` WHERE `userid` = " .mysql_real_escape_string( $_POST[ 'userid' ], $connection ) ."'", $connection );
if( $userdataquery && mysql_num_rows( $userdataquery ) )
{
print( " userform = parent.getElementById( 'userinfo' );\n" );
$userdata = mysql_fetch_assoc( $userdataquery );
foreach( $userdata as $userkey => $uservalue )
print( " if( typeof userform.elements[ '$userkey' ] !== 'undefined' ) userform.elements[ '$userkey' ].value = '$uservalue';\n" );
}
else print( "parent.document.getElementById( 'errortag' ).innerHTML = 'That user does not exist';\n" );
?></script>
NOTE: this has not been tested for syntax and the logic may not align with the expected output since I may have jumbled some variable/element names.
There are 2 forms on the main page. The first is the one your user interfaces, the second is purely functional. When the drop-down is changed in the first form, javascript sets the userid as the value in the second form then automatically submits that form. The target of the second form is the IFRAME, where the query runs and prints its results in the parent window either to the first form or to the errortag based on the outcome.