pass an array of objects to another page

Asked

Viewed 1,289 times

2

            scoreBoard.push({
                acertosOuErros: acertoOuErro,
                multiplicacao: resultado
            });     

This is my array made in javascript, I wanted to pass this array of objects to another page for me to treat it (I would assemble a table of results), I saw that there are several methods (via cookie, via url, via form in Hidden), but I wanted to know to send it via ajax (I think it’s the best way, isn’t it?). could you teach me? I want to learn.

I saw a tutorial elsewhere, but I did not understand very well.
This Array (dataString) is the example mounted by the site.

Javascript:

$(document).ready(function(){
    $('a').on('click',function(){
    var dataString = {
        'id':'1',
        'name':'peter parker',
        'age':'unknown',
        'role':'spiderman'
    }
    $.ajax({
         url: "result.php",
         data: {'data':dataString}, 
         type: "POST",
         cache: false,
         success: function(response){
         alert("ok");
         $('#test').html(response);
        }
     });
   })
})

HTML:

<a href="javascript:void(0);">Click Me</a>
<div id="test"></div>

result php.

$data = ($_POST['data']);

foreach($data as $d){
    echo stripslashes($d);
}

In this example, the array is sent via ajax to result.php and a string is returned to the previous page? And how do I send my object array to another page? I would assemble a table with that array sent from the previous page...

  • I think you just need to serialize your array and send it normally by ajax. Replace the {'data':dataString} by its already serialized array.

  • i wanted to tinker with this array on another page (result.php), in that example the array is returned to the same page that sent the array. has something to do with that Success function? I wanted to redirect to the result.php page and stir the data there.

  • in ajax, you will not change page, it will send your array() for a $_POST, then it will execute everything you have done inside the result.php in the background. When he is finished he will take as return the echo you give on the page result.php. the Success is the party that takes that return

  • The only way you can switch pages by sending a $_POST is through a form.

1 answer

-1

I think where you have date: {'data':dataString}, change dataString to the name of your array...

  • this example worked, but the doubt is another: I wanted to tinker with this array on another page (result.php), in that example the result is returned again to the same page that sent the array. what I need to modify in the code to do this ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.