Receive data in JavaScript

1

I have a very vile doubt about JavaScript, but I believe you will understand me, since I am a newcomer to the language.

Next:

I created a page in html where the user will have 5 options of services to choose, right? Right!

When the user clicks on the chosen service it will be directed to the next page, which will be the same regardless of the user's choice. And this is where my doubt comes in.

I want to know how to show on the next page just the choice that the user has selected. I know how to do this in PHP, where I get with $_POST['nome_do_campo_html'] and I assign it to a variable, but I need to do this in JavaScript.

    
asked by anonymous 26.10.2016 / 00:11

2 answers

2

Get the URL using:

var url = window.location;

Then use split to sort by ? . Separates the domain from the parameters.

Then it makes a split in the result [1], now with & to have each of the parameters separated. More or less like this:

var parametrosDaUrl = url.split("?")[1];
var listaDeParametros = parametrosDaUrl.split("&");
    
26.10.2016 / 01:37
0

I relied a little on the answer from @SergioBarros.

This solution will, before taking the parameters of the URL, take the fragment of it, then pick up the list of parameters, and finally build a parameter object. All parameters will be saved to this object if the query ? exists.

In the end, to read the parameters of the URL simply index the object params , just as you index $_POST in PHP!

params["nome_do_campo_html"];
params.nome_do_campo_html;
var url = location.href;

// Ignora o fragmento da URL
var noFrag = url.split('#')[0];

// Pega a lista de parâmetros
var query = noFrag.split('?')[1];

// Cria um objeto
// para guardar os parâmetros
var params = {};

// Checa se a lista de parâmetros
// existe
if (query) {
    // Faz a separação dos parâmetros
    let offParams = query.split('&');

    // Vamos iterar os parâmetros!
    for (let param of offParams) {
        // Checa se o parâmetro não está vázio
        if (param.length) {
            // Pega a índice do '='
            let assignIndex = param.indexOf('=');
            let key, value;

            // Checa se o '=' existe
            if (assignIndex >= 0) {
                // Pega o nome do parâmetro
                key = param.substring(0, assignIndex);
                // Pega o valor (depois de '=')
                value = param.substring(assignIndex + 1) || true;
            } else {
                // O nome do parâmetro...
                // realmente pertence à @param.
                key = param;
                // O valor padrão será true
                value = true;
            }

            // Por fim guardamos o parâmetro
            // no objeto params
            params[key] = value;
        }
    }
}

Ah! And another alternative is to use the URLSearchParams interface to interpret the URL query and use the URLSearchParams#get method to read a parameter:

var noFrag = location.href.split('#')[0];
var urlQuery = noFrag.split('?')[1];

if (urlQuery) {
    var searchParams = new URLSearchParams(urlQuery);

    // Exemplo de uso !
    var myParam = searchParams.get('nome');
}

This interface is implemented in current browsers.

    
26.10.2016 / 11:46