what am i missing?
i declare a variable in php:
<?php
$MyPHPStringVar = 'Hello World';
?>
i have a form
<form id="from1" action="someaction.php">
<button>xy</button>
</form>
i have a javascript function in which i want to retrieve the declared php variable, but if i print it (var javaScriptVar), it returns the full
"<?php echo $MyPHPStringVar; ?>"
what am i missing?
<script>
var javaScriptVar = "<?php echo $MyPHPStringVar; ?>";
document.querySelector('#from1').addEventListener('submit', function(e) {
var form = this;
e.preventDefault();
swal({
title: "Are you sure?",
text: javaScriptVar,
icon: "warning",
buttons: [
'No, cancel it!',
'Yes, I am sure!'
})
...
...
...
</script>
.phpfile or an external.jsfile? (<?php echo ...?>will only work in.phpfiles, unless you tell your server otherwise)var javaScriptVar = "Hello World";. PHP is processed before the page is sent to the browser, the javascript afterwards, so there's no mixing of the two in a situation like this."<?php echo $MyPHPStringVar; ?>"? My guess, the file does not end in.php, or you dont have php installed