Default text input reads from mysql db

Hi Guys - This is my 1st question and 1st day on this forum.
Any help you can provide would be appreciated.
I have a simple db table setup on my raspberry pi that currently only stores IP addresses for a main device and 2 auxillary devices.
I'm able to save new IP address entries to my database but I'd like to have the IP address value that's currently in the database be displayed as the value in the field rather than just the "label name".

1st-q

I am not sure this is what you are asking for, but you can set an initial value for the form fields.

If your field names are MainIP, AuxIP1 and AuxIP2, send a message with msg.payload.MainIP, msg.payload.AuxIP1 and msg.payload.AuxIP2.

Untitled 2

[{"id":"7e181bcb67b9f660","type":"ui_form","z":"59da08a70aea79db","name":"","label":"","group":"9755aa093da6a48c","order":1,"width":"8","height":"4","options":[{"label":"Main IP","value":"MainIP","type":"text","required":true,"rows":null},{"label":"Aux IP 1","value":"AuxIP1","type":"text","required":false,"rows":null},{"label":"Aux IP 2","value":"AuxIP2","type":"text","required":false,"rows":null}],"formValue":{"MainIP":"","AuxIP1":"","AuxIP2":""},"payload":"","submit":"submit","cancel":"cancel","topic":"topic","topicType":"msg","splitLayout":"","className":"","x":410,"y":120,"wires":[["ea7babb0d96543c2"]]},{"id":"654ca0c4d30f52d7","type":"inject","z":"59da08a70aea79db","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"{\t    \"MainIP\":\"192.168.1.100\",\t    \"AuxIP1\": \"10.0.0.95\",\t    \"AuxIP2\": \"127.0.0.1\"\t}","payloadType":"jsonata","x":270,"y":120,"wires":[["7e181bcb67b9f660"]]},{"id":"ea7babb0d96543c2","type":"debug","z":"59da08a70aea79db","name":"debug 237","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":570,"y":120,"wires":[]},{"id":"9755aa093da6a48c","type":"ui_group","name":"Pies","tab":"e61b26453c6cfc60","order":1,"disp":false,"width":"24","collapse":false,"className":""},{"id":"e61b26453c6cfc60","type":"ui_tab","name":"Pies","icon":"dashboard","disabled":false,"hidden":false}]
1 Like

Thanks so much for the quick reply. It's not exactly what I need but I did learn something from your example, so thanks again. I should have added the JSON and a screenshot from my database.

I'd like the values in the database to always be displayed in the field so that:
When a user navigates to the web page, they'll see the values that are already in the db, and:
When they enter a new value, that new value is going to be displayed in the input fields.

A quick edit to further explain, the value being used should always be displayed. Using the above example of the "Main IP" field. The IP address that shows is the IP Address that's being used. The Text Input field is an input and a "read back". If we take 192.168.1.100 as an example. If that's the IP of the "Main" unit, that should be the IP that shows, which is exactly what we see in jbudd's example.
If I enter 192.168.1.101 into the text input field, that should be the value that's passed to the database and the value that now displays in the Main IP input field.
So jbudd's image example is exactly right for how it should look. I just need it to show the new value every time the IP address is changed by the user.
Hope this makes sense.

Any help you could give would be appreciated. Thanks!

test-db

[{"id":"6347f6358104d5ad","type":"ui_form","z":"9a0b69c2bdc3d86a","name":"","label":"IP Form","group":"0cd96a8f430a482a","order":3,"width":0,"height":0,"options":[{"label":"Main IP Address","value":"ipMain_entry","type":"text","required":true,"rows":null},{"label":"Aux 1 IP Address","value":"ipAux1_entry","type":"text","required":true,"rows":null},{"label":"Aux 2 IP Address","value":"ipAux2_entry","type":"text","required":false,"rows":null}],"formValue":{"ipMain_entry":"","ipAux1_entry":"","ipAux2_entry":""},"payload":"","submit":"submit","cancel":"cancel","topic":"topic","topicType":"msg","splitLayout":"","className":"","x":360,"y":400,"wires":[["adceece4ddc319e0"]]},{"id":"a94a160a8c2ecf9c","type":"mysql","z":"9a0b69c2bdc3d86a","mydb":"be040c076f34df78","name":"","x":730,"y":400,"wires":[["52abeedc0976b03a"]]},{"id":"52abeedc0976b03a","type":"debug","z":"9a0b69c2bdc3d86a","name":"debug 23","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":920,"y":400,"wires":[]},{"id":"adceece4ddc319e0","type":"function","z":"9a0b69c2bdc3d86a","name":"Insert IP","func":"var ipMain_entry = msg.payload.ipMain_entry;\nvar ipAux1_entry = msg.payload.ipAux1_entry;\nvar ipAux2_entry = msg.payload.ipAux2_entry;\n\nmsg.topic = \"UPDATE`test` SET`ID` = '1', `ipMain` = '\" + ipMain_entry + \"', `ipAux1` = '\" + ipAux1_entry + \"', `ipAux2` = '\" + ipAux2_entry + \"' WHERE ID=1\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":400,"wires":[["a94a160a8c2ecf9c"]]},{"id":"986e4cc5394398b9","type":"function","z":"9a0b69c2bdc3d86a","name":"Select Query","func":"msg.topic = \"SELECT`ID`, `ipMain`, `ipAux1`, `ipAux2` FROM `test` WHERE 1\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":170,"y":400,"wires":[["6347f6358104d5ad"]]},{"id":"0cd96a8f430a482a","type":"ui_group","name":"IP Entry Form Fields","tab":"342ba07502d51c10","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"be040c076f34df78","type":"MySQLdatabase","name":"","host":"127.0.0.1","port":"3306","db":"testdb","tz":"","charset":"UTF8"},{"id":"342ba07502d51c10","type":"ui_tab","name":"IP Entry Form","icon":"dashboard","disabled":false,"hidden":false}]

The problem is that with my example the IP addresses disappear when you press Submit or Cancel?

So after Submit you can once more send the values to the form.

I don't know if it's possible to catch if the Cancel button is clicked and re-initialise the fields.
You might have to use text input fields and button nodes rather than the form to achieve that.
Or maybe someone else might know how it can be done.

1 Like

Yeah. I was going to try the "non-form" text input fields and button nodes next. I just didn't want to give up in case someone has accomplished this before. Thanks anyway for taking the time to try to help me!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.