Sending form array values to remote server via Xhr

You must Login before you can answer or comment on any questions.

Hi there, I'm encountering some problems trying to submit an original html form array to a remote web server PHP page via xhr POST method.

As an example, I need to submit the following form:

<form method="post" action="http://www.mysite.com/">
    <input name="option[123]" value="205">
    <input name="option[124]" value="409">
    <input name="otherparams" value="1">
...
</form>
so the only way I found to manage this is the following code:
var client = Ti.Network.createHTTPClient({
    onload : function(e) {
        Ti.API.info(this.responseText);
    },
    onerror : function(e) {
        Ti.API.error(e.error);
    },
    timeout : 100000  // in milliseconds
});
client.open("POST", url);
client.send({'otherparams': 1, 'option[123]': 205, 'option[124]': 409});
where the PHP page (with a print_r($_REQUEST)) returns
Array
(
    [otherparams] => 1
    [option] => Array
        (
            [124] => 409
            [123] => 205
        )
)
now the problem is, how can I dinamically build my object? As an example, any way I tryed to make something like
...
var mynumber1 = '123',
mynumber2 = '124';
client.send({'otherparams': 1, 'option['+mynumber+']': 205, 'option['+mynumber2+']': 409});
 
/* OR */
 
var myObj = {};
    myObj.otherparams = 1;
    eval("myObj.option["+mynumber+"] = 205;");
client.send([myObj]);
unfortunately failed...

Does anybody alredy realized how to do that? I absolutely cannot change the way my server manage requests parameters...

Thanks in advance to everybody...

— asked 9 months ago by Silvio Zennaro
3 Comments
  • I missed to specify my Titanium version: 2.1...

    — commented 9 months ago by Silvio Zennaro

  • in your last code block you've never defined a var called "mynumber" - you have "mynumber1"... ...maybe this is the problem... But there should also be an error message in the debug view.

    And please describe more detailed what you mean by "unfortunately failed..." - do you get an error? - does the php script returns wrong data? - what exactly failed?

    — commented 9 months ago by Soeren Damrau

  • Hi and thx for your reply... That was just an example, you can suppose no error was in the variable definition... The failure results in a generic syntax error, it seems that json (and more in general js) doesn't allow object keys defined as above.

    But the question is why it allows to define my object as:

    client.send({'otherparams': 1, 'option[123]': 205, 'option[124]': 409});
    and not as:
    client.send({'otherparams': 1, 'option['+mynumber+']': 205, 'option['+mynumber2+']': 409});
    in both ways I define two object attributes as string and my server accept it as well...

    Another way could by to write my own attributes string, specifying something similar to:

    otherparams=1&option[123]=205&option[124]=409
    but how to send this via Titanium Xhr object?

    — commented 9 months ago by Silvio Zennaro

Your Answer

Think you can help? Login to answer this question!