Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-31-2012, 02:07 PM   PM User | #1
russthebarber
New to the CF scene

 
Join Date: Dec 2011
Location: U.K.
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
russthebarber is an unknown quantity at this point
Send JSON array alongside HTML form

I have a basic form as follows:

Code:
<form action="myForm_submit.php" method="post" accept-charset="utf-8" name="myForm">
<input type = "text" name="firstName" />	
<input type = "text" name="lastName" />	

<input type="submit" value="Submit">
</form>
And I have a javascript array:

Code:
myArray = ["bananas", "apples", "pears", "peaches", "oranges"];
When I hit submit and send my form data I would like to send myArray in JSON format to my php submit file. How would I do that and how would I code the php $_POST = ..... for my JSON array?
russthebarber is offline   Reply With Quote
Old 08-31-2012, 03:59 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 806
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Code:
<?PHP
echo $_REQUEST["aray"];
?>

<form method="post" accept-charset="utf-8" name="myForm" onsubmit="addJSON(aray)">
<input type = "text" name="firstName" />	
<input type = "text" name="lastName" />	
<input type = "hidden" name="aray" >
<input type="submit" value="Submit">
</form>

<script>
myArray = ["bananas", "apples", "pears", "peaches", "oranges"];
function addJSON(el){
	el.value=JSON.stringify(myArray)
}
</script>

Last edited by DaveyErwin; 08-31-2012 at 07:04 PM..
DaveyErwin is offline   Reply With Quote
Old 08-31-2012, 06:54 PM   PM User | #3
russthebarber
New to the CF scene

 
Join Date: Dec 2011
Location: U.K.
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
russthebarber is an unknown quantity at this point
Thanks for the reply.
Code:
myArray = ["bananas", "apples", "pears", "peaches", "oranges"];
is just an example to simplify what I am trying to do. I actually have a large form with hundreds of fields to send. That's why I wanted to do it with one JSON array and not with hundreds of hidden input fields.
russthebarber is offline   Reply With Quote
Old 08-31-2012, 07:06 PM   PM User | #4
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 806
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Quote:
Originally Posted by russthebarber View Post
Thanks for the reply.
Code:
myArray = ["bananas", "apples", "pears", "peaches", "oranges"];
is just an example to simplify what I am trying to do. I actually have a large form with hundreds of fields to send. That's why I wanted to do it with one JSON array and not with hundreds of hidden input fields.
so uh well these fields ?
are not form elements ?
that the user types in ?
where do they come from ?

Last edited by DaveyErwin; 08-31-2012 at 07:09 PM..
DaveyErwin is offline   Reply With Quote
Old 08-31-2012, 07:30 PM   PM User | #5
russthebarber
New to the CF scene

 
Join Date: Dec 2011
Location: U.K.
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
russthebarber is an unknown quantity at this point
No, they are not form elements. They come from two large javascript arrays. I need to send all this data with the form. The form alone has about 100 elements at the moment and the arrays both have about 150 elements.
russthebarber is offline   Reply With Quote
Reply

Bookmarks

Tags
array, form, javascript, json, post

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:44 PM.


Advertisement
Log in to turn off these ads.