Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

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 02-25-2011, 04:55 PM   PM User | #1
rpjd
Regular Coder

 
Join Date: Apr 2007
Posts: 123
Thanks: 1
Thanked 0 Times in 0 Posts
rpjd is an unknown quantity at this point
passing javascript array and var to php

I'm submitting data from a form to another page. Using the onsubmit event I'm generating a javascript array and var which I want to access both the array and var using $_POST. The array and var I'm assuming need to be passed as POST parameters. If so, in what format exactly, as they are not values within the submitting document?
rpjd is offline   Reply With Quote
Old 03-01-2011, 12:07 PM   PM User | #2
MarPlo
Regular Coder

 
Join Date: Mar 2011
Posts: 145
Thanks: 0
Thanked 20 Times in 20 Posts
MarPlo is an unknown quantity at this point
Hy
You can pass the Array easily with JSON format:
var aray = ["elm1", "elm2", ...];
Then, in php you can use json_decode() function.

About vars, I think you can put them in an JSON object
var vars = {"var1":"val1", "var2":"val2", ...};
Then, in php
Code:
$vars = json_decode($_POST['vars']);
foreach($vars as $k => $v) {
  $$k = $v;
}
MarPlo is offline   Reply With Quote
Old 03-01-2011, 12:18 PM   PM User | #3
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Another option is to create dynamically hidden textbox elements in the onsubmit function and attach them to the form before the form is submitted. The hidden textboxes can contain the values in the var and the array. Make sure the name of the hidden textboxes for the array values have the same name and have [] appended to the name so they are sent as an array to the php script.
bullant is offline   Reply With Quote
Old 03-01-2011, 01:15 PM   PM User | #4
rpjd
Regular Coder

 
Join Date: Apr 2007
Posts: 123
Thanks: 1
Thanked 0 Times in 0 Posts
rpjd is an unknown quantity at this point
My tabular form has rows
Code:
<tr>
<td><input type'checkbox' name='checkbox' value'x-y-z'></td>
<td>x</td>
<td>y</td>
<td>z</td>
<td><textarea name='text'>Text goes here</textarea></td>
</tr>
I originally had the textarea disabled to prevent users from editing the text. This however was preventing $_POST from reading the text. So now the textarea is not disabled, the text is now visible to $_POST, but the user can edit the text which I don't want. If there is no way of displaying and posting the text without preventing the user from editing the text, I might have to just display the text like x, y and z, and collect the text from a database instead. Unless there is any other solution?
rpjd is offline   Reply With Quote
Reply

Bookmarks

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 10:46 PM.


Advertisement
Log in to turn off these ads.