Reputation: 817
I'm having trouble re-initializing some code.
I have a function from a plugin that I need to reinitialize on click. I started learning JS and PHP last month so I am still uncomfortable with manipulating code I have not written.
Here's the code that I need to reload, It was found in a 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'm thinking I would use 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've learned so much from you guys so far! Oh, also, I'm fine if the answer is in jQuery.
Upvotes: 3
Views: 102
Reputation: 1022
"I have a function from a plugin that I need to reinitialize on click"
Everything will depend on what you want this function to do. Do you want it to produce some output to show on your page? Do you want it to just run a server side script? Both will require you to do a call to a php page that will run that php code, which will probably be in the form of an ajax call, but what you do with the result will differ depending on your goal.
To do 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 that success = function (result)
section, which will probably be to put the output result into some element.
Upvotes: 2
Reputation: 26902
If you are trying to reload/rerun a PHP function from Javascript you will need to create an API endpoint that you can make an http call to from javascript or perhaps do an old fashioned page refresh. If that doesn't make sense to you look into the fundamentals of http requests and responses a bit more.
Upvotes: 0