• Hello,

    I made a form to add entries to a database, however I’m having issues with the update. When I want to edit the form, I want the fields to auto populate with the information from the database based on the ID, and then update based on that ID. I don’t want to have to create different forms for everyone (because I know you can manually set to update by ID) but I have 100’s of entries in my database that can be updated so it would be incredibly tedious and time consuming to have to make a different form for every entry in the database.

    So, is there anyway to dynamically auto populate form fields with database content based on an ID, and then also update based on that ID (if the ID is passed to the page through a GET or some other means)

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Yes the fields can be dynamically populated etc.

    You need to write a plugin that registers several shortcodes that do various database functions. These shortcodes go into different pages.
    Say you have shortcodes called:
    – searchanddisplay
    – editentry
    – addentry
    – databaseadd

    You create your custom database tables using phpmyadmin etc, at this stage dont bother with creating the tables in code.

    This is what each of these shortcode functions would do:
    – searchanddisplay
    It has a search fields FORM and a results display table. The FORM has a “Search” button that does a submit to the same URL of this page. There could also be pagination controls, the pagination controls are distinct SUBMIT codes.
    On entry it test if the search fields are set, if not it sets the search to default like ALL or whatever. It renders the HTML for the form, setting the fields to the received values (or default). It then does an SQL queary, possibly with pagination. If there are no results it says so. Otherwise it generates a table with one result per row, each row has columns for the various fields and a column with an “EDIT” button which is a LINK to the page with the “editentry” shortcode, it also has the key for that row.

    – editentry
    Tests that the row key is set, complains otherwise.
    Tests if it is being invoked by itself using its own submit buttons, does the distinct operations some of which result in a page redirect, otherwise it does an SQL query on that row, displays the results in a form, there are submit action buttons for savechanges, back/cancel and delete.

    – addentry
    Displays a form, has submit buttons for ADD and cancel.
    The ADD action directs to databaseadd page. The cancel links to the searchdisplay page.

    – databaseadd
    Validates the fields and reports errors, otherwise does an SQL database insert, reports on errors such as duplicate keys.

    Note that other organizations are possible.
    You can use javascript to provide various support and convenience features (such as “clear search fields”)
    When I was getting started I found this plugin to be very handy:
    https://wordpress.org/plugins/wp-csv-to-database/

    Moderator bcworkz

    (@bcworkz)

    Ross said “Note that other organizations are possible.”

    That’s for sure! Shortcodes are certainly a reasonable approach. If you need these forms to appear in various post content, it’s likely the best approach. But if your forms only need to appear in one place, IMO shortcodes add needless complication. I personally would simply build the necessary form code as a PHP page of some sort. If the forms are to appear on the front end, I’d make a custom page template that handles everything. On the backend, I’d make a menu page callback that does the same.

    I judge the custom page template the simplest to implement. It’s as close as WP gets to a traditional stand alone PHP file. It’s best to consider what the best user experience would be before deciding what approach to use. What’s easiest shouldn’t be the criteria, but with limited time and/or skill, it becomes a factor.

    Yes, I concede that custom page templates present a much more conventional application development context, your comment is a worthwhile contribution to this discussion. Some things are even easier, like I described page redirects, now I think of it I am not sure how I would do this from shortcodes ? Ideas anyone ?

    However page templates are an uncomfortable straddle across the realms of theme which controls presentation, and all the rest which controls content. Separating presentation from content is a major theme in the world of the web, html vs css, why you don’t use tables any more to control layout etc. On occasions we have to muddle this separation, like when we target page IDs in css etc, but in general it is a divide worth maintaining.

    Building applications in page templates is contrary to the WordPress ecosystem, there are a zillion plugins in the library and elsewhere, I have yet to run across one that incorporates creating an alliance with page templates, which would be a good technique, I just have not seen it.

    Thoughts anyone ?

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Auto populate fields’ is closed to new replies.