How to Extract Data from Database and Display in a Form

magento-1.7magento-1.8magento-1.9

I stored some data in database. I want to function my program in such a way that when I click on an edit link whole data gets extracted from database and get displayed on a form.

enter image description here

When I click on 1st row edit button its corresponding data must get displayed in the above form.

The given below is my code for form and table.

<form role="form" action="<?php echo $this->getUrl('test/user_view/save'); ?>" method="post">
 <div id="form_div">
  <label>Title</label>
  <input id="title" type="text" name="title" />
  <br /><br />
  <label>File Name</label>
  <input id="file_name" type="text" name="filename" />
  <br /><br />
  <label>Content</label>
  <input id="content" type="text" name="content" />
  <br /><br />
  <label>Status</label>
  <input id="status" type="text" name="status" />
  <br /><br />
  <input id="add_button" type="submit" value="Add" />
 </div>
 <br /><br />
</form>
<form role="form" action="<?php echo $this->getUrl('test/user_view/edit'); ?>" method="post">
 <table border="">
  <tr>
   <th>Test_Id</th>
   <th>Title</th>
   <th>File Name</th>
   <th>Content</th>
   <th>Status</th>
   <th>Created Time</th>
   <th>Updated Time</th>
   <th>Edit</th>
   <th>Delete</th>
  </tr>
 <?php
 $model = Mage::getModel('test/test')->getCollection();
 foreach($model as $data)
  {
    echo '<tr>';
    echo '<td>'.$data->getTestId().'</td>';
    echo '<td>'.$data->getTitle().'</td>';
    echo '<td>'.$data->getFilename().'</td>';
    echo '<td>'.$data->getContent().'</td>';
    echo '<td>'.$data->getStatus().'</td>';
    echo '<td>'.$data->getCreatedTime().'</td>';
    echo '<td>'.$data->getUpdateTime().'</td>';
    ?>
    <td><a id="edit">Edit</a></td>
    <td><a href='?id=<?php echo $data->getTestId() ?>'>Delete</a></td>
    <?php
    echo '</tr>';
   }
 ?>
 </table>
 </form>

Best Answer

Through Ajax you can get data from database in the following way:

First send the Ajax request with id parameter through onClick() event of edit link as shown below:

<?php
$model = Mage::getModel('test/test')->getCollection();
foreach($model as $data)
{
  echo '<tr>';
  echo '<td>'.$data->getTestId().'</td>';
  echo '<td>'.$data->getTitle().'</td>';
  echo '<td>'.$data->getFilename().'</td>';
  echo '<td>'.$data->getContent().'</td>';
  echo '<td>'.$data->getStatus().'</td>';
  echo '<td>'.$data->getCreatedTime().'</td>';
  echo '<td>'.$data->getUpdateTime().'</td>';
 ?>
 <td><a id="edit" onclick="getData(<?php echo $data->getTestId()?>)">Edit</a></td>
 <td><a href='?id=<?php echo $data->getTestId() ?>'>Delete</a></td>
 <?php
 echo '</tr>';
}
?>

Then write the getData() function to send the ajax request as shown below:

<script>
function getData(id){
  var postData = {"id":id};
  var formURL = "<?php echo $this->getUrl('test/user_view/getData'); ?>" ;  
  jQuery.ajax({
                 type: "POST",
                 url: formURL,
                 data: postData,
                 dataType: 'json',
                 success: function(result)
                           {
                             jQuery("#title").val(result.title);
                             //similarly assign other form field value.

                           } 
                }); 

}
</script>