0

I want to get the input in blur, but I don't know if it's possible to do this in php, so im doing in javascript to convert in php after, but im not getting

<!DOCTYPE html>
<html>

<body>

<h4>
    Change the text of the text field
    ,and then click the button.
</h4>

<label for="domTextElement">Name: </label>
<input type="text" onclick="getValueInput()" onblur="getValueInput()" id="domTextElement">

<form action="teste2.php" method="GET">
    <p <?php //$_SESSION['id'] = echo "<script>document.getElementById('valueInput').innerHTML</script>"; 
        ?> id="valueInput"></p>
</form>

<script type="text/javascript">
    const getValueInput = () => {
        let inputValue = document.getElementById("domTextElement").value;
        document.getElementById("valueInput").innerHTML = inputValue;

    }
</script>
<?php

$id = $_GET['valueInput'];
echo $id;
?>
</body>

</html>
  • PHP runs server-side only so you can't detect the blur event using PHP, that's the sort of reason why JavaScript exists. If you want PHP to do something when the blur event happens, you need to get JavaScript to send a HTTP request to the server to trigger a PHP script to execute. – ADyson Jul 12 '22 at 10:38
  • imagine, can I get the variable in js and then convert to php? – Afonso Marques Jul 12 '22 at 10:42
  • 1
    "convert" is the wrong word to describe it, really. You can _send_ that data to PHP, via a HTTP request (just like in any other interaction between the browser and the server, such as submitting a form or loading a page). – ADyson Jul 12 '22 at 10:44
  • i want to do this without buttons... but if i refresh the page the variable becomes null again – Afonso Marques Jul 12 '22 at 10:51
  • Well yes of course it does, because web pages are stateless - variables don't persist between requests automatically. But you _could_ handle the blur event, and in the JS code tell the browser to navigate to a URL which includes the textbox value in the URL as a query parameter, e.g. `window.location.href = "yourpage.php?id=" + urlencode(inputval);` ...something like that. That would mean that `$id = $_GET['valueInput'];` ...etc in the PHP would then do what you're expecting. – ADyson Jul 12 '22 at 11:02

0 Answers0