Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
Is there any way to access a JS variable in PHP within the same file?

I have this html file which I am using to encrypt a string to sha1 hash. I receive the string like "1 | 2 | 3" from a javascript function (I haven't shown the function below), and I need to replace the | with - and then encrypt the modified string. Here's the code I have built so far -

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
    var test;
    var value;
    var message = "1 | 2 | 3";
    test = message.split(' | ');
    value = "<?php $final = test[0] . " - " . test[1] . " - " . test[2]; echo sha1($final);?>";
    alert(value);
</script>
</body>
</html>

I came to know about posting the value to another PHP file using HTML form, and accessing it through $_POST/$_GET - but my app won't allow for 2 files; hence I am trying to combine it all in one html file...


PHP has a in-built SHA1 encryption function, but javascript doesn't - the value variable in the above code is broken; how do I pass a variable to PHP from JS and back from PHP to JS? Again, I just want to modify (encrypt it) a JS variable using PHP and then set the PHP modified variable's value to a new JS variable. What should I do? Please guide :)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can't access to javascript variable like that. PHP is executed by the server and JavaScript by the client. You can create an ajax function in JS that call your PHP script.

Tutorial: http://www.w3schools.com/ajax/

JS

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
       xhttp.responseText // return from your PHP;
    }
  };
 xhttp.open("GET", "yourphp.php?variable="+yourjsvariable, true);
 xhttp.send();
}

PHP

<?PHP

return $_GET["variable"]; 

?>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda