PDA

View Full Version : Need Advice for Submitting Form Data


Shelby
02-20-2006, 11:43 AM
Scenario:

I need to display a modal window which has a form that the user will submit to a PHP file for processing. The submission will need to also include special processing instructions that the user cannot change.

Problem:

Really, I have implemented two different methods already, both of which seem to work just fine. What I really want is to ask on these forums if there are more elegant solutions. I will provide descriptions of both methods below.

Method 1:

Basically, I created a modaldialog, placed some edittext components on it with a submit and cancel button. I then overrode the open() and close() methods and created an onclose event. The new open() method accepted some initialization parameters and the close() method would send the onclose event with a modal close result (mrOk, mrCancel, stuff like that).

Basically, in order to use this I had to create a delegate to listen for the onclose and process it based on the modal result. It would also have to grab the information from the now invisible modaldialog before call a doRequest() with the data as parameters.

I don't like this method because the logic is all over the place. Also, delegates have to be cleaned up manually, especially if you want to use the same dialog in more than one place.

Method 2:

This time, I decided to use the <form> component to submit the information to my PHP file. This seemed like a great idea, except that I couldn't find a way to elegantly pass non-editable values as parameters along with the rest of the information. So, I had to extend a <baseformitem> that was invisible and pass the information to it by overriding the modaldialog's open() method.

This method has the advantage of being easier to maintain and reuse. Still, I can't help but wonder if I'm missing something. I appreciate any advice that can be given. I will post the code behind this second Method.

class: invisibleSubmitParam


<class name="invisibleSubmitParam" extends="baseformitem" focusable="false" visible="false">

<attribute name="text" setter="setText(text)"></attribute>

<attribute name="ontext" value="null"></attribute>

<method args="val" name="setText">
this.text = val;
if (this.ontext) this.ontext.sendEvent(val);
</method>
</class>


class: myForm


<class name="myForm" extends="modaldialog" width="640" height="480" closeable="true" title="Submission Form">

<method name="open" args="_dataset,val1,val2"><![CDATA[
//set the query type of the dataset to POST
_dataset.setQueryType("POST");
this.mainForm.submitData.setDataset(_dataset);
this.mainForm.someData1.setText(val1);
this.mainForm.someData2.setText(val2);
super.open();
]]>
</method>

<form name="mainForm">

<submit name="submitData"/>

<radiogroup name="place">
<radiobutton>Hawaii</radiobutton>
<radiobutton>Paris</radiobutton>
<radiobutton>Jamaica</radiobutton>
</radiogroup>

<edittext name="textField" text="some Text"/>

<invisibleSubmitParam name="someData1"/>
<invisibleSubmitParam name="someData2"/>

<button isdefault="true">submit
<method event="onclick"><![CDATA[

parent.submitData.submit();
classroot.close();
]]>
</method>
</button>

</form>
</class>


main.lzx


<canvas debug="true">

<include href="my_form.lzx"/>

<dataset name="test_ds" request="false" src="http://someurl/test.php" type="http"
ondata="Debug.write(this.data.serialize());"></dataset>

<myForm name="testForm"/>

<simplelayout axis="y" spacing="2"/>

<button onclick="testForm.open(test_ds, 'hello', 'goodbye');">Open Form</button>

<view datapath="test_ds:/results/post">
<simplelayout axis="x" spacing="5"/>
<text datapath="@key" resize="true"/>
<text text=":"/>
<text datapath="@value" resize="true"/>
</view>

</canvas>


backend PHP


<?php

// create a new XML Document
$doc = domxml_new_doc('1.0');

// create a root 'results' node
$root = $doc->create_element('results');
$root = $doc->append_child($root);

// Loop through all post variables
reset ($_POST);
while (list ($key, $val) = each ($_POST)) {

// Create a 'post' element
$post = $doc->create_element('post');
$post = $root->append_child($post);

// Add the post variable key and value as attributes to this element
$post->set_attribute('key', $key);
$post->set_attribute('value', $val);

}

//get completed xml document
$xml_string = $doc->dump_mem(true);

echo $xml_string;

?>


edited for minor fixes

zalex
07-30-2006, 06:19 AM
the second way seems better, but what if there was a list in your form (so the variable is an array) ?