Using Javascript to update records
-
Is it possible to use Javascript to update a record in a WP Data Access table? I have a Javascript submit button handler on which it would be nice to be able to update a specific table, before calling a Data Table. I have searched the documentation without reaching any conclusion.
Thanks in advance.
-
Hi @jpamado,
To update records in a database table, we usually suggest using Data Forms. It’s currently not possible to modify a data table prior to it rendering on a page. The main purpose of it is to display data.
Could you tell us more about your code/project in case we can suggest something better?
We’ll wait for your response! ??
Sorry if the following information runs a bit long.
The project revolves around a database storing information from documents created between the 14th and 15th centuries. The client has requested the ability to search between dates, according to a specific ruleset (maybe a bit convoluted but they make sense to me – I’m also an historian).
Search results are shown via a Data Table based on a View. The ruleset for handling dates goes something like this:
- dates are stored both as separate values for Year, Month and Day and as a single (calculated) value in the YYYY-MM-DD format
- the 3 elements may not always be present, historical documents being the way they are; if a given date has only the Month element (for instance) then the calculated YYYY-MM-DD will have ’empty’ components, such as ‘0000-12-00’ (to be defined yet)
- a search may be performed over any one of these fields – Year, Month Day – separately or combined
- the search form has two sets of Y-M-D fields, for starting and ending dates
- searching individual elements – for instance, all documents belonging to a specific year – is made from the first set of Y-M-D fields; using the second set of fields in this context returns an error
- a search between dates can only be performed with full dates with Y-M-D.
There is further logic to verify if the dates belong inside the upper and lower chronological limits and also, when searching between dates, if the ending date is higher than the starting date and other odd bits and pieces. This logic has only been designed and not yet written.
My problem is that, since searches are performed over all the fields in the search form, I find myself with a search beeing made over an unexisting field in the database, since each document has only a creation date. I could add a second date field to the View, but that would not make sense.
As far as I could see, the problem would be solved with a WHERE clause, to control the record set over which the remaining search would be performed. I’m able to use form fields as arguments of the WHERE clause – but they are also used as search arguments by themselves, negating the putative results of the clause.
I asked about a Javascript solution because I’m using it to handle the behavior of the ‘Submit’ button in the search form, before opening the Data Table with the results. This way I can change the contents of the date fields according to the ruleset. So I came up with the following idea:
- any search, with or without a date component, would always have a WHERE clause for searching between dates – most of the time between the starting and ending dates of the full document set
- these starting and ending dates are stored as the single record of an auxiliary table, related to the main document body; this way, each and every document is always related to a starting and ending date (a bit of information that is meant to be used only in the search backoffice and never shown to the end-users)
- if there are valid starting and ending dates supplied in the search form, these values are written to the auxiliary table
- the way I see it, this would allow any search to be performed with a temporal WHERE clause – either with the full starting and ending dates, or with more specific ones
Writing the date values to the auxiliary table would have to be made in the Javascript phase of the process, after pressing the ‘Submit’ button, before opening the Data Table with the WHERE clause.
I don’t know if I’m overthinking this or if there is a blindingly obvious solution that I’m missing. The existing interface isn’t public yet, so I have no URL to show.
Any help is welcome ??
Best regards
jpamadoHi @jpamado,
Thanks for the detailed information about your project!
You mentioned an auxiliary table. Is this table already involved in your initial query for the table’s view?
Modifying a record in the backend would involve transactional actions to your database and in terms of doing it with JavaScript, I’ll have to ask Peter. (I’ll inform him of this post)
Have you checked out the Search Builder and Search Panes for Data Tables? It looks like you’re already using a separate search form but just in case the other two might be simpler for you.
We’ll follow up later with what Peter has to say about your initial question. ??
We appreciate your patience! ??
Hi @jpamado
If I understand your question correctly, you are designing your own search form and redirecting the user from your search form to your data table? Correct?
If your search form and your data table are on different pages, you can call an intermediate page on user submit. Your intermediate page can be a PHP page where you perform database tasks depending on the user input and then redirect to the data table page.
If your search form and your data table are on the same page and you use javascript to update the data table, you can also call a PHP page to perform your database tasks, but it will be difficult to sync this action with your data table update. You’ll need to wait until the database task is ready.
You cannot update your database directly from javascript!
Alternatively, this page might help you:
https://wpdataaccess.com/docs/data-tables-filters/static-filters/Please read through the httpGet, httpPost en httpRequest parameters. You can use them directly in your where clause. Although I don’t fully understand your requirements, I have the impression these new filter parameters could be an alternative. It will make the preparation of your arguments and your where clause more complex but increase performance as you can skip the database tasks.
Does this help?
Thanks,
PeterThank you both, Kim and Peter, for clarifying some doubts. I’ll investigate both the intermediate PHP page and the Search Builder – is it me, or was it presented differently in a previous version of the plugin?
Best regards and keep up the good work!
jpamadoHi @jpamado,
You’re welcome! ??
Search Builder – is it me, or was it presented differently in a previous version of the plugin?
The Search Builder looks the same as in the previous versions, but let us know if you need assistance in trying it out.
We’ll mark this thread as resolved in the meantime. If you have a new question, please create a new thread. ??
Thank you!
- The topic ‘Using Javascript to update records’ is closed to new replies.