-3

I have some php $_SESSION vars that I am using in my site. like follows

$_SESSION[$select_grid_name."_id"];

The grid name is a name of the table been targeted. This all seems to work well but I have one issue that now I am trying to get the session var on a javascript call which is generated programatically for all grids

I want to do something like this

var grid_name = sender["id"]; //id assigned form grid call on select
var field_id = "<?php echo $_SESSION[?>" +grid_name +"]; ?>";

this errors because I am breaking up the php echo call. How can I call the php session var based on the javascript grid name?

hakre
  • 193,403
  • 52
  • 435
  • 836
Richard perris
  • 511
  • 1
  • 6
  • 15
  • var means variables ?? – Vinod VT Mar 03 '14 at 10:24
  • 1
    @Jaykishan – You can't use referenced links syntax in comments; only `[text](url)` syntax works, but you can edit (for a couple of minutes) and delete your own comments instead of posting multiple ones. – Quentin Mar 03 '14 at 10:29
  • 1
    @Jaykishan, he's trying to access a PHP variable with a JavaScript variable. It's a little different. – maček Mar 03 '14 at 10:30
  • He has edited his question, at very first he was not clear about what he wants\ – Jaykishan Mar 03 '14 at 10:32

3 Answers3

1

You simply cannot do that.

Client side (JavaScript) variables are not sent to the server like that.

If you want PHP to be aware of variables set in JavaScript, you should look into making an AJAX request. Popular libs like jQuery will make easy work of this for you.

maček
  • 76,434
  • 37
  • 167
  • 198
0

try this

in html

<input type="hidden" id="<?php echo $select_grid_name; ?>" value="<?php  echo $_SESSION[$select_grid_name."_id"];?>"/>

in jquery

var grid_name = sender["id"]; 
var field_id = $("#"+grid_name).val();
krishna
  • 4,069
  • 2
  • 29
  • 56
-2

Try this :

var grid_name = <?php echo sender["id"]?>
ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
Azrael_404
  • 426
  • 1
  • 6
  • 17
  • Because the grid_name is the name of the php $_SESSION var not the contents of the var. I am trying to get the contents of the $_SESSION named i.e grid_one where I know that grid_name = grid_one so the php SESSION would be $_SESSION["grid_one"] – Richard perris Mar 03 '14 at 10:31
  • my bad , so , haven't read enought. So try to echo your php var in the js declaration – Azrael_404 Mar 03 '14 at 10:34