0

I want to get an id from the query and display it in html.

I want to keep things in separate javascript files, because in the future, it will retrieve a json from php and will filter it based on some checkboxes, radio buttons, etc. and then display the result.

In this moment, I get a blank page, and the id value is not shown, so I'm doing something wrong.

http://localhost:10600/js1/index.php?id=5

index.php

<html>
<head>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script>
</head>
    <body>
        <div class="results"></div>
    </body>
</html>

<script type="text/javascript" src="config.js"></script>
<script type="text/javascript" src="display.js"></script>
<script type="text/javascript" src="sequence.js"></script>

config.js

function getId() {
    id = "<?php echo $_GET['id']; ?>";
}

display.js

function updateHtml() {
window.onload = function() {
    document.querySelector('.results').innerHTML = id;
}
}

sequence.js

function seq()
{
    getId();
    updateHtml();   
}
seq();
Nick
  • 331
  • 3
  • 14

3 Answers3

1

Please try the following: index.php

<html>
<head>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script>
</head>
    <body>
        <div class="results"></div>
    </body>
</html>

<script type="text/javascript">
var id;
function getId() {
    id = "<?php echo $_GET['id']; ?>";
}

</script>
<script type="text/javascript" src="display.js"></script>
<script type="text/javascript" src="sequence.js"></script>
Maths RkBala
  • 2,207
  • 3
  • 18
  • 21
1
function getParameters() {
    var url_params = window.location.search.substring(1); // Removing characters : ?
    var params = url_params.split("&");
    var res = {};

    for (var i = 0, c = params.length; i < c; i++) {
        var split = params[i].split("=");
        res[split[0]] = split[1];
    }

    return res;
}

Returns an object {param: value}

1

In index.php within body you can do like this one

<input type="hidden" name="hidden_id" id="hidden_id" value="<?=$_GET['id']?>">

In JS :

If you are using jQuery,then you should do like this one.

$(document).ready(){ 
    var strHiddenId = $("#hidden_id").val();
    $(".results").html(strHiddenId);
}
RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53