Page 1 of 1

Handling POST forms and data

Posted: 31 Jul 2017 17:48
by jstaffon
I had this working at one time and something is now broken. I've modified the SecurityMode module in Zway to send data via "POST" to an Apache2 webserver running on my ZWay box (Raspbian Jessie and Zway v2.1.1) when a motion detector turns on. The modified section of the SecurityMode module is :

// If Email exist, then send email
if (self.email)
{
http.request(
{
method: 'POST',
url: "http://192.168.1.99",
data:
{
to: self.email,
subject: "Notification from Smart Home",
message: self.message
}
});
}

The PHP code at the root location of the Apache2 webserver is:

<?php
//if "to" variable is filled out, send email
if (isset($_POST["to"]))
{
//send email
mail($_POST["to"], $_POST["subject"], $_POST["message"]);
}
else
{
//Write error file
$fil = fopen('/tmp/zway-email-error.txt', w);
fwrite($fil, "Error in SecurityMode App Form");
fclose($fil);
}
?>

The above php code works if I create an index.html file/page to prompt me to fill out a form then call an "action" to execute the php file. If I want Zway to make an http request (I'm assuming it's using Mongoose) in the SecurityMode module, how do I tell the Apache2 website to accept the posted form data? I had this figured out at one time but now I'm confused. Any help will be appreciated!

Re: Handling POST forms and data

Posted: 02 Aug 2017 14:07
by PoltoS
http.request uses curl. You can compare browser and Z-Way using tcpdump

Re: Handling POST forms and data

Posted: 02 Aug 2017 23:18
by jstaffon
PoltoS wrote:
02 Aug 2017 14:07
http.request uses curl. You can compare browser and Z-Way using tcpdump
Not sure I follow. The code should issue an http.request to the Raspberry Pi because that's the IP I supplied in the code. My question is about a more fundamental understanding of what the file name is and what the code should look like. My php file works by itself if I have an index.html file on my Apache2 web server that displays a form and then Submits to the appropriate php file as stated in the Action statement. What I'm not sure of, is how to or where to put the index file that will capture the POSTed data from the http.request since it's being sent from the code and not an interactive form and I don't see a way to specify the Action or script to execute on the web server side. Do I need Apache2 running to capture the request or will the Mongoose web service capture the http.request? Also, where does the index file go and what does the name need to be?

Re: Handling POST forms and data

Posted: 02 Aug 2017 23:57
by PoltoS
http.request call in Z-Way JS API is a way to connect to a remote server (like you Apache). Action is method: POST or GET, location of the script is the url in http.request.

Re: Handling POST forms and data

Posted: 03 Aug 2017 05:00
by jstaffon
PoltoS wrote:
02 Aug 2017 23:57
http.request call in Z-Way JS API is a way to connect to a remote server (like you Apache). Action is method: POST or GET, location of the script is the url in http.request.
OK...I got that part now. Most web servers have an index.html file in the root directory. If I'm using my web server (Apache in this case) to catch the POST data from the http.request, what does the file name need to be? If I use index.html, but have php code in the file, will that catch the POST data?

Re: Handling POST forms and data

Posted: 03 Aug 2017 13:51
by PoltoS
should