1

I am in a problem. I have a page with several text field with same class name and different attribute values. I would like to create an array which having unique key with joining the text field attribute values. And pass it to a PHP page using ajax.

My problem is

I can create the array successfully. But when I try to send the data through ajax, it become empty and the post array did not contain that value. But when I try to console the array, then it has the value

My JQuery code is

$(document).on('click', '#mybutton', function () {

    var c_array = [];
    $('.class').each(function(){

        var val1 = $(this).attr('attr1');
        var val2 = $(this).attr('attr2');
        var val3 = $(this).attr('attr3');
        var key = val1+'_'+val2+'_'+val3;
        var no = $(this).val();
        c_array[key] = no;
        alert(c_array[key]);
    });
    console.log(c_array);
    var type = 'check_cookie';
    $.ajax({
        type: 'POST',
        url: 'action.php',
        dataType: 'json',
        async: false,
        data: { type: type, c_array: c_array },
        success: function (data) {
            console.log(data);
            if (data.msg !== '' && data.msg !== null) {
                window.location = 'new.php';
            }
            else {
                alert('error');
            }
        }
    });
});

What is the problem with this code? Please help me. Thank u all in advance....

1
  • provide your php code and html as well Commented Nov 29, 2014 at 5:30

1 Answer 1

1

Since you declared your c_array as an array, you should assign it numeric indices (or keys as per your codes). If you need to work with an associative array (i.e. non-numeric indices), you can either instantiate c_array as a javascript object, a json data structure or an array of javascript objects.

Take a look at this fiddle or the stack snippet below.

// an array with numeric indices works.
// some_array has 2 entries.
var my_array = new Array()
my_array[0] = 'Test 1'
my_array[1] = 'Test 1'
var my_type = 'My Type 1'
var my_data = {type: my_type, some_array: my_array}
console.log(my_data)

// an array with non-numeric indices doesn't work.
// some_array is an empty array.
var my_array2 = new Array()
my_array2['a'] = 'Test 2'
my_array2['b'] = 'Test 2'
var my_type2 = 'My Type 2'
var my_data2 = {type: my_type2, some_array: my_array2}
console.log(my_data2)

// an array of objects work.
// some_array has 2 entries.
var my_array3 = new Array()
my_array3[0] = {'a': 'Test 3'}
my_array3[1] = {'b': 'Test 3'}
var my_type3 = 'My Type 3'
var my_data3 = {type: my_type3, some_array: my_array3}
console.log(my_data3)

// an object works.
// some_array has 2 entries.
var my_var_obj = {a: 'Test 4', b: 'Test 4'}
var my_type4 = 'My Type 4'
var my_data4 = {type: my_type4, some_array: my_var_obj}
console.log(my_data4)

// a JSON data structure works.
// some_array has 2 entries.
var my_var_obj = $.toJSON({'a': 'Test 5', 'b': 'Test 5'})
var my_type5 = 'My Type 5'
var my_data5 = {type: my_type5, some_array: my_var_obj}
console.log(my_data5)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://jquery-json.googlecode.com/files/jquery.json-2.2.min.js"></script>

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

1 Comment

Thank you for this information. I changes the index as numeric and it works just fine... :)

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.