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

4
  • just echo it in the js code Commented Aug 10, 2016 at 9:38
  • @gbestard i tried to echo it using var a='<?php echo $eid;?>';but it echos it as a string and displays <?php echo $eid;?> Commented Aug 10, 2016 at 9:41
  • Possible duplicate of How to access PHP variable in Javascript code? Commented Aug 10, 2016 at 9:50
  • Possible duplicate of How can I get query string values in JavaScript? Commented Aug 10, 2016 at 9:50

7 Answers 7

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>
Sign up to request clarification or add additional context in comments.

1 Comment

This is very much overkill, the window.location already has the querystring as a property in window.location.seach
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.

7 Comments

if this is used in a .js file, it probably won't work, since by default only php-files are interpreted by php
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 ...
the title does not imply an external JS file... this answers the question that is posed, no more no less.
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).
Let's just say currently you are the only one telling me it's a wrong assumption because OP never mentions it
|
1

Example

    <?php
    $id = 1;
    ?>
    <script>
    var MyId = '<?php echo $id; ?>';
    </script>

Comments

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

Comments

0

You can use it as a javascript variable globally.

<script>
    var eid = "<?php echo $eid;?>";
</script>

Comments

0
  <script>
    var yourId = '<php echo $_REQUEST['eid']; ?>';
  </script>

Comments

0

Try This:

var eid = window.location.search.substring(1);

1 Comment

@Randy, this is return the last value form last segment of url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.