How to call function by onfocus and add a value in a php variable?

Asked

Viewed 69 times

0

I would like to call a JS function when I enter Focus in some html field! But the code does not call the function!! How to do?

Example:

        <html>
            <input type="text" name="tx_nome_usua" onfocus="minhaFunc(this.name)" value="" >
        <input name="tx_rg_usua" onfocus="minhaFunc(this.name)">
        </html>

        <script language="javascript" type="text/javascript">
        function minhaFunc(nome){
           switch(nome){
            case tx_nome_usua :
                <?php  $j = 1;?>
            break;
            case tx_rg_usua :
                <?php  $j = 3;?>
            break;
    alert(<?$j?>);
        }
        }
        </script>

It is possible to assign a value to a php variable in this way?

  • With your editing, the question has changed quite a bit...

  • 1

    Using PHP the way you put it, it will never run, because PHP runs on the server, that is, it runs when you load the page and not when the javascript function is called. For you to set a variable in php by javascript, you need to make an Ajax call, and within the php file called by ajax, you arrow what you need to set, bearing in mind that the PHP variables are not global, then you’ll need to save it in $_SESSION to be able to read elsewhere afterwards.

No answers

Browser other questions tagged

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