0

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>
6
  • 2
    Is your JS inside a .php file or an external .js file? (<?php echo ...?> will only work in .php files, unless you tell your server otherwise) Commented Mar 25, 2021 at 16:34
  • Take a look at this post Commented Mar 25, 2021 at 16:34
  • brombeer it is all in a php file Commented Mar 25, 2021 at 16:37
  • View the source in the browser. Make sure it says 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. Commented Mar 25, 2021 at 16:43
  • 1
    if i print it (var javaScriptVar), it returns the full "<?php echo $MyPHPStringVar; ?>"? My guess, the file does not end in .php, or you dont have php installed Commented Mar 25, 2021 at 16:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.