How to call a function on click

I am having a problem reinitializing some code.

I have a function from a plugin that I need to re-initialize on click. I started learning JS and PHP last month, so I'm still uncomfortable manipulating code that I haven't written.

Here is the code I need to reload. It was found in the PHP file:

/** $return FFGeneralSettings */
public function getGeneralSettings(){
    return $this->generalSettings;
}

public function register_shortcodes()
{
    add_shortcode('ff', array($this, 'renderShortCode'));
}

public function renderShortCode($attr, $text=null) {
    if ($this->prepareProcess()) {
        $stream = $this->generalSettings->getStreamById($attr['id']);
        if (isset($stream)) {
            $settings = new FFStreamSettings($stream);
            if ($settings->isPossibleToShow()){
                /*ob_start();
                include(AS_PLUGIN_DIR . 'views/public.php');
                $file_content = ob_get_clean();
                echo $file_content;*/
                ob_start();
                include(AS_PLUGIN_DIR . 'views/public.php');
                $output = ob_get_contents();
                ob_get_clean();
                return $output;
            }
        }
    }
}

      

I think I will be using Javascript to reinitialize the actions in the above code? Am I on the right track with this skeleton I made below?

$(document).ready(function(){
    $('a').on('click',function(){
        //does the magic happen in here?
        });
});

      

Thanks for any advice, I learned so much from you guys! Oh, also, I'm fine if the answer is in jQuery.

+3


source to share


2 answers


"I have a function from a plugin that I need to re-initialize on click."

Everything will depend on what you want this feature. Do you want it to display some results to show on your page? Do you want it to just run the server-side script? Both will require you to call a php page that will run this php code, which will probably be in the form of an ajax call, but what you do with the result will be different depending on your purpose.

To make an ajax call using jquery, do something like:



$(document).ready(function(){
    $('a').on('click',function(){

        // magic happens here!
        $.ajax({
            url: "yourfile.php",
            method: "get",
            success: function(result) {
                // do something with your result here
                console.log(result);
            },
        });

    });
});

      

It looks like your code will return something - so this code above should do something in this section success = function (result)

, which should probably put the result of the output in some element.

+2


source


If you are trying to reload / restart a PHP function from Javascript, you will need to create an API endpoint with which you can make an HTTP call from javascript, or perhaps do an old fashioned page refresh. If that doesn't make sense, you will learn more about the basics of HTTP requests and responses.



0


source







All Articles