insert javascript variable in a php session

-1

function bimestre(event) {
        var targe = event.target || event.srcElement;
        var o = targe.getAttribute('value')
        o = parseInt(o);
        console.log(typeof(o));
        // alert(event.target.innerHTML);
        var bimestre = "<?php  $bim =(int) "<script> document.writeln(o); </script>";  Session::set("bolo", $bim); ?>";
        return o;
    }

I need to insert the variable value into a php session variable, please help

    
asked by اِبان ليماجي 10.01.2017 в 20:47
source

3 answers

0

You can not .
At least not directly. This is because both languages have different purposes. Javascript operates on the client side, and PHP on the server, if you are starting as a programmer this can be confusing, but then you will understand it better. However you can do some tricks to achieve what you want.
Javascript gets along very well with the DOM, so you can modify with JavaScript components of it and take its value with PHP. A better approach is to use Ajax to communicate the client with the server and the friend @guzgarcia has given you a good answer . If you do not want to use AJAX you can use the DOM itself:

documento.php

 <div ID="1">
  ...Contenido...
</div> 

php

<?php
 $searchNode = $xmlDoc->getElementsByTagName( "div" )->getAttribute('Valor'); ?>

Javascript

 var node = document.getElementsByClassName('div')[0];
 //Por aca 'insertas' con javascript contenido que es tomado por PHP directamente del DOM
 node.setAttribute('Valor', 'TuValor');
    
answered by 10.01.2017 / 21:49
source
1
  

I need to insert the variable value into a php session variable

To communicate backend with frontend you need to do it via HTTP requests. If your intention is to send a variable that you have in JavaScript to the backend (PHP), you only need to make a simple request.

The following example sends o via ajax:

var xhr = new XMLHttpRequest();
xhr.open('POST', 'server.php');
xhr.onload = function () {
  if (xhr.readyState === 4) {
    if (xhr.status === 200) {
      // se ha enviado la variable
    }
  }
xhr.send('bim='+o);

In server.php (or whatever your php file is called), you get this value and add it to the session:

if(isset($_POST['bim']) {
  $bim = (int) $_POST['bim'];
  $_SESSION['bolo'][$bim];
}

Of course you can do it with a normal GET request:

var o = parseInt(targe.getAttribute('value'));
window.location = '/server.php?bim=' + o;

And you get bim through $_GET :

if(isset($_GET['bim']) {
  $bim = (int) $_GET['bim'];
  $_SESSION['bolo'][$bim];
}
    
answered by 10.01.2017 в 21:29
0

Good morning

This is the line:

    var bimestre = "<?php  $bim =(int) "<script> document.writeln(o); </script>";  Session::set("bolo", $bim); ?>"

In my humble opinion, you can make a php variable "print" to say it in some way, some content in the statements js, but in the opposite direction, which is what you seem to want to achieve, it does not work like that

the document.writeln () statement, prints values in the browser, not in the php script, that would be insecure.

visualize how the elements are organized, browser, server, http communication between them, and this arrangement of elements that interact, you can call it "architecture", and in the architectural model, you will notice, that there is no possibility that the browser, define the instructions declared on the server in a natural way.

imagine, if I could alter the javascript code on the server so simply, and with that start executing code right and left, without a form of authorization or point of entry, I could have the server at my disposal. craving

can be done technically, but it is not the focus of this technology

    
answered by 11.01.2017 в 13:57