Receive variable in php and call Ajax function passing this variable

3

I'll explain this post better, I do not think it was clear on the issue. Today, I have a page where I search for a certain term and the result is then presented to me, a search AJAX Live Search .

The page looks like this:

Thecodeislikethis,IhaveafieldinputwiththeIDsettoTermo,see:

<inputtype="text" class="form-control required" id="Termo" value="" onkeyup="sendRequest();" placeholder="Informe uma palavra">

The calling function looks like this:

// BUSCA DINÂMICA
function sendRequest() {
    var Termo = document.getElementById('Termo').value;
    if (Termo.length > 2) {
        var url = "pBuscaRamais.php?Termo=" + Termo;
        ajax.open('GET', url, true);
        ajax.onreadystatechange = ajaxListener;
        ajax.send(null);
    }
}

function ajaxListener() {
    if (ajax.readyState == 1) {
        // INSERIR GIF DE CARREGAMENTO
    } else if (ajax.readyState == 4 && ajax.status == 200) {
        ramais.innerHTML = ajax.responseText;
        // REINICIALIZANDO A FUNÇÃO APÓS O RETORNO
        // DETALHES scripts.js
        _toggle();
    }
}

What I'm trying to do, searching for a term from another page, passing the input to the iBuscaRamais.php page, retrieving the variable, calling a function, and returning the search on the iBuscaRamais.php page.

The page for this call is this:

Thecodehtmlofitislikethis:

I'mretrievingtheTermvariableintheiBuscaRamais.phppage,theretrievedvariableiswiththecontenttyped,theproblemishowtocallthefunctionthatIcandothesearchandreturntome,inthatsearchitisnotaAJAXLiveSearch

I'mretrievingthevariableandtryingtopassthevariablelikethis:

if(isset($_POST['Termo'])){$Termo=$_POST['Termo'];echo"
function loadDoc($Termo){
}
"  
}

And the called function is this:

function loadDoc() {
    var Termo = "";

    console.log(Termo);

    var url = "pBuscaRamais.php?Termo=" + Termo;
    ajax.open('GET', url, true);
    ajax.onreadystatechange = ajaxBuscaIndex;
    ajax.send(null);
}

function ajaxBuscaIndex() {
    if (ajax.readyState == 1) {
        // INSERIR GIF DE CARREGAMENTO
    } else if (ajax.readyState == 4 && ajax.status == 200) {
        ramais.innerHTML = ajax.responseText;
        // REINICIALIZANDO A FUNÇÃO APÓS O RETORNO
        // DETALHES scripts.js
        _toggle();
    }
}

My console does not display any error messages, but does not work.

    
asked by anonymous 21.07.2016 / 23:45

3 answers

2

In PHP do so: if (isset($_POST['Termo'])) { $Termo = $_POST['Termo']; echo '<script> loadDoc("'.$Termo.'") </script>; } And in your JS:

function loadDoc(meuTermo) {
var Termo = meuTermo;

console.log(Termo);

var url = "pBuscaRamais.php?Termo=" + Termo;
ajax.open('GET', url, true);
ajax.onreadystatechange = ajaxBuscaIndex;
ajax.send(null);
}

function ajaxBuscaIndex() {
if (ajax.readyState == 1) {
    // INSERIR GIF DE CARREGAMENTO
} else if (ajax.readyState == 4 && ajax.status == 200) {
    ramais.innerHTML = ajax.responseText;
    // REINICIALIZANDO A FUNÇÃO APÓS O RETORNO
    // DETALHES scripts.js
    _toggle();
}
}
    
25.07.2016 / 19:56
1

Brother, since you are sending the data via form I recommend the following

data : $("form").serialize()

So all the data in the form goes to the variable data and then you can work with ajax ...

I hope to have helped, hug.

    
22.07.2016 / 00:16
0

I think I understand what you want now. see if this is it, I did a simple test and it worked here.

<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      document.getElementById("demo").innerHTML = xhttp.responseText;
    }
  };
  xhttp.open("GET", "response.txt", true);
  xhttp.send();
}
</script>
<!DOCTYPE html>
<html>
<body>
<form method="post" action="">

<input type="text" value="" placeholder="Nome*" maxlength="100" class="form-control required" name="Termo" />
</br >
<input type="text" value="" placeholder="Entreposto*" maxlength="100" class="form-control required" name="busca_ramal[Entreposto]" />
</br >
<input type="submit" value="BUSCAR" class="btn btn-success btn-block" />
</form>

<div id="demo"><h2>Aqui é o response</h2></div>
<?php
if (isset($_POST['Termo'])) {
$Termo = $_POST['Termo']; 
 echo '<script type="text/javascript">
            loadDoc();
        </script>'; 
}
?>


</body>
</html>

response.txt

Response ok !

Receive the variable:

    function sendRequest() {

    /*var Termo = "RECEBER A VARIÁVEL PHP AQUI";
    coloque um id="id_input" no input*/
    var Termo = document.getElementById("id_input").value;
    var url = "pBuscaRamais.php?Termo=" + Termo;
    ajax.open('GET', url, true);
    ajax.onreadystatechange = ajaxListener;
    ajax.send(null);    
    }

See if that's it, anything says.

    
22.07.2016 / 01:12