Send value of a Javascript variable to PHP

0

I have searched a lot and done many tests but I am not able to send the value of a Javascript variable to a PHP variable.

My case is as follows: I have a real estate portal and would like to get the information (more precisely the real estate data or just the name of it) searched for by a Javascript function and move to a variable in PHP, in case the variable $title , which is the variable that regulates the title that will appear on each page in the browser.

The JS function that takes this information is this:

function getDadosImobiliaria(id) {
$.getJSON('../site/Control/controlUsuario.php', {type: 'selectDadosImob', imobiliariaId: id}, function(data) {
    if (data.tipo == "f") {
        var imobiliaria = data.nome+" "+data.sobrenome;
        $('.nomeAnunciante').html(imobiliaria);
    } else {
        var imobiliaria = data.sobrenome;

        $('.nomeAnunciante').html(imobiliaria);
    }

    $('#logoAnunciante').attr('src', 'imagensUpload/'+data.logo);
    $('.ruaNumero').html(data.rua+', '+data.numero);
    $('.bairro').html(data.bairro);
    $('.cidadeUf').html(data.cidade+' - '+data.uf);
    $('#fone').html(data.fone);
    $('.showCidade').html(data.cidade);
    $('.showCidade').attr('href', '/imobiliarias/'+data.cidade);
    $('.countTotalImoveis').html(data.count);

    if (data.creci != undefined) {
        $('.creci').html("CRECI: "+data.creci);
    }

}); 
}

How can I make to pass the real estate variable or any other variable to the PHP $title variable contained in another file and thus form the title pages of the real estate portal? By POST ? Ajax?

Thank you in advance!

    
asked by anonymous 18.04.2017 / 03:22

2 answers

0

After I got a better look at your code, I noticed that you were using jquery . Is there a way for you to send the variable in js to php the same way you are using ajax . However, I find it easier, in this case, to change the title of the document through jquery . From what I understood from the code the variable imobiliaria is the one you want to be in <title> . Then it would look like this:

 ...

    // Dentro da função mesmo...

    if (data.tipo == "f") {
        var imobiliaria = data.nome+" "+data.sobrenome;
        $('.nomeAnunciante').html(imobiliaria);
    } else {
        var imobiliaria = data.sobrenome;

        $('.nomeAnunciante').html(imobiliaria);
    }

    $(this).attr("title", imobiliaria); // aqui ele altera o título

...

This is probably going to work. If it does not, comment here that I'll find a way

EDIT

"houston we have a problem!"

Based on what you have put so far there is a variable called $title that is in a php file. What you want is to post a content retrieved through the file in js and pass that content to that variable, because supposedly, you would solve the problem of SEO and change <title> right ?. There is a way to do this, however, this will not solve your problem because php is interpreted before javascript and the only thing you will do is update a part of the code, but the page loaded on the server continues the same. This is the same as the previous example, but with a larger turn. See:

This is the php code ( resgataVariavel.php )

<?php 
    @$title = $_POST['title'];
    echo json_encode($title);
?>

This is index.php :

<html>
<head>
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
    var imobiliaria = "Imobiliária Stack Overflow";
    $.ajax({

        type: 'POST',
        url: 'resgataVariavel.php',
        cache: false,
        data: { 'title': imobiliaria },
        success : function(retorno){
                 var resultado = JSON.parse(retorno);
                 document.title = resultado;
           }
    })

});
</script>
<title></title>
</head>
...

Through ajax, I passed the contents of the variable imobiliaria to the variable $title . However, at the end, the title will be changed by javascript with document.title = resultado; .

What you need to do is get this content through php and throw it into the $title variable before the javascipt runs.

Understand, even if this variable of php $title is inside the <title></title> tag, and I use javascript to change it, it still would not work, because the server already "read" the content before it was changed by javascript.

A SOLUTION THAT CAN WORK ...

Try to do this:

 ... (código anterior)...

if (data.tipo == "f") {
        var imobiliaria = data.nome+" "+data.sobrenome;
        $('.nomeAnunciante').html(imobiliaria);
    } else {
        var imobiliaria = data.sobrenome;

        $('.nomeAnunciante').html(imobiliaria);
    }


    $.ajax({

        type: 'POST',
        url: 'resgataVariavel.php',
        cache: false,
        data: { 'titulo': imobiliaria },
        success : function(retorno){
            document.write(retorno);
        },
    })

   ...(resto do código)...

File resgataVariavel.php

<?php 
    @$title = $_POST['titulo'];
    echo "<title>".$title."</title>";
?>

Note that I put php to include <title></title> , I do not know if it will work for SEO, because the one who writes this file is javascript. But it is an option. I hope I have helped.

    
18.04.2017 / 20:00
1

I was able to get the value in another way, direct with php, and set the title, after too much try haha. But I think the question I was looking for, which was to pass the value from a variable js to php, was resolved in the answer above, where it actually worked, but, as I commented, on the return to the JS file, the page refreshes and all the loaded information goes away, leaving only the right title. But anyway, the main issue has been solved and thus, I close the question here. Many thanks to all and hope the answer given by Andrei will help many with the same doubt. Big hug!

    
26.04.2017 / 13:40