0

I have a Form Where users Upload Images and enter title for them and then submit. In order to accomplish it, I integrate the AJAXUPLOADER, it doesn't allow multiple Image Upload at once, but one by one, I don't have problem with it.

On Success, it returns the uploaded file name, what I do, I insert a hidden field containing the Image file name as value. And I insert a text field for users to enter the title.

basically, I wanted to have an array with the multiple file names and title so I put this code:

 input type="text" name="images[][title]" input type="hidden" value="'+response+'" name="images[][url]" 

It works perfectly, but there's a problem. the array structure builds with the above code:

[images] => Array
    (
        [0] => Array
            (
                [title] => Ferrari
            )

        [1] => Array
            (
                [url] => d2339e1d8da95e811c4344eaef226d09.jpg
            )

        [2] => Array
            (
                [title] => Ferrari
            )

        [3] => Array
            (
                [url] => 714208a8c1b819a548a258c33e311e98.jpg
            )

    )

However, I want them in this format:

  [images] => Array
    (
        [0] => Array
            (
                [title] => Ferrari,
                [url] => d2339e1d8da95e811c4344eaef226d09.jpg
            )

        [1] => Array
            (
                [title] => Ferrari,
                [url] => 714208a8c1b819a548a258c33e311e98.jpg
            )

    )

Any quick help will be appreciated!

2 Answers 2

3

By declaring indices in your input, that array will automatically be built properly for you, no need to do any fancy array merging.

<input type="text" name="images[0][title]" />
<input type="hidden" value="'+response+'" name="images[0][url]" />

<input type="text" name="images[1][title]" />
<input type="hidden" value="'+response+'" name="images[1][url]" />

So on and so forth :) If you're using a PHP loop to declare your inputs, it's as simple as this.

<? for($i = 0; $i < 2; $i++) { ?>
<input type="text" name="images[<?= $i ?>][title]" />
<input type="hidden" value="'+response+'" name="images[<?= $i ?>][url]" />
< } ?>

I hope this helps make your life easier!

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

3 Comments

for example: user upload a file an array with images[0][title] images[0][url] will be inserted into the form, and then he uploads further more, everytime the counter will be 0.
That's the purpose of the loop... You're changing the key every time for each set of images. If you force the value of the index, you can depend on that value being sent along with the request. By using [] as the key, you're going to increment the values independently, according to your initial issue. Please trust that the implementation that I've provided does indeed solve the problem, and it's been tested and confirmed that it's working.
All you need to do is keep an incrementing value. If you have 5 keys, you know you have 5 keys, you don't have to confirm you have 5 keys to add the 6th, just increment $i by 1, using $i++. I'm really not sure what the problem is with the solution...
0

Name it titles[] and then combine them in PHP.

Example:

<?php
header("Content-type: text/plain"); //Display only
$urls = array(
    "http://example.com/",
    "http://example.com/images/"
);
$titles = array(
    "Example",
    "Example Images"
);
$images = array();
foreach ($urls as $key => $value) {
    $images[$key]["url"] = $value;
    $images[$key]["title"] = $titles[$key];
}

print_r($images);

4 Comments

Yes, However, you code is required some fixes, but it's the right approach!
I never wanted to accomplish like this, but I see no other options :)
My code works at it is (I've tested it). You may need to adjust it to match your input. But yes. How were you planning to accomplish it?
I was trying to achieve my goal on uploading, but that's truth, did not think of placing them in a separate arrays and merge them.. I thought it took more server resource.

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.