1

I am not quite sure why have I not been able to call a function from say File1 to File2 . I have used the require_once also .

Eg :

File1.php

<?php
function Test()
{
alert("Hello");
}
?>

File2

<?php
require_once("File1.php");

My code 
?>

<script type="text/javascript">

My code....

$("#email_summary").click(function() {
     <?php    
     Test();   
     ?>                            
});
</script>

Could you please let me know my mistake ? Thanks guys :)

4
  • you are calling php function in javascript. add <?php test(); ?> Commented Jan 18, 2013 at 5:52
  • PHP is server side and javascript is client side. You can't directly call a PHP function from javascript. Commented Jan 18, 2013 at 5:52
  • You mix different language. You cant call Test() only call it at a client side. Commented Jan 18, 2013 at 5:53
  • You cannot mix call by client side's click. what you want to do in php's function Test? Commented Jan 18, 2013 at 5:54

1 Answer 1

3

you can not call php function by JavaScript like you did

$("#email_summary").click(function() {

     Test();                               
});
</script>

should be

$("#email_summary").click(function() {

    <?php echo Test();   ?>
});
</script>

and

function Test()
{
return 'alert("Hello")';
}

edit: if you want to make it work like your code you can do this by

File1.php

<?php function Test() { ?>

    alert("Hello");

<?php } ?>

and than just include like

<?php    
    Test();   
 ?>   
Sign up to request clarification or add additional context in comments.

11 Comments

@Justin sorry you need echo too
This will never work, How are you mixing server side function with client side language.
It worked fine . What if I add echo instead of alert ? will it work ?
@Justin for discussion come to php chat room chat.stackoverflow.com/rooms/11/php
I meant instead of alert("Hello") I replace it by echo ("Hello") will it work ? will the echo statement work inside javascript ??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.