0

http://192.168.21.189:8096/attend/supervisor/view_employee_attendence.php?eid=24

i want to access eid=24 in javascript is there any way to access that... in php we can access it using

$eid=$_REQUEST['eid'];

so i was wondering that is there any way i can access that in my javscript... i want to use the value of eid to fetch data from mysql db using ajax

iqra
  • 115
  • 1
  • 12
  • just echo it in the js code – gbestard Aug 10 '16 at 09:38
  • @gbestard i tried to echo it using var a='';but it echos it as a string and displays – iqra Aug 10 '16 at 09:41
  • Possible duplicate of [How to access PHP variable in Javascript code?](http://stackoverflow.com/questions/22876503/how-to-access-php-variable-in-javascript-code) – NDM Aug 10 '16 at 09:50
  • Possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Jakumi Aug 10 '16 at 09:50

7 Answers7

1

in your template, echo the value in a javascript variable:

var id = '<?php echo $eid; ?>';

The quotes are not needed when you echo an integer.

NDM
  • 6,731
  • 3
  • 39
  • 52
  • if this is used in a .js file, it probably won't work, since by default only php-files are interpreted by php – Jakumi Aug 10 '16 at 09:44
  • well, the title says, he wants to access a php variable from js, in a now deleted comment he says, he's using an "external" js file, so a few words should be said how to adapt this. In theory I would agree, that his problem description is insufficent ... – Jakumi Aug 10 '16 at 09:47
  • the title does not imply an external JS file... this answers the question that is posed, no more no less. – NDM Aug 10 '16 at 09:49
  • Let's just say, you had certain reasonable assumptions which turned out to be wrong. For example never did he say, that he was producing js from php, nor that he was inlining js. It's also quite apparent, that he's not a professional, so he would put your code in the js file (not assuming, comment on the question confirms). – Jakumi Aug 10 '16 at 09:53
  • Let's just say currently you are the only one telling me it's a wrong assumption because OP never mentions it – NDM Aug 10 '16 at 09:56
  • He posted a comment on a now deleted answer, that he is indeed using an external js file. Don't want to bully or impose, sorry ;o/ – Jakumi Aug 10 '16 at 09:57
  • You should comment on the question asking for additional info if you think this is needed. – NDM Aug 10 '16 at 10:01
1

You can get the part after ? in URL using window.location.search and you have to manually parse it in JavaScript. Similar question with the solution you need: https://stackoverflow.com/a/901144/1608594

Community
  • 1
  • 1
carbolymer
  • 1,439
  • 1
  • 15
  • 30
1

Example

    <?php
    $id = 1;
    ?>
    <script>
    var MyId = '<?php echo $id; ?>';
    </script>
Vishnu Bhadoriya
  • 1,655
  • 1
  • 20
  • 28
1

Please use below javascript function to fetch querystring value in javascript.

<script>
function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

// query string value
var eid = getParameterByName('eid');
</script>
Rahul Patel
  • 5,248
  • 2
  • 14
  • 26
  • This is very much overkill, the window.location already has the querystring as a property in `window.location.seach` – NDM Aug 11 '16 at 12:49
0

You can use it as a javascript variable globally.

<script>
    var eid = "<?php echo $eid;?>";
</script>
Gulmuhammad Akbari
  • 1,986
  • 2
  • 13
  • 28
0
  <script>
    var yourId = '<php echo $_REQUEST['eid']; ?>';
  </script>
0

Try This:

var eid = window.location.search.substring(1);
er.irfankhan11
  • 1,280
  • 2
  • 16
  • 29