Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Post Array data

Status
Not open for further replies.

pingit

MIS
Nov 24, 2003
37
0
0
NZ
Hello All

I have an ASP page that connects to an access db through IIS and displays the results of a query. There is Javascript on the page to verify user input and stores it in an array. I now want to pass the array to an asp page to input the data back into the database. I have tried and can pass a single array element and insert data but cannot get back to pass the next element. I have tried concatenating the array and passing in the querystring but im limited to 2052 chars. I have heard you can post the array from a form but how do get the java array into the post part and how do you get the elements into an array on the asp page

any help would be much appreciated

Thanks
 
Hi

With "plain" JavaScript, you will need a [tt]form[/tt] to POST.
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]postit[/color][teal]([/teal]where[teal],[/teal]what[teal])[/teal]
[teal]{[/teal]
  [b]var[/b] theform[teal]=[/teal]document[teal].[/teal][COLOR=darkgoldenrod]createElement[/color][teal]([/teal][green][i]'form'[/i][/green][teal])[/teal]
  [b]with[/b] [teal]([/teal]theform[teal])[/teal] [teal]{[/teal]
    action[teal]=[/teal]where
    method[teal]=[/teal][green][i]'post'[/i][/green]
  [teal]}[/teal]
  document[teal].[/teal]body[teal].[/teal][COLOR=darkgoldenrod]appendChild[/color][teal]([/teal]theform[teal])[/teal]
  [b]var[/b] theinput[teal]=[/teal]document[teal].[/teal][COLOR=darkgoldenrod]createElement[/color][teal]([/teal][green][i]'input'[/i][/green][teal])[/teal]
  [b]with[/b] [teal]([/teal]theinput[teal])[/teal] [teal]{[/teal]
    type[teal]=[/teal][green][i]'hidden'[/i][/green]
    name[teal]=[/teal][green][i]'thearray'[/i][/green]
    value[teal]=[/teal]what[teal].[/teal][COLOR=darkgoldenrod]join[/color][teal]([/teal][green][i]'[/i][/green][lime][i]\t[/i][/lime][green][i]'[/i][/green][teal])[/teal]
  [teal]}[/teal]
  theform[teal].[/teal][COLOR=darkgoldenrod]appendChild[/color][teal]([/teal]theinput[teal])[/teal]
  theform[teal].[/teal][COLOR=darkgoldenrod]submit[/color][teal]()[/teal]
[teal]}[/teal]

[COLOR=darkgoldenrod]postit[/color][teal]([/teal][green][i]'[URL unfurl="true"]http://example.com/thepage.asp'[/URL][/i][/green][teal],[[/teal][green][i]'one'[/i][/green][teal],[/teal][green][i]'two'[/i][/green][teal],[/teal][green][i]'three'[/i][/green][teal]])[/teal]
In my example I simply concatenated the array values, so the server-side script will have to split it. Or you can put each value in a separate [tt]input[/tt], in which case no extra step is required by the server-side script :
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]postit[/color][teal]([/teal]where[teal],[/teal]what[teal])[/teal]
[teal]{[/teal]
  [b]var[/b] theform[teal]=[/teal]document[teal].[/teal][COLOR=darkgoldenrod]createElement[/color][teal]([/teal][green][i]'form'[/i][/green][teal])[/teal]
  [b]with[/b] [teal]([/teal]theform[teal])[/teal] [teal]{[/teal]
    action[teal]=[/teal]where
    method[teal]=[/teal][green][i]'post'[/i][/green]
  [teal]}[/teal]
  document[teal].[/teal]body[teal].[/teal][COLOR=darkgoldenrod]appendChild[/color][teal]([/teal]theform[teal])[/teal]
  [b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal],[/teal]l[teal]=[/teal]what[teal].[/teal]length[teal];[/teal]i[teal]<[/teal]l[teal];[/teal]i[teal]++)[/teal] [teal]{[/teal]
    [b]var[/b] theinput[teal]=[/teal]document[teal].[/teal][COLOR=darkgoldenrod]createElement[/color][teal]([/teal][green][i]'input'[/i][/green][teal])[/teal]
    [b]with[/b] [teal]([/teal]theinput[teal])[/teal] [teal]{[/teal]
      type[teal]=[/teal][green][i]'hidden'[/i][/green]
      name[teal]=[/teal][green][i]'thearray['[/i][/green][teal]+[/teal]i[teal]+[/teal][green][i]']'[/i][/green]
      value[teal]=[/teal]what[teal][[/teal]i[teal]][/teal]
    [teal]}[/teal]
    theform[teal].[/teal][COLOR=darkgoldenrod]appendChild[/color][teal]([/teal]theinput[teal])[/teal]
  [teal]}[/teal]
  theform[teal].[/teal][COLOR=darkgoldenrod]submit[/color][teal]()[/teal]
[teal]}[/teal]
Or you can do it with AJAX. Search for "POST" in forum1600 for more.


Feherke.
 
Hello Feherke

Thanks for the example. I have a couple of questions to get my head around this.
1.In the second example does the html address = the "where" variable and the array = the "what" variable.
2.Again in the second example does the function append seperate variables for each array item and if yes how do you get asp to seperate them at the other end?

Sorry if this is difficult to understand this is my first crack at this stuff

Thanks
 
Hi

pingit said:
1.In the second example does the html address = the "where" variable and the array = the "what" variable.
Yes, the parameters are the same as in the first code.
pingit said:
2.Again in the second example does the function append seperate variables for each array item
More precisely, appends separate [tt]form[/tt] elements for each array item.
pingit said:
and if yes how do you get asp to seperate them at the other end?
No idea, I do not know ASP. The array POSTed by my second code in PHP would be available as an array :
PHP:
[COLOR=darkgoldenrod]print_r[/color][teal]([/teal][navy]$_POST[/navy][teal]);[/teal]
Code:
Array
(
    [thearray] => Array
        (
            [0] => one
            [1] => two
            [2] => three
        )

)
I would expect to happen something similar in ASP too. Try it. Or ask for more in forum333.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top