-->
Page 1 of 2

how to pass variable in url

PostPosted: Sat Dec 05, 2015 12:13 pm
by Trickuncle
Goal: read a slider control value in browser and pass it back to ESP8266 server to be used as parameter for pwm of pin.
This code reads the slider and in the last line prints it successfully to the page as a debug step. But the line that puts the x variable in the url results in the url looking like this:
http://192.168.1.174/?somevar= x
And then my Lua program borks at trying to use x as the pwm parameter. I have tried endless methods of converting the var x to a string but so far nothing works. This is my first exposure to javascript.

Can someone tell me how to successfully pass the slider value in the url? Thanks!!!

Code: Select allconn:send('<script>')
        conn:send('function myFunction() {')              
        conn:send('    var x = document.getElementById("cmd").value;')
        conn:send('    document.location.href=\"?somevar= x ";') 
        conn:send('    document.getElementById("demo").innerHTML =  x ;')
conn:send('}</script>')

Re: how to pass variable in url

PostPosted: Sat Dec 05, 2015 1:37 pm
by trackerj
You can find a working HTML Slider in the MAINS Dimmer web interface example. Just take the HTML/script part of the code and adapt it for your needs.

Re: how to pass variable in url

PostPosted: Sat Dec 05, 2015 2:49 pm
by Trickuncle
Hi trackerj! Your code is a few levels above my understanding at present but thanks for posting it!

Right now, I would love to find out why my javascript code doesn't function.

Re: how to pass variable in url

PostPosted: Sat Dec 05, 2015 8:45 pm
by Trickuncle
Partial success!!! I modified the code to use this function: encodeURI(uri) and now it works in Mozilla Firefox and maybe MSIE. Unfortunately, it appears that Chrome is confused. This is unfortunate because I have an Android phone running chrome and it was my plan to use it to control the system.

Onward and sideways!

Code: Select all        conn:send('<script>')
        conn:send('function myFunction() {')               
        conn:send('    var x = document.getElementById("cmd").value;')
        conn:send('    document.getElementById("demo").innerHTML =  x ;')
        conn:send('     var uri = "\?somevar=" + x;')
        conn:send('     var enc = encodeURI(uri);')
        conn:send('     document.location.href=\enc;')         
        conn:send('}</script>')