javascript - Passing Session Variable from php to JS -
good day community. using jquery gets content within td cell. passing content via variable file. i'm trying figure out best way grab session variable , seem have hit rut. below brief pseudo code along js/jquery.
edit/update
correction post below well. here manageusers.inc.php
<?php // load 404 page if file accessed directly if(!defined('include_check')) die(header("http/1.0 404 not found")); // load 404 page if page accessed users or qa agents if($_session['role_id']<2 or $_session['role_id']==5) die(header("http/1.0 404 not found")); ?> <script src="/resources/js/getbusinessunit.js"></script> <script type="text/javascript" src="../../resources/js/user.js"></script> <div id="main"> <div class="container"> <h1>manage users</h1> <h2>this page allows add , update authorized users</h2> </div> <div class="container"> <div style="margin-left:auto; margin-right:auto; text-align:center;"> <span id="user"> <form name="insertuser" action="<?php echo htmlspecialchars($_server['php']);?>" method="post"> <div style="margin-top:0px; margin-left:auto; margin-right:auto; text-align:center;"> <fieldset> <legend class="legend1"><h2>  user info  </h2></legend> <div style="padding-top: 5px;"> <input type="text" name="uname" class="textinput1" id="uname" value="" style="width:200px;" maxlength="200" tabindex="2" placeholder="user name" /><br /> <input type="text" name="fname" class="textinput1" id="fname" value="" style="width:200px;" maxlength="100" tabindex="3" placeholder="first name" /><br /> <input type="text" name="lname" class="textinput1" id="lname" value="" style="width:200px;" maxlength="100" tabindex="4" placeholder="last name" /><br /> <div style="float:left"><input type="checkbox" name="status" id="status" value="yes"/>enabled </div> </div> </fieldset> <fieldset> <legend class="legend1"><h2>  company info  </h2></legend> <div style="padding-top: 5px;"> <div class="input-field"> <?php require $adm_pg.'selectrole'.$ext; ?> </select> </div> <div class="input-field"> <?php require $adm_pg.'selectbusinessunit'.$ext; ?> </div> <span id="deptcontainer"></span> </div> </fieldset> </div> <?php require $adm_pg.'adminbuttons'.$ext; ?> </form> </span> </div> </div> <div class="container"> <?php require $adm_pg.'displayusers.inc.php'; ?> </div> </div> <?php require 'db/manageusers.db.php'; require 'db/logaction.db.php'; ?>
here user.js file
$(window).on('load', function() { $('.user').on('click', function() { var id = $(this).html(); $.get("/includes/adminpages/update_user.inc.php?selection_id=" + id, function(data) { $('#user').html(data); }); });
});
well there 2 options here.
the php file send session variable part of
data
returned in$.get
call.if session value doesn't change during page's execution, other's have suggested, doing
echo $_session['bus_id'];
if you're having trouble getting #2 work, here do. reduce html file (i'll leave out header stuff, assume correct in file):
<div> <?php print_r($_session); ?> </div>
that give session values. way can see if value want there. if is, work fine:
<script> var myvar = "<?php echo $_session['bus_id']; ?>"; </script>
with problems this, important thing simplify them. remove not needed page, , add code in small steps, can identify problem happening. it seems problem you're having isn't problem think you're having :)
edit:
based upon updated information, problem you're expecting run php code in file never parsed php (user.js
). if need access php variable in javascript function defined in external .js file, have pull variable javascript in main php file , send parameter whatever code you're executing in external .js file, or declare globally , ensure it's visible external code. example, using parameter solution:
index.php
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="myfunctions.js"></script> <script> $(document).ready(function(){ var bus_id = "<?php echo $_session['bus_id']; ?>"; setup_binds(bus_id); }); </script>
myfunctions.js
function setup_binds(bus_id) { $(".user").on("click",function(){ alert("bus_id is: " + bus_id); }); }
in other words, need think how design pages include multiple files this.
Comments
Post a Comment