2

I use Uploadifive and have modified a couple of things.

I made a form where people can select and input values as well as allowing people to select multiple numbers, like this:

 <input type="checkbox" id="checker_tiny" name="lid[]" value="1" />
 <input type="checkbox" id="checker_tiny" name="lid[]" value="2" />
 <input type="checkbox" id="checker_tiny" name="lid[]" value="3" />
 <input type="checkbox" id="checker_tiny" name="lid[]" value="4" />
 <input type="checkbox" id="checker_tiny" name="lid[]" value="5" />

I now want the form to be send to /upload/uploadifive.php

I have tried this by the following code:

    <script type="text/javascript">
//<![CDATA[
$(function() {
    // Initialiseer uploadifive
    $('#file_upload').uploadifive({
        'auto'              : false,
        'checkScript'       : '/uploadifive/Sample/check-exists.php',
        'onFallback'        : function () {
                                  window.location = '/home.php';
                              },
        'method'            : 'post',
        'queueID'           : 'queue',
        'uploadScript'      : '/upload/uploadifive.php',
        'onUploadComplete'  : function (file, data) {
                                  console.log(data);
                              }
    });

    // Hang een click-event aan de knop
    $('#subby').on('click', function () {
        // Werk formData van uploadifive bij met de betreffende waarden
        $('#file_upload').data('uploadifive').settings.formData = {
            'timestamp'   : '<?php echo $timestamp;?>',
            'token'       : '<?php echo md5('unique_salt' . $timestamp);?>',
            'projectnr'   : $('input[name=projectnr]').val(),
            'gebruiker'   : $('input[name=gebruiker]').val(),
            'gebruikerid' : $('input[name=userId]').val(),
            'mapId'       : $( "#mapId option:selected" ).val(), // moet dit niet val() zijn???
            'uploadbash'  : '<?php echo $uploadbash; ?>',
            'upload_reden' : $( "#upload_reden option:selected" ).val(),
            'todo'        : $("input[name=todo]:checked").val(),
            'lid'         : $('input[name=lid[]]:checked').val()
        };
        // Voer de upload uit
        $('#file_upload').uploadifive('upload');
    });
});
//]]>
</script>

At the moment all the data is being sent and it works, but only the multiple number checkboxes don't send the data correctly.

I get "undefined" on /upload/uploadifive.php

What is wrong?

1
  • side note: It's bad practice to have multiple elements with the same id. It will make problems for you in future. Commented Dec 15, 2013 at 13:42

2 Answers 2

1

Two fixes. First, as in Rajaprabhu Aravindasam's answer, you need to escape the special characters in the name. Second, you need to iterate over all the lid[] elements and add them separately into the formData object.

var formData = {
    'timestamp'   : '<?php echo $timestamp;?>',
    'token'       : '<?php echo md5('unique_salt' . $timestamp);?>',
    'projectnr'   : $('input[name=projectnr]').val(),
    'gebruiker'   : $('input[name=gebruiker]').val(),
    'gebruikerid' : $('input[name=userId]').val(),
    'mapId'       : $( "#mapId option:selected" ).val(), // moet dit niet val() zijn???
    'uploadbash'  : '<?php echo $uploadbash; ?>',
    'upload_reden' : $( "#upload_reden option:selected" ).val(),
    'todo'        : $("input[name=todo]:checked").val(),
};
$('input[name="lid[]"]:checked').each(function(i) {
    formData['lid['+i+']'] = $(this).val();
});
$('#file_upload').data('uploadifive').settings.formData = formData;
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, that works, but i get this as a result when i check 2 numbers: lid%5B%5D=13647&lid%5B%5D=13649 It are the right numbers, but i preffer to get them back as: 13647, 13649 Seperated with a comma, no other things. Is this possible?
I was worried that might happen. serialize() uses URL-encoding, which is appropriate for URL query parameters and x-www-form-urlencoded POST data. But FormData is multipart/form-data, and this needs each array element to be put into a different parameter, not combined like this.
Well, i now fixed this problem with str_replace in PHP. So thank you for the help!
I updated the answer with something I think will work automatically.
Another solution would be to join all the checkbox values in Javascript, and the explode them in PHP.
0

Because it contains two meta characters in it. Just escape it like below,

$('input[name=lid\\[\\]]:checked').val()

or

$('input[name="lid[]"]:checked').val()

Please read here to know more about meta-characters.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.