Jump to content

Prefill form data with mysql data.


tsiedsma

Recommended Posts

I found a piece of code that will check a radio button based on form input but I am pulling data from a database and prefilling the form data so I can update the data stored in the database.
[code]
          <td colspan="2"><input type="radio" name="ud_approved" value="YES"
          <?php
            if (isset($_POST['ud_approved']) && $_POST['ud_approved'] == 'YES') {
                echo 'checked=\"checked\" ';
            }
            ?>>YES
            <input type="radio" name="ud_approved" value="NO"
            <?php
              if (isset($_POST['ud_approved']) && $_POST['ud_approved'] == 'NO') {
                echo 'checked=\"checked\" ';
              }
            ?>>NO
       </td>
[/code]

I want the radio button to be preselected based on whether or not the field 'approved' in the database says either "YES" or "NO". How can I do this with simple php?

The other issue is, I need to update the database with my selection on the radio buttons. If I choose YES or NO, I need it to update to mysql. Do I have to do anything special to make this work? Just so you know, I am able to update every field except the radio buttons. They do not work for updating.
Link to comment
https://forums.phpfreaks.com/topic/4455-prefill-form-data-with-mysql-data/
Share on other sites

OK, I fixed the selection issue. I used the following code and now it selects the appropriate checkbox based on the mysql field value.
[code]
          <td colspan="2"><input type="radio" name="ud_approved" value="YES"
          <?php
            if ( $approved == 'YES' ) {
                echo 'checked=\"checked\" ';
            }
            ?>>YES
            <input type="radio" name="ud_approved" value="NO"
            <?php
              if  ( $approved == 'NO' ) {
                echo 'checked=\"checked\" ';
              }
            ?>>NO
           </td>
[/code]
Next issue... If I click on the opposite radio button and click submit to update my changes, it does not update the mysql table. Do I need to tree radio buttons differently for submitting form data to mysql?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.