How to Embed PHP Code in JavaScript

How do I embed PHP code in JavaScript?

If your whole JavaScript code gets processed by PHP, then you can do it just like that.

If you have individual .js files, and you don't want PHP to process them (for example, for caching reasons), then you can just pass variables around in JavaScript.

For example, in your index.php (or wherever you specify your layout), you'd do something like this:

<script type="text/javascript">
var my_var = <?php echo json_encode($my_var); ?>;
</script>

You could then use my_var in your JavaScript files.

This method also lets you pass other than just simple integer values, as json_encode() also deals with arrays, strings, etc. correctly, serialising them into a format that JavaScript can use.

How do I embed Javascript in PHP code?

Try use onclick on your Button

<input type="button" name="edit" id="edit" onclick="document.getElementById('oldnotes').disabled=false; document.getElementById('record').disabled=false; return false;">

I hope it helps.

how to insert php code inside javascript code

It's a much better practice to avoid mixing PHP with JavaScript. PHP is a server-side language and as a result, executes differently from the browser. It can work but usually leads to unexpected behaviour and for the most part, it's much cleaner to keep everything separate.

You can achieve passing data between PHP and JS using hidden inputs or DOM elements with data-* attributes, e.g.

<div id="php-data" data-id="<?php echo $foo; ?>"></div>

then in your jQuery

let id = $('#php-data').attr('data-id');

However, for your problem in particular (as it's an include). It's much much better to simply include the file on load using a class of hidden:

css:

.hidden {
display: none
}

html/php:

<div class="title">
<div class="part partpiano hidden">
<?php include_once 'piano.php'; ?>
</div>
</div>

Then in your JS:

$('button').click(function()
{
$('.part').removeClass('hidden')
})

How to put php inside JavaScript?

Try this:

<?php $htmlString= 'testing'; ?>
<html>
<body>
<script type="text/javascript">
// notice the quotes around the ?php tag
var htmlString="<?php echo $htmlString; ?>";
alert(htmlString);
</script>
</body>
</html>

When you run into problems like this one, a good idea is to check your browser for JavaScript errors. Different browsers have different ways of showing this, but look for a javascript console or something like that. Also, check the source of your page as viewed by the browser.

Sometimes beginners are confused about the quotes in the string: In the PHP part, you assigned 'testing' to $htmlString. This puts a string value inside that variable, but the value does not have the quotes in it: They are just for the interpreter, so he knows: oh, now comes a string literal.

How to embed php in javascript?

use " instead of ' like this:

var id = $("#data-1").val();
var url = "<?= base_url('home/alone/'); ?>" + id;
console.log(url)

How to insert php code inside javascript

If you ever need to pass data between the two. Put the PHP value into a hidden field. then read it like you did with bar container

Save the value from PHP:

<?php $cat = get_the_category(); $cat = $cat[0]; ?>
<input id="executeListValue" type="hidden"
value="<?php echo $cat->cat_name."-".wp_title();?>" >

read it in js:

var executeListValue = document.getElementById("executeListValue").value;
//...
autoExecuteList : {
cycleTime : GSvideoBar.CYCLE_TIME_SHORT,
cycleMode : GSvideoBar.CYCLE_MODE_LINEAR,
executeList : executeListValue
}

How to use php code in javascript

What you can do is pass the variable from outside that .js file, like this:

// index.php

<div class="row">
<div class="small-12 columns">
page content
</div>
</div>
<script>var test = '<?php echo $variable; ?>';</script>

and then reference that variable the same as if you defined it within that .js file (mind the potential scope issue).



Related Topics



Leave a reply



Submit