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.– RodrigoBorth
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.
– Jose Maximilian
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 pageresult.php
. the Success is the party that takes that return– RodrigoBorth
The only way you can switch pages by sending a $_POST is through a form.
– RodrigoBorth