0

So my php needs 2 values, operation => string and data => array. The following is the form (dynamically generated inputs) :

<form method="post" action="/operations.php">
        Title: <input type="text" value="valuehere" name="data[title]">
                    .
                    .
                    .
        Description: <textarea name="data[description]"></textarea><br>

        <button class="btn janitor_edit" type="submit">Edit Media</button>

                    <input type="hidden" value="operateMePls" name="operation"> 
        <input type="hidden" value="254" name="data[id]">
</form>

And now I have to create an array from all data[xyz] in the form, but I'm having trouble finding a way to do so. The closest I've come to was doing like so: link

I must store the array as key/values, no other way, so I can ajax_request = { operation: operation_input, data : input_array_data };.

Oh, and the form works as expected when submiting "normaly" trought POST.

2 Answers 2

3

If the form works by itself, then let jQuery take care of it converting the data into a form suitable for XHR for you.

data: $('#your_form').serialize();
Sign up to request clarification or add additional context in comments.

3 Comments

If I POST this string, how do I turn it back into an array/object on the PHP side of things? Do I have to manually parse it?
@Juventus18 — You look in $_POST, just as you would if you submitted the form normally. The point of serialize is that it uses the standard format so you don't need to use a custom parser.
yeah, this works. Silly me, even though I "tested" this, I don't know why I thought it wouldn't work...
1

I've used an object instead of an array. When you json_decode in PHP, pass "true" as your second argument, and the data will come out as an array. I think this is what you wanted. Please comment if you were looking for something different.

$(".janitor_edit").click(function () {
  var data = {};
  data.operation = $("input[name='operation']").val();
  data.data.id = $("input[name='data\\[id\\]']").val();
  data.data.title = $("input[name='data\\[title\\]']").val();
  data.data.description = $("input[name='data\\[description\\]']").val();
});

5 Comments

I forgot to mention that the form has dynamically generated inputs, so there's no telling what will be the names of each input.
this is a good answer, but named keys arrays like array['key'] work fine in JS
dunno if can be helpful, but I did this jQuery plugin to handle dynamically generated forms, thought the right way would be using .serialize, it may help. codereview.stackexchange.com/questions/13443/…
So you can do named keys. For the longest time I thought it auto cast to object because this doesn't work: var my_array = ["key" : "value"] I've adjusted my answer.
this links shows you're somehow right... it is possible to do var my_array; my_array['key'] = 'value'. But this is a way to 'turn an array into an object' infact the object['attribute'] = 'value' assignment is an Object feature. mojavelinux.com/articles/javascript_hashes.html

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.