0

Is it possible to get all values from a <form> and parse it to mootools/json, so that I can use it in an XHR-request like:

var req = new Request.HTML({
 method: 'post',
 url: 'xhr_request.php',
 data: {
  formdata:       $('inputform').getdata(),
  someotherdata:  'hello world'
 }
}).send();

At the moment I have only text-input fields. In the json I like to have the input-name or ID as the key for the value. e.g.:

<form id="inputform">    
<input type="text" id="name" name="name"/>
<input type="text" id="company" name="company"/>
</form>

//mootools script parsing above to this:

data: {
'name' : 'John Doe',
'company': 'Joeys LTD'
},
Karl Adler
  • 15,780
  • 10
  • 70
  • 88

1 Answers1

1

that works automatically if you pass the $('inputform') as data: - it will serialize it for you.

Dimitar Christoff
  • 26,147
  • 8
  • 50
  • 69
  • i found this meanwhile: var formstring = $('requestForm').toQueryString().parseQueryString(); var formjson = JSON.encode(formstring); is it what you meant? http://stackoverflow.com/questions/2166042/how-to-convert-form-data-to-object-using-mootools – Karl Adler May 17 '13 at 06:08
  • 1
    @abimelex You're overcomplicating things, just pass the `$('inputform')` as the `data` property, and Mootools will detect it's a form element and properly serialize it *internally itself*. – Niels Keurentjes May 17 '13 at 22:55
  • Thanks for the tipp, but I thin it will not work in my case, because I want to add same additional data to the json. Or is there a way to do so? (I updated my question to clarify it) – Karl Adler May 21 '13 at 06:27