Reputation: 117
I'm having trouble with a post form in javascript. Here is my code...
function save() {
var form = document.createElement("form");
form.setAttribute('method', 'post');
form.setAttribute('action', '/quiz_score');
var i = document.createElement("input");
i.setAttribute('name', 'Score');
i.setAttribute('value', ""+score);
form.appendChild(i);
form.submit();
}
Can you see anything wrong with this code? I have a button setup to run this function onClick()...
<input type="button" name="finish" class="finished_button" onclick="save()"/>
Again, can anyone see any problems with this? When the button is clicked nothing happens. It appears it doesn't post and doesn't redirect.
Thanks for your help!
Upvotes: 1
Views: 485
Reputation: 12129
You need to attach the form to the body of the document by using document.body.appendChild(form);
function save() {
var form = document.createElement("form");
form.setAttribute('method', 'post');
form.setAttribute('action', '/quiz_score');
document.body.appendChild(form);
var i = document.createElement("input");
i.setAttribute('name', 'Score');
i.setAttribute('value', ""+score);
form.appendChild(i);
form.submit();
}
Upvotes: 1