1

I want to execute a function in php that returns a string but I couldn't find a clear explanation.

I would really like if you could answer for this example.

this is my php file, hello.php:

<?php
  function HelloWorld()
  {
    return "Hello!";
  }
?>

and let's say this is my javascript in my HTML file

MainPage.html

<body>
  <script type="text/javascript">
    sayHello()
    {
      alert(/*This is where I want My php to call*/);   
    }
  </script>  
  <input type = "submit" onclick="sayHello()"/>
</body>

What I am asking is how can I get my php and use functions in it succesfully

4
  • alert('<?php echo HelloWorld()?>'); Commented Jun 4, 2015 at 10:11
  • 1
    You will need to call the server-side page (hello.php) and execute the function, then do whatever you wish with the response. Commented Jun 4, 2015 at 10:12
  • I am sorry but this doesn't work. I have a couple of jquery codes. And this breaks other codes somehow.. Commented Jun 4, 2015 at 10:14
  • 1
    @Viral that won't work because the MainPage.html isn't a server-side document. hence MainPage - .html Commented Jun 4, 2015 at 10:17

2 Answers 2

1

Do one thing, change MainPage.html to MainPage.php.

So, you MainPage.php will look like

<?php
  function HelloWorld()
  {
    return "Hello!";
  }
?>
<body>
  <script type="text/javascript">
    sayHello()
    {
      alert('<?php echo HelloWorld()?>');
    }
  </script>  
  <input type = "submit" onclick="sayHello()"/>
</body>

This will work for you.

Sign up to request clarification or add additional context in comments.

Comments

0

Without changing file extensions:

Make your file speak by echo at the end of file hello.php

<?php
  function HelloWorld()
  {
    return "Hello!";
  }
  echo HelloWorld();
?>

Change MainPage.html to call ajax from hello.php like,

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $('#clickme').click(function(){
            $.ajax({
                url : 'hello.php',
                type : 'GET',
                dataType:'html',
                success : function(data) {           
                    alert(data);
                }
            });
        });
    });
</script>
<body>
  <input type="button" id="clickme"/>
</body>

Also check that both of your files is in the same directory, i used popular JavaScript framework called jQuery for ajax call.

2 Comments

can you explain how do I choose which function do I call from php file?
that you have to decide on your php file, see above, i have called HelloWorld() by placing echo at start, you can call any other function, there are many methodologies, please go through some pages here, you will get more idea.

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.