Handling POST forms and data

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
jstaffon
Posts: 80
Joined: 29 May 2015 04:47

Handling POST forms and data

Post 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!
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: Handling POST forms and data

Post by PoltoS »

http.request uses curl. You can compare browser and Z-Way using tcpdump
jstaffon
Posts: 80
Joined: 29 May 2015 04:47

Re: Handling POST forms and data

Post 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?
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: Handling POST forms and data

Post 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.
jstaffon
Posts: 80
Joined: 29 May 2015 04:47

Re: Handling POST forms and data

Post 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?
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: Handling POST forms and data

Post by PoltoS »

should
Post Reply