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
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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.