0

i want to send an array to php via Ajax Json, but i cant do it, when i send it all i get on my server side is a csv string,

here is my jquery code where i create my array

$('#guardar_curso').click(function(){
    var respuestas = new Array();

    var a = 0;
    var b = 0;
    var c = 0;

    var last_hidden = $('body').find('input[type="hidden"]').filter(':last');
    last_hidden = parseInt(last_hidden.val()) + 1;
    var count = 0;
    for(var a = 0; a<last_hidden; a++){
        for(var b = 0; b<4; b++){

            c = $('body').find('input[name="resp['+ a +']['+ b +']"]').val();
                if(c == ''){
                    respuestas[count] = c; //ignore this

                }else{
                    respuestas[count] = c; 
                }
            count++;
            }
        }

and this next is my jquery code where is my Ajax call triggered from a submit button:

                $.ajax({
                        type:"POST",
                        url:CI.base_url + 'admin/guardar_curso',
                        data: curso_data + '&respuestas=' + respuestas,
                        dataType:"json",
                        success:function(response){
                            $.each(response, function(key, value){
                                salida = salida + value + "\n";
                            })  

curso_data is my serialized data and "respuestas" is my array

it's just part of my code, i hope it helps make my point

any help i can get plz, this has been driving me nuts for hours, i want to loop that array with a foreach loop in php, thanks...

1
  • for me you are sending the "data" parameter in a wrong way. You should use Json syntax for that: data: {curso_data: respuestas}, Commented Nov 14, 2012 at 2:24

1 Answer 1

1

You are tring to concatenate an array and text which doesn't work. You could send the array as a JSON string, and decode it when it is received.

data: curso_data + '&respuestas=' + JSON.stringify(respuestas),

Then in php:

 /* convert json to php array*/
$respuestas= json_decode($_POST['respuestas']); 

A simple test in php is print_r( $respuestas) and look at the returned array in browser console

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

1 Comment

this worked, thanks a ton, i had tried stringify function but in a wrong way, your answer worked as a charm! thanks again!!

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.