0


I want to trigger a php function like this-

"If I click Here" (it can be a button or link)

// then this function would work:

<?php
function a(){
$b=10;
echo $b;
}
?>

How can I do that without ajax ? (I don't want to reload the page.You can show me way to put that php function inside javascript too if required)

-Thanks.

"Edit" : Would it be possible by php cURL ?

2
  • 4
    You cant. PHP runs on the server! Commented Dec 1, 2012 at 2:44
  • 4
    You must use AJAX, or reload the page. PHP and JavaScript in the client browser have no knowledge of one another. Commented Dec 1, 2012 at 2:44

3 Answers 3

3

It can't be done in PHP without reloading the page or using Ajax. The only alternative is converting that piece of PHP code to Javascript:

<script type="text/javascript">
function a()
{
  var b = 10;
  document.write(b);
}
</script>

<input type="button" value="Click Me!" onclick="a();">
Sign up to request clarification or add additional context in comments.

4 Comments

I want php. may be mixed with javascript but not total javascript.
Well, then you have to use Ajax or reload the page. Good luck.
No, you still need Ajax to call your PHP curl function. Then you might as well use Ajax to call you PHP function directly without using curl.
I see, yes if still requires ajax then no need to use curl.
1

PHP Code example.php:

<?php

function a()
{
    $b=10;
    echo $b;
}

if (isset($_GET['run_func']))
{
    a();
}

?>

HTML Code:

<a href='example.php?run_func=a'>Click Me</a>

1 Comment

Sorry I forgot to say that I don't wanna reload the page.
1

javascript is client side language (executes in browser), while PHP is server side (executes on the server). To execute the PHP a REQUEST has to be made on the server

This can be done by visiting a url, or by invoking ajax call (which does the request)

If you want to execute a function or a call you have to make a request to that php file.

So, you put code in my_php_fle.php and then do a request via link

 <a href="my_fle.php"> If I click Here </a>

However, you will have no result as you still have to make a call to php function.So, your my_file.php needs to look like this

<?php
function a(){
    $b=10;
    echo $b;
}
 a(); //executes your function
?>

Comments

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.