Load data from database & retain data between postbacks
<p>In PHP Project, I'm creating page a which retrieve the data from database on page load then allow the user to change data &amp; Update. I have to use <code>input text</code> and <code>option</code> controls to display data. Editing form data may involve in few <code>POST</code> requests. To load data from database and retain data between <code>POST</code>s I use this approach.</p>

<p><strong>Load Data from Database</strong> </p>

<pre><code>$oClass = new DataClass();
$oClass -&gt;GetRecordById($_REQUEST['id']);
$dataName = $oClass -&gt;Name;
$dataYear = $oClass -&gt;Year;
</code></pre>

<p><strong>INPUT TEXTs</strong></p>

<pre><code>&lt;input type="text" name="txtName" value="&lt;?php echo isset($_POST['txtName']) ? $_POST['txtName'] : $dataName ?&gt;" &gt;
</code></pre>

<p><strong>OPTIONs</strong></p>

<pre><code>$arrYears = GetYears();
foreach ($arrYears as $year) {

    if (isset($_POST['ddlYear'])) {
        if ($_POST['ddlYear'] == $year) {
            echo '&lt;option value="' . $year . '"  selected="selected"&gt;' . $year . '&lt;/option&gt;';
        } else {
            echo '&lt;option value="' . $year . '"&gt;' . $year . '&lt;/option&gt;';
        }
    } else {
        if ($dataYear == $year) {
            echo '&lt;option value="' . $year . '"  selected="selected"&gt;' . $year . '&lt;/option&gt;';
        } else {
            echo '&lt;option value="' . $year . '"&gt;' . $year . '&lt;/option&gt;';
        }
    }
}
</code></pre>

<p>Is there a better approach to retain data between posts than using like</p>

<pre><code>if (isset($_POST['NAME']))
 {$_POST['NAME']} 
else
 {$name}
</code></pre>
