How to pass variables Javascript to PHP

How to pass variables Javascript to PHP

A debate proposal does few days read in a Blog on Design Web exceeds what programming language has more future, to read this
In the first place a reflection, and is that we remember that Javascript is executed of the side of the client whereas PHP does of the side of the servant, reason why really what we are doing with the first technique that appears next it is to print in screen the variable in €œformat text€.

In the second and third example we will see as really keeping the data in the variable from the side of the servant, for it exist several forms to make it, as for example through INPUT, AJAX, keeping it in a data base MYSQL or a variable of Session. In this way we will be able to pass variables Javascript to PHP.

Javascript php
Copyright: 123RF

  • METHOD 1: passage of variable €œin way text€
    <script> bar variableJS = €œcontained of the variable Javascript€; </script> <? php $PHPvariable = €œ<script> document.write (variableJS) </script>€;
    I throw €œPHPvariable = €œ. $PHPvariable;? >

    This last code can to be €œdeceptive€, since it is not PHP that really prints the value of the variable but again Javascript, although is certain that sometimes very concrete it can be to us very useful.
    Therefore PHP does not store the value, but it is really Javascript that interprets it in our navigating Web, since PHP long before executed in time in the side of the Web server.

    The solution then happens because the event Javascript sends to a request Web to PHP that causes a click to a page PHP, and that ends up passing the parameters through POST or GET assigning them to the corresponding variables thus.

  • METHOD 2: passage of variable JS to PHP through form
    The Web server in the previous cases cannot store the variable of any of the ways while the code is executed in the side of the client, therefore we solved it of the following way:
    <? php I throw €œ<script language='javascript'> bar la_cantidad;
    	la_cantidad=prompt (€˜It introduces the amount€™, 1);
         </script> €œ;
    //Ya we have captured the variable with Javascript
    I throw €œ<form action=$_SERVER [PHP_SELF] method=post name=enviar>
                  <input type=hidden name=var_php></form>€;
    I throw €œ<script language='javascript'>
                  document.enviar.submit ();
    </script>€;? >

    With these lines of code the file php is called to itself, passing through form the variable Javascript €˜la_cantidad€™.

  • METHOD 3: passage of variable JS to PHP through a variable of Session
    jQuery (€˜#div_session_write€™) .load (€˜session_write.php? session_name=new_value€™);
    In session_write.php it cases out:
    session_start ();
    if (isset ($_GET [€˜session_name€™])) \ {$_SESSION [€˜session_name€™] = $_GET [€˜session_name€™];}? >

Another interesting article that can interest to you is How to insert HTML in php.

it comments

If you liked this article or she served as help comp¡rtelo please to you
A greeting,
By “scar Carrillo