Reputation: 1
I'm trying to develop a little project and I got a little problem I'm making a PHP page connected to a MySQL Server. I have a page where only the administrator have access and that page allows him to change users information. So, I have a select that show all usernames on website and 3 textbox where can be inserted the First Name, Last Name and e-mail. My database update successfully, but what i'm trying to do is when is select a username on select the 3 textbook auto fill with that username information. Here is an image that explains better what I would like to do. Sorry for my english and thanks to all http://s3.postimg.org/da9srvh5f/table_copy.jpg
Upvotes: 0
Views: 132
Reputation: 1574
<script type="text/javascript">
function reload() {
var val=document.getElementById("c-name").value;
self.location="your_url.php?name=" + val ;
//alert(val);
}
</script>
<form name="data" action="your_url.php" method="post">
<table id="tbl">
<tr>
<td>Name </td>
<td>
<select id="c-name" name="name" onChange="reload()" style="width:340px;">
<option value="">Select Name</option>
<option value="abc">ABC</option>
<option value="xyz">XYZ</option>
<option value="mno">MNO</option>
</select>
</td>
</tr>
<?php
if(isset($_GET['name'])){
$name = $_GET['name'];
$SQL_Data = "select * from client_data where cname = '".$name."'";
$result = mysqli_query($SQL_Data);
while($details = mysqli_fetch_array($result)) {
?>
<tr>
<td>First Name</td>
<td><input type="text" name="fname" class="c1" value="<?=$details['fname']?>" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" class="c7" name="lname" value="<?=$details['lname']?>" /></td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" class="c7" name="uname" value="<?=$details['uname']?>" /></td>
</tr>
<?php
} }
?>
<tr><td align="center" colspan="2"><input type="submit" name="submit" value="Submit" /></tr>
</table>
</form>
Upvotes: 2
Reputation: 20007
You could have your PHP backend return JSON data to your frontend. Then, with javascript, do something like:
document.getElementsById('ID').Value = VALUE;
Where ID
is the Id of your select and VALUE
is the value found in your JSON data.
Alternatively, you could do this purely in PHP/MySQL.
Upvotes: 1