1

I'm using the following code to create file upload inputfields and it works perfectly.

however, I need to rename them like so:

mytext[1]
mytext[2]
mytext[3]

etc etc...

so I did simply put var i = 0; and then I put the i++ in the code. when I alert the i I get 1,2,3 etc in the alert box so I know that works.

but this line is wrong and I cannot figure out how to use the i variable in this line:

<div><input type="file" name="mytext[i]"/></div>

and this is the entire code:

<script>
$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID
    var i = 0;

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            i++
            $(wrapper).append('<div><input type="file" name="mytext[[i]]"/></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
</script> 

and the HTML code:

            <div class="input_fields_wrap">
    <button class="add_field_button">Add More Photos</button>

    <div><input type="file"  name="mytext[]"/></div>
</div>

could someone please advise on this?

Thanks

3 Answers 3

2

You need to concatenate the value into the string, you can't just add i within the actual string value.

Also, not sure why you have double square brackets [[]], I think you just need one set.

Try this:

$(wrapper).append('<div><input type="file" name="mytext[' + i + ']"/></div>');

Here is a basic example.

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

Comments

0

Try something like this,

$(wrapper).append("<div><input type='file' name='mytext[["+i+"]]'/></div>");

Comments

0

You don't even need to add the "i". PHP Will automatically do this when you submit the forum. Unless you want to give them other names. The shorter your code is the easier it is to maintain.

1 Comment

How do you know the OP is using PHP?

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.