0

I know that PHP is only server-side and it's impossible to call directly a PHP-FUNCTION from a link.

But I can't use JavaScript \ jQuery \ Ajax

This is my code in main.php

function refreshgt2(){
    for($l=1; $l!=$max_cicle; ++$l ) {
        $data->query("INSERT IGNORE INTO `table` (`name1`,`name1`,`anothername`,`dog`) VALUES ('ME','".$img[$l]."','".$l."','".$hello[$l]."')");        
    }       
}

And the html,

<a href="#" >Do something</a>

I've just tried with this need a button that calls a php function but didn't work for me.

Someone can help me?

I need something that load the function only when I will press the link\button\image

3
  • So why can't you use Ajax? Is the php not the same origin?Even though you can try request via jsonp, like xxx.php?callback=cb&param=abc. Commented Sep 5, 2013 at 8:09
  • My link is: localhost/index.php?opz=zen the index.php call zen.php and zen.php call main.php. In this case a can use ajax? Commented Sep 5, 2013 at 8:58
  • Of course you can, you just need an ajax request, zen.php in this case.And it returns json/xml data, and you get it and invoke js function.As for zen.php calls main.php, that's server-side matter, require_once('main.php') ,run the function/method, and return result. Commented Sep 6, 2013 at 2:00

5 Answers 5

2

this should work. it gives a button, support multiple function call and is pure php + html

<form action="<?php $_PHP_SELF ?>" method="post">
<input type="hidden" name="function" value="refreshgt2">
<input type="submit">
</form>

<?php 
if($_POST!=null && array_key_exists('function', $_POST)){
    if(strcasecmp($_POST['function'],'refreshgt2')==0){
        for($l=1; $l!=$max_cicle; ++$l )
        {
            $data->query("INSERT IGNORE INTO `table` (`name1`,`name1`,`anothername`,`dog`) VALUES ('ME','".$img[$l]."','".$l."','".$hello[$l]."')");        
        }
    }else if(strcasecmp($_POST['function'],'some_other_function')==0){
        //do things
    }
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

But i cant's use a javascript\jquery\ajax

Then the answer is No, you can not. Only option is to link your button to a new page which will generate the PHP output. PHP itself does not know anything about what you do at client side and whether you have clicked a link, PHP functions cannot execute at that time since PHP has already done its job and gone.

Comments

0

If you dont mind a redirect

<a href="your_php_script.php" >Do something</a>

you_php_script.php

<?php

refreshgt2();

Comments

0

If you can't use javascript/ajax (why not? we are in 2013), you can't refresh part of the page on the fly. Your only chance is to call a page like

<a href="my-script.php">Do something</a>

and my-script.php then renders the page again after doing some stuff.

Comments

0

Use querystrings, set the href in your html to point to your page, and add a querystring parameter so that your php can decide which function to execute:

HTML

<a href='main.php?fn=some_function'>Do something<a/>

PHP

switch($_GET['fn']) {
case 'some_function':
    //call your function 
    break;
default:
    //Handle this
}

Otherwise, redirect to another php script like the other answers mentioned.

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.