[Solved] auto fill html form using php [closed]


To fill the form without reloading the page you will have to use Ajax to request the data from the server (database) and then fill it using javascript, i suggest reading about ajax functions with jquery.

if you want for example to fill the id and click a button and the page will reload with the data, use the php post method, a quick example:

<?php
    if($_POST['getdata']){
        $id = $_POST['itemID'];
        /*
        connect to the database and get the data here and return in with an array called $data for example
        */
    }
    if($_POST['savedata']){
        /*
        use this if you want to do another action to the form, update the values for example
        */
    }
?>
<form action='' method='POST'>
    <input name="id" value="<?php echo $data['id'];?>" type="text" />
    <input name="name" value="<?php echo $data['name'];?>" type="text" />
    <input name="contact" value="<?php echo $data['contact'];?>" type="text" />
    <input name="getdata" value="Get Data" type="submit" />
    <input name="savedata" value="Save Data" type="submit" />
</form>

UPDATE:

regarding your update

first of all you better use PDO or MySQLi database instead of MySQL function because mysql_* is no longer maintained so since you are fresh to php learn PDO instead.

to your code, if you are expecting a single row from the database select statement, you should not use while() because its used to loop through the mysql object which has more than one row.

if we assume your table has exactly email, firstname, lastname fetch it directly into an array:

$myrow = mysql_fetch_assoc($result);

then convert that array into json and print it out

echo json_encode($myrow);
exit();

11

solved auto fill html form using php [closed]