PDA

View Full Version : Posting form data from the browser to the server


lyndonwong
04-13-2003, 12:58 PM
Hi all, any wisdom/help on posting data from LZX input fields up to server would be greatly appreciated!

I currently have a a working PHP script (input_data.php listed below) which accepts form-post input from an HTML form (index.php listed below), and writes that data to a log form. When I try to connect to that same PHP script from an LZX front-end (SimpleFormPost.lzx listed below), the PHP script executes and writes a new log entry, but does not get the parameter data passed from the LZX front-end (sender_name, sender_email, etc.).

The attached screen grab shows the LZX front-end, the HTML front-end and the log entries generated. Notice that Lyndon's HTML post successfully relays name and email info to the first log entry, but Gropius' LZX post yields blank values for name and email in the second log entry. In case the attachment doesn't work, the screen grab is also at:

http://lyndonwong.home.att.net/SimpleFormPost/

thx in advance,

Lyndon

[source code listings]

------------------------------------------------
HTML INPUT FORM: /SimpleFormPost/index.php
------------------------------------------------

<html>
<header>
<title>Simple Form Post</title>
</header>
<BODY>


<form action="store_input.php" method="POST">
<table>
<tr><th width=40%><th width=60%></tr>
<tr>
<td>
Your name:<br>
Your email:<br>
</td>
<td>
<input type="text" name="sender_name" /><br>
<input type="text" name="sender_email" /><br>
</td>
</tr>
</table>
<br>
<small>Enter Brief Message: (max. 3 lines of text)</small>
<br>
<textarea rows=4 cols=50 name="sender_text"></textarea>
<br><br>
<input type="submit" value="Send Data">
</form>

</body>
</html>


------------------------------------------------
LZX INPUT FORM: SimpleFormPost.lzx
------------------------------------------------

<canvas width="900" height="700" debug="true" >
<include href="redmond" />


<!-- DATASET DECLARATIONS -->

<dataset name="dsSendData" autorequest="false" src="http://192.168.1.4/~lyndonwong/SimpleFormPost/store_input.php" type="http" >
<method event="ondata">
debug.write(this.getPointer().getXPath("result/text()"));
</method>
</dataset>

<!-- APPLICATION INTERFACE -->

<view name="formFields">
<text y="10"> Name: </text>
<windowtext name="sender_name" datapath="@sender_name" width="100" x="80" y="10" />
<text y="35"> Email: </text>
<windowtext name="sender_email" datapath="@sender_email" width="100" x="80" y="35" />
<text y="60"> Message: </text>
<windowtext name="sender_text" datapath="@sender_text" width="100" x="80" y="60" />

<method name="sendData" >
var d=canvas.datasets.dsSendData;
var p=new LzParam();
p.addValue("sender_name", sender_name.getText(), true);
p.addValue("sender_email", sender_email.getText(), true);
p.addValue("sender_text", sender_text.getText(), true);
d.setQueryString(p);

debug.write(p);

d.doRequest();
</method>
</view>

<view y="90" >
<simplelayout axis="x" />
<button width="100" > Send Data
<method event="onclick">
canvas.formFields.sendData();
</method>
</button>
</view>

</canvas>

------------------------------------------------
SERVER-SIDE PHP SCRIPT: store_input.php
------------------------------------------------

<?php
/***************************************
** Filename.......: store_input.php
** Project........: SimpleFormPost
** Last Modified..: 13 April 2003
***************************************/

/***************************************
** The header() makes
** the output look lovely.
***************************************/

header('Content-Type: text/plain');


/******************************
** Prepare log entry data
******************************/

/***************************************
** These parameters passed from the
** calling script.
***************************************/

$msg_sender_name = $_POST["sender_name"];
$msg_sender_email = $_POST["sender_email"];
$msg_sender_text = $_POST["sender_text"];

/***************************************
** These parameters generated dynamically
** using PHP function calls.
***************************************/

$msg_date = date("Y-m-d");
$msg_time = date("H:i:s");
$msg_sender_ip = $_SERVER["REMOTE_ADDR"];
$msg_sender_hostname = gethostbyaddr($msg_sender_ip);
$msg_sender_browser = $_SERVER["HTTP_USER_AGENT"];

/************************************************** ****
** Write data to log file
************************************************** *****/

$filename = 'SimpleFormPost.log'; // Edit this path to access desired log file.

$log_entry = "<date>" . $msg_date . "</date>" . "<time>" . $msg_time . "</time>" . "<IP>" . $msg_sender_ip . "</IP>" . "<host>" . $msg_sender_hostname . "</host>" . "<name>" . $msg_sender_name . "</name>" . "<email>" . $msg_sender_email . "</email>" . "<browser>" . $msg_sender_browser . "</browser>" . "<destination>" . $msg_destination . "</destination>" . " \n";

// Make sure the file exists and is writable first.
if (is_writable($filename)) {

// opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $log_entry will go when we fwrite() it.
if (!$fp = fopen($filename, 'a')) {
print "Cannot open file ($filename)";
exit;
}

// Write $log_entry to our opened file.
if (!fwrite($fp, $log_entry)) {
print "Cannot write to file ($filename)";
exit;
}

// print "Success, wrote ($log_entry) to file ($filename)";

fclose($fp);

} else {
print "The file $filename is not writable";
}


// CONFIRMATION MESSAGE IN XML FORMAT.

echo "<result>"."\n";
echo "<name>".$msg_sender_name."</name>"." \n";
echo "<email>".$msg_sender_email."</email>"." \n";
echo "<msg_text>".$msg_sender_text."</msg_text>"." \n";
echo "<msg_status>".$msg_status."</msg_status>"." \n";
echo "</result>"."\n";
?>

antun
04-14-2003, 11:21 AM
Hey Lyndon

First off, your php script is expecting HTTP POST variables ($_POST["sender_name"] gets you the value of the HTTP POST variable named sender_name). By default when you do a request from an LZX app, it's going to send HTTP GET variables.

So in this line of your script you need to specify HTTP POST:


d.setQueryString(p, 'POST');


To help you debug this, you should have an onerror event handler in your dataset tag:


<dataset name="dsSendData"
autorequest="false"
src="http://localhost/lyndon/store_input.php"
type="http"
onerror="debug.write( this.getErrorString() )">


If you do this you would have found strange messages in the debugger which tell you that it's trying to parse a <body> tag. This is because nothing was getting returned by your script (when using GET variables) and therefore the server was throwing together a quick HTML page by adding HTML tags. My server was doing that at least.

In addition, if something does go wrong with you PHP script, it is set to write text straight out, which the client can't parse. Instead you should have it build up an XML document that contains the error message, so instead of


if (!$fp = fopen($filename, 'a')) {
print "Cannot open file ($filename)";
exit;
}


... you might have it write out an error node:


<error msg="Cannot open file (myFilename.log)" />


Take care,

Antun

lyndonwong
04-14-2003, 02:47 PM
Hi Antun,

Thanks much for the tip. Everything works now that I modified SimpleFormPost.lzx to set the request type t = 'POST' for setQueryString(q,t).

jricardo
08-04-2005, 07:57 AM
does the <windowtext> tag exist???

strange!!!

amirimran
01-26-2006, 09:20 AM
1. Changed <windowtext> to <edittext>
2. <view name="formFields" datapath="dsSendData:/">
3. used d.setQueryType("POST");
I am using LPS-3.1.