-1

File structure is

functions/functions.php

function name($name){
   return $name;
}

HTML file
<?php include("./functions/functions.php") ?>
<script src="./js/custom.js"></script>
<button id="btn">click me</button>


JS/custom.js
$("#btn").click(function(){
    var name = <?php echo name("Alex");?>
    alert(name);
});

This is not working, can someone please tell me what am I doing wrong? I'm getting : 'Uncaught SyntaxError: Unexpected token < ' in the console.

--

Edit : Error disappears after I made the change 
var name = '<?php echo name("Alex");?>';
However, I'm still not gettion the alert.
John Powell
  • 12,253
  • 6
  • 59
  • 67
Siddharth Patel
  • 193
  • 1
  • 2
  • 15

2 Answers2

0

that should be: var name = "<?php echo name("Alex");?>";

Vince
  • 36
  • 3
0

You use JQuery selector but don't import jQuery lib to your code, and in java/script code use ready function, your code should be this :

<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
$().ready(function(e) {
    $("#btn").click(function(){
        var name = <?php echo name("Alex");?>;
        alert(name);
    });
})
</script>