Setting Variables from Web Form

I am really confused, I cannot seem to figure this out.

I am ready to build a project that involves collecting data on a form, then sending to the imp to do things with the data. Example, move motor this many steps… etc.

I am very new at this and was confident I could figure it out like arduino, but I need help. I have spent the entire night trying this with no resolution.

I am trying to learn step by step, starting with basic functions. The first task was getting the time. Done. The second task was getting a variable from a form, not done.

No matter what I change, if I am trying to assign a variable in the function set I get: “ERROR: trying to set ‘class’”.

The planner: I have HTTP IN pointed to my device(variable test) and pointed to Show Input. Whatever I type in the form shows on the show input. But the log gives me an error and seems to pause the imp.

The code:
`// variable test code

x <- 0; // set default
value <- 0; // set default

function display() //function to show on log and planner that value has been received
{

server.log(x); //fuction shows on code log
server.show(x); //fuction shows on planner
imp.wakeup(0.5, display); // loop
}

class value extends InputPort // import
{
name = “value” //the name I have the form field
type = “number” // the type of field I have the form set as
function set(value) // set function
{

    x = value;  // set "x" to equal whatever value I typed in on the form

    //imp.wakeup(0.5, display);

}
}

imp.configure(“Variable Test”, [value], []); //configure
display(); // run

//end code`

My form:
`

Select x value:
`

Any ideas?
Thank you.

Screenshot of planner attached.

I think your issue is with using value for both a class name & variable name. Try calling your class webValue or something else

class webValue extends InputPort imp.configure("Variable Test", [webValue], []);

Same error. :frowning:

Ok tested you code you need to create an instance of your class and use it in the imp config I use Postman a Chrome plugin great for testing

// variable test code

x <- 0; // set default
value <- 0; // set default

function display() //function to show on log and planner that value has been received
{

server.log(x); //fuction shows on code log
server.show(x); //fuction shows on planner
imp.wakeup(0.5, display); // loop
}

class webValue extends InputPort // import
{
name = “wsValue” //the name I have the form field
type = “number” // the type of field I have the form set as
function set(value) // set function
{

    x = value;  // set "x" to equal whatever value I typed in on the form

    //imp.wakeup(0.5, display);

}
}
local _webValue = webValue();

imp.configure(“Variable Test”, [_webValue], []); //configure
display(); // run

That did it! Thank you! Man I am in over my head I think. Now I need to figure out how to pass 5 or so variables in the same form. Do you think that is possible?
Thanks

No as you can only use Value you will have to combine the values and then split them out in the input port. It’s described hear see HTTP in it has both Web client code and imp code

http://devwiki.electricimp.com/doku.php?id=httpapi

You can use an array. Name you form variables value[0], value[1], value[2], etc. and your HTTP in will receive your form variables in an array named value.

example of the above:
`
class In extends InputPort {
function set(value) {
server.log("value is " + (typeof value))
foreach(v in value)
server.log(v)
}
}

imp.configure(“http-in-form-values”, [In()], [])
`

Your HTML form looks like this (use the url of your HTTP IN object):
`

`

(it’s frustrating how the forum editor always cuts off the code segments, and sometimes replaces quotes with html-entities, but not always…)

By the way, this trick only works with the HTTP IN node in the planner, not with Agents and http.onrequest(). The latter always returns the query as a table with key/value pairs. The keys are always strings, and in this case just would be “value[0]”, “value[1]” etc. These are not automatically converted to an array as the HTTP IN node does. Of course, you can use the table as is, and access each member in array notation. In this case as: req.body[“value[0]”], etc.