I have various textnodes (p) and headers that I extract using jquery and store into an array :
var textnode = $(source).children("p:not(:has(img))").map(function () {
return $(this).outerHTML();
}).get();
var headerone = $(source).children('h1').map(function () {
return $(this).outerHTML();
}).get();
I need to take the textnode array and headerone array and pass it via ajax to a php script (which will consequently store it in mysql). Does serializeArray work in this case or could I use .stringify. Would I need to .decode this in php (version 5.3.4)?
-
is this javascript? and if it is, what library are you using?Naftali– Naftali2011年06月27日 17:26:06 +00:00Commented Jun 27, 2011 at 17:26
-
@Praneet Sharma: Have you tried anything yet or are you just wondering?netcoder– netcoder2011年06月27日 17:28:50 +00:00Commented Jun 27, 2011 at 17:28
-
1i am not sure what you are doing. that does not look like proper jQuery at all...Naftali– Naftali2011年06月27日 17:29:09 +00:00Commented Jun 27, 2011 at 17:29
-
I posted the slice of code that shows how I add elements into the array. @netcoder, I've been looking at jquery-json but I was wondering if decoding in php would also be necessary and how I would use the .ajax method to pass it to phpre1man– re1man2011年06月27日 17:35:10 +00:00Commented Jun 27, 2011 at 17:35
-
@Neal: yes it does, what are you talking about?Dereleased– Dereleased2011年06月27日 17:35:38 +00:00Commented Jun 27, 2011 at 17:35
1 Answer 1
If you send it via jQuery Ajax, it will be automatically serialized and will be available to your PHP as a $_REQUEST array variable.
answered Jun 27, 2011 at 17:28
bpeterson76
12.9k6 gold badges51 silver badges82 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
re1man
$.ajax({ type: "POST", url: "store.php", data: textnode&headerone, success: function(){ alert( "saved data" ); } });
re1man
would that be enough or would I have to encode in json first?
Dereleased
well, you'll need for data to look more like:
'textnode=' + textnode + '&headerone=' + headerone or {textnode:textnode,headerone:headerone}, but you don't have to serialize it as JSON. If you send it like that, you'll access it using $_POST['textnode'] and $_POST['headerone']bpeterson76
@Dereleased, according to the documentation jQuery .ajax will serialize a key->value array automagically. So, you're correct that you can pass a string, but according to the documentation you can also send a properly formatted array and it will do the rest.
Dereleased
Nothing you just said isn't true =) I was responding to the suggestion of
data: textnode&headerone |
default