简体   繁体   中英

How to pass the value from url of php to .js

Hi I am new to javascript and I am trying to pass a value from Get in url of php to javascript. This is my code.

this is the url

someproject/index.php?roomId=2

index.php

room = $_GET['roomId'];

<input hidden id="room" value=<?=$room;?>>

script.js

I want to get the value from php and I want something like:

$('#room').getValueFromPHP();

but I do not know how to do it. Please help me.

This should do it:

PHP

<input type="hidden" id="room" value="<?=$room;?>">

JavaScript

$(function() {// make sure the document is ready.
    var url = $("#room").val();
});

In order to pass a value from PHP to JavaScript, you can use echo as follows:

<?php $room = $_GET['roomId']; ?>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM