I want to assign a php variable to javascript variable.
<?php
$type =1;
$form = ' <script>
var jquery_type " <?php = $type; ?>";
</script>';
?>
But in source page, it is showing like below,
var jquery_type " <?php = $type; ?>" ;
How can i get this php value ?
within php code :
$type = 1;
$form = '<script>
var jquery_type = "'.$type.'";
alert(jquery_type);
</script>';
echo $form;
If its js file.
var jquery_type = " <?php echo $type; ?>";
You did wrong there in your script, you can not use PHP tag inside PHP.
Unmanage single
(')
and double(")
quotes, which causesyntax error, unexpected '='
See this code
<?php
$type =1;
$form = '<script> var jquery_type = '.$type.'</script>';
echo $form;
?>
OR
if you want to just only assign and use $type
value into script than the below code is works.
<script>
var jquery_type = '<?php echo $type ?>';
<script>
from my experience the most universal solution always working, is this:
<html>
...
<input type="hidden" id="hiddenID" value="<?=$myPhpVar;?>" />
....
<script type="text/javascript">
var x = document.getElementById("hiddenID").value;
</script>
...
</html>
@RanaRubel You received negative votes on your question because, to a respected developer, it is just plain stupid. Applications in PHP, run on a server machine, has it's own variable scope that is totally different from JavaScript variable scope (run on your local computer, a different machine).
That's why you got negative votes.
What you're really trying to do is pass data, not variables. There are quite several ways to do so:
When rendering a PHP page, also render the data inside website body. You can, for example render a plain JavaScript name or an input field
<?php
$data = "Yolo";
echo "<script>var data = $data;<script>";
?>
or you can render an input field
<?php
$data = "Yolo";
echo "<input id='data' value='$data'/>";
?>
<script>
var data = document.getElementById("data").value;
</script>
This is rather crude and amateur way to pass data.
Cookies are even more advanced way of sending data, because they don't interfere with your application view-model. They're plain data. You can even easily read them in Developer Console.
<?php
$data = "Yolo";
setcookie("data", $data);
?>
<script>
var data = document.cookie;
</script>
Not as bad as rendering data in the body, but not used because Ajax calls are more professional, more versatile and provide more control of the data flow.
This is the most sophisticated method, it gives the programmer the most control over how data is retrieved, stored and sent. This is how modern front-end frameworks like Angular, React or Vue.js work.
<?php
$data = "Yolo";
echo $data;
?>
<script>
fetch('/your-script.php')
.then(response => {
var data = response;
})
.catch(error => console.log('Error loading data', err));
</script>
Keep in mind that fetch()
method only works in newer browsers. In older you'd need to use XMLHttpRequest
or use JQuery.
<meta>
tagThis is another way of rendering in a data, and it should be used for more document/web page information, and not to pass data that is business logic. This is slightly better than rendering in a raw JavaScript variable or an input, because they can mess up your html/css structure and then your layout. <meta>
tags aren't rendered so they can't mess your layout structure.
<?php
$data = 'Yolo';
echo "<meta name='data' content='$data' />";
?>
<script>
var meta = document.getElementsByTagName('meta')['data'];
var data = meta.getAttribute('content');
</script>