Hi all.
I have the following form:
<form id="registrationForm">
<input id="forename" type="text"></input>
<input id="surname" type="text"></input>
<input id="emailAddress" type="text"></input>
<input id="password" type="password"></input>
<input id="confirmPassword" type="password"></input>
<input id="agreeToTerms" type="checkbox"></input>
<input id="clickToRegister" type="button"
onClick="javascript:SpeakAtServer(FormToJson('registrationForm'))"
value="Register"></input>
</form>
...with the following JavaScript...
FormToJson = function(sourceElement) {
var JsonToServer = new Object();
if (document.getElementById(sourceElement).childNodes) {
var form = document.getElementById(sourceElement);
var data = new Object();
for (var element = 0; element < form.length; element++) {
// Get the id e.g. firstname, surname, emailAddress etc...
var key = form.elements[element].getAttribute("id");
// Get the value e.g. Jane, Doe, jane@example.com etc...
var value = form.elements[element].value;
// Build a key/value pair e.g. "firstname":"Jane" etc...
data.key = value;
}
JsonToServer.formData = data;
}
return JSON.stringify(JsonToServer);
}
SpeakAtServer() {
// Generic Ajax function that sends the JSON to a PHP script which then
// returns a copy of what it received e.g. {"JsonData":"SomeData"}
}
I was hoping that I would get something like:
{"formData":{"forename":"Jane","surname":"Bloggs" etc...etc...}}
...but instead I got:
{"formData":{"key":"Register"}}
...which suggests my JavaScript is partially working, but is failing on my attempt to make Key/Value pairs. Unfortunately, I'm not knowledgeable enough on JavaScript/DOM to identify an error and would be grateful if a more seasoned programmer could let me know the error of my ways.
Thanks in advance for any help/tips/pointers. :)