Handling POSTed data from forms #4654
Replies: 1 comment
-
Posted at 2018-07-19 by @allObjects The response to the post has to set render the checkbox as expected - and it most likelu doe snot do it - it just renders it by default unchecked. What's the link to the demo? Posted at 2018-07-20 by user92178 http://www.espruino.com/Posting+Forms Posted at 2018-07-20 by @gfwilliams Right - I think I see what's going on... The flow of execution goes something like this:
So actually if you want to receive all the data and then send out the new page, you can use the I tweaked the main bit of code to do what I believe you're after here:
(I also send a Content-Length header as it looks like on recent Espruino builds that may be needed to work nicely with Chrome) Posted at 2018-07-20 by @allObjects @92178, give code below - version 1 - a shot (changes marked/commented with // ##### ....).
This is one way to do it... another one - version 2 - would be to read the status of the leds right in the expressions in the template, which would then be the only change to the code, such as:
Version 2 though works only when the LED1 and LED2 pins are set to some of output... In other words, if it is not working, add these two lines before line
The reason that both versions works is due to the fact that get and post have the same code path to respond to the request and therefore respond with the same html. Just before the response is created, post data is processed is conditionally invoked ( Posted at 2018-07-20 by user92178 Thank you Gordon, I tried with your changes and it now works as expected. I will try to apply this to my project that involves monitoring and controlling a battery charger. I want to be able to read battery voltage and charge current, display the readings in a table in the web browser and set the charge current and charge voltage remotely. I am using for example the LED1 output to turn the charger on and off. So I was struggling with trying to understand why the webpage would show data that was not updated. I intend to share the code when it is more functional, there may be other people trying to do something similar. Posted at 2018-07-20 by user92178 Thank you, allObjects. I greatly appreciate your guidance and time and will try the changes you suggest. I am not much of a programmer and only barely know enough to keep my nose above water, but now and then I manage to get my code working. Kind of. Posted at 2018-07-20 by @allObjects @gfwilliams, comparing your solution with mine, I notice a change in flow when acting on a request. I assumed the example responds to the post request when done with receiving all data for the request and processing it and only after that writing the response... oops... how mistaken I was. No matter what, the common behavior of (post) request and response is receiving the whole (post) request, do the processing, and only after completion - successful or failed - return the response, because the processing may have an impact not only on particular data but also on the level of the whole response for HTTP request, such as the HTTP status response code (200,500,etc). @user92178, my example is not doing what it is supposed to do, because the response is created prematurely (for various reasons). If you go the route of my version 1 - storing the post data - I prefer storing the individual data - led1, led2 - separately from the requestData... because multiple different posts may update different sets of data, and the way it is built right now is that post handling blows away all data. If you do not want to store the data individually, you can create a separate object and update it with what the various posts provides. For things like the LEDs though, I prefer solution approach 2, which reads the actual status of the LEDs (hardware), because some - other than post - event and process may change the LED status and the response / storedData will differ from reality... Therefore, in the template, use, for example, Posted at 2018-07-23 by @gfwilliams @user92178 great! I'd be really interested to see how you get on. @allObjects yes - I need to update the docs on POSTing. I believe this behavior is what node.js does too (eg if someone is POSTing a 4gb file you don't want to have the receive the whole file before you execute your handler), but it's definitely not what you'd expect. Posted at 2018-07-23 by Wilberforce If you change the method here to
You can then just the parse the values of out the URL string. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2018-07-19 by user92178
I tried the demo on my Espruino WiFi 1.1 and it worked as expected. Except I am wondering about that when I click one of the check boxes that control the LEDs, then click Submit, the page is reloaded and the check box value then becomes unchecked.
Is there a way to modify the code so that a check box stays checked when reloading the page? That could perhaps be done so that the value of the check box is saved and when reloading the page, that check box stays checked?
that brings me to the question if the page is loaded before or after postDATA is updated?
Beta Was this translation helpful? Give feedback.
All reactions