Receive variable in php and call Ajax function by passing this variable

Asked

Viewed 881 times

4

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

The page looks like this:

inserir a descrição da imagem aqui

The code is like this, I have a field input with the ID defined as Termo, see:

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

The function called is 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, search for a thermopo from another page, passing the input to the iBuscaRamais.php page, retrieve the variable, call a function and return the search on the iBuscaRamais.php page.

The page of that call is this:

inserir a descrição da imagem aqui

The code html of the same is so:

inserir a descrição da imagem aqui

I am recovering the variable Term on the iBuscaRamal.php page, the recovered variable is with the typed content, the problem is how to call the function that I can do the search and return me, in this search is not a AJAX Live Search

I’m retrieving the variable and trying to pass the variable like this:

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

And the so-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();
    }
}

On my console I am not presented any error message, but it does not work.

  • 1

    This $_POST is coming through an action?

  • Put the whole section, where the values are being passed to the variable $Term, if you have a silly mistake is easy to see...

  • Well let’s go in pieces, make a test there in the $_POST, and see if the variable is coming... It has not been done but... That way we will funnel the error... Give an echo where you are receiving the variable...

  • Hello@Magichat, thanks for the tip, yes, the variable is with value.

  • Less 1 problem, then the problem is in the function call or the function itself...let there... Appears some error ?

  • Not missing, sorry if wrong, initialize request? var ajax = new Xmlhttprequest();

  • Don’t put it in the post, but I am getting started @Magichat.

  • um... boot there tmb , to follow, every detail is relevant.

Show 3 more comments

3 answers

3


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();
}
}
  • Hello @Will Knippelberg, thanks for the tip, but the impression is that the function is not being invoked, in my js I put a console.log to display a message and not even this appears.

  • Take a look at the editing I did. The Error was in the invocation in your PHP

  • Hi @Will Knippelberg, I’m getting the following message on my console: iBuscaRamais.php:1 Uncaught Referenceerror: loadDoc is not defined, I have an idea of what it might be, but I couldn’t solve it.

1

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

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

so all form data goes to the variable data and then you can work it with ajax...

I hope I helped, hug.

0

I think now I understand what you are wanting. 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>

sponse.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 comments.

  • Hello @Magichat, tell me one thing, where you are passing the variable "Term" to the PHP search page?

  • @adventistapr Nussa bro, I think I need to wear glasses, this you put in miúscula letter.. kkk I will edit

Browser other questions tagged

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