0

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)?

10
  • is this javascript? and if it is, what library are you using? Commented Jun 27, 2011 at 17:26
  • @Praneet Sharma: Have you tried anything yet or are you just wondering? Commented Jun 27, 2011 at 17:28
  • 1
    i am not sure what you are doing. that does not look like proper jQuery at all... Commented 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 php Commented Jun 27, 2011 at 17:35
  • @Neal: yes it does, what are you talking about? Commented Jun 27, 2011 at 17:35

1 Answer 1

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.

http://api.jquery.com/jQuery.ajax/

Sign up to request clarification or add additional context in comments.

6 Comments

$.ajax({ type: "POST", url: "store.php", data: textnode&headerone, success: function(){ alert( "saved data" ); } });
would that be enough or would I have to encode in json first?
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']
@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.
Nothing you just said isn't true =) I was responding to the suggestion of data: textnode&headerone
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.