How to access, in PHP, values passed as parameter using jQuery.post()

Asked

Viewed 32 times

0

How to individually access string values cores and array filmes on a PHP page?

var filmes = ["Mad Max", "Assassin's Creed"];

var cores = "cor_um=Azul&cor_dois=Preto";

$.post("autosave.php", {'_cores': cores, '_filmes': filmes});

1 answer

1

Using the tag $_POST

$filmes = $_POST['_filmes'];
parse_str($_POST['cores'], $cores);

To get each value of the sent arrays, just use the foreach

foreach($filmes as $filme) {
    print_r($filme)
}

I recommend you change the format of your variable colors to an array instead of query string

  • Jeferson, how do I take each value individually?

  • As you are passing them in an array, you need to traverse this array using foreach, I’ll edit with an example

  • If you send colors as an array, just do the same thing for the variable $cores

  • "cores" is a string is serialized from a form. This is just a MCV example.

  • Add an option for you to transform query string into array

  • Jeferson, if you can supplement your answer by showing how you do...

Show 1 more comment

Browser other questions tagged

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