Forum Moderators: open

Message Too Old, No Replies

Get POST data with Javascript

         

Muppet9010

1:05 pm on Jan 8, 2010 (gmt 0)

10+ Year Member



I've been reading around and read somewhere that when getting POST data using javascript the below 2 should have the same effect:

var score = document.edit_cup.score_1_1.value;
var score = document.["edit_cup"].score_1_1.value;

But for me the first one works and the second one just causes none of the javascript on my page to work.

The reason i ask this is i have the below javascript code (The webpage is created by php via echo, hence the escaping of the double quotes):


function submitCupDetails(cup_id, match_count) {
var form = document.createElement('form');
form.setAttribute('method', 'post');

//submit each score field, regardless of enty or not
for(var i=0; i<match_count; i++) {
for(var j=1; j<=2; j++) {
var score = document.edit_cup.[\"score_\" + i + \"_\" + j].value;
var hiddenField = document.createElement('input');
hiddenField.setAttribute('type', 'hidden');
hiddenField.setAttribute('name', \"score_\" + i + \"_\" + j);
hiddenField.setAttribute('value', score);
form.appendChild(hiddenField);
}
}

//submit the form so that the page reloads the new data
document.body.appendChild(form);
form.submit();
}

I have around 40 text fields each labelled in pairs with the names of 'score_0_1', 'score_0_2', 'score_1_1', 'score_1_2', 'score_2_1', ect and i am needing to get the values of each to submit.

Fotiman

1:54 pm on Jan 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld!
var score = document.["edit_cup"].score_1_1.value;
should be
var score = document["edit_cup"].score_1_1.value;
Just need to remove that extra .

You can access object properties using the dot notation:

object.property

or array:

object["property"]

Muppet9010

2:36 pm on Jan 8, 2010 (gmt 0)

10+ Year Member



thats done the trick, cheers :)