How to Put PHP Inside JavaScript

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 can you put PHP inside a Javascript?

To modify @Pedro Lobito's answer - you echo the value inside the js as follows:

<?php
$number = "1234";
?>
<script>
$({someValue: 0}).animate({someValue: <?php echo"$number";?>}, {
duration: 3000,
...

or alternatively:

    <?php
$number = "1234";
?>

<script>
var testValue = <?php echo"$number";?>;
$({someValue: 0}).animate({someValue: testValue}, {
duration: 3000,
...

or even

<input type="hidden" name="testInput" value="<?php echo"$number";?>" />

<script>
var testValue = $('[name=testInput]').val();
$({someValue: 0}).animate({someValue: testValue}, {
duration: 3000,
...

or a hidden div / span - but move the display:none to the external CSS sheet

<span style="display:none"  id="testSpan"><?php echo"$number";?></span>

<script>
var testValue = $('#testSpan').text();
$({someValue: 0}).animate({someValue: testValue}, {
duration: 3000,
...

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 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).

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 code into JavaScript?

You just missed the quotation around the <?php & semi-colon before ?>,

document.addEventListener("DOMContentLoaded", function(event) {
var gg1 = new JustGage({
id: "gg1",
value: "<?php echo 'Hello World!'; ?>", // This line is edited
min: 0,
max: 100,
title: "Target",

how to write php inside javascript

You can't do it like this. The correct way would be to make an ajax request to backend, and then have php delete the row.

Edit
here is some sample code for you

<script>
function delete_id() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
alert("deleted");
}
};
xhttp.open("GET", "/delete.php", true);
xhttp.send();
}
</script>

and the delete.php goes like

<?php
//write code for delete here
?>

Another point is that header("Location...") would redirect but in ajax, hence it is better to not use php redirect, but check in javascript and then use document.location for the redirect.



Related Topics



Leave a reply



Submit