Call Php Script From Tpl Onclick Button

Hello,

How to call php script from tpl onClick button?

I have created an addon and I need to run a php function on click, so I have created my addon.xml then my func.php in app/addons/my_addon/ with:

<?php/***************************************************************************
*                                                                          *
*   (c) 2004 Vladimir V. Kalynyak, Alexey V. Vinokurov, Ilya M. Shalnev    *
*                                                                          *
* This  is  commercial  software,  only  users  who have purchased a valid *
* license  and  accept  to the terms of the  License Agreement can install *
* and use this program.                                                    *
*                                                                          *
****************************************************************************
* PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
* "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
****************************************************************************/
if ( !defined('AREA') ) { die('Access denied'); }

function fn_my_function_name($productsid)
{

some functions

}
?>

Then in the template design/backend/emplates/addons/my_addon/hooks/products/update_product_name.pre.tpl:

{__("my_addon")}:

But I get a blank page, I think the problem is when I call the php function in update_product_name.pre.tpl template. Is this the correct way to call the function?

onClick="{$product_data.product_id|fn_my_function_name}"

What could be the problem?

Nicolas

You cannot call php functions by javascript. I suggest you to use pre controller ( call_requests.pre.php )

I have reviewed the documentation, but I am not sure how to achieve this.

Please check

https://docs.cs-cart.com/latest/developer_guide/core/controllers/pre_postcontrollers.html

For example, you can use My changes addon and following file (must be created)

app/addons/my_changes/controllers/frontend/call_requests.pre.php

Thanks eComLabs for your help.
so I have created app/addons/my_changes/controllers/frontend/call_requests.pre.php but unfortunately I can't make it to work, so instead I have created in my addon:
init.php
if (!defined(‘BOOTSTRAP’)) { die(‘Access denied’); }

fn_register_hooks(
‘update_product_pre’
);

?>

func.php

function fn_ng_auto_title_update_product_pre(&$product_data, &$product_id, &$lang_code, &$create)
{
if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {

{

  shell_exec("python3 /var/www/html/python/test.py " . $product_id);

}

}
}
?>

and in my update_product_name.pre.tpl file
{__("ng_auto_title")}:
When I click the button it works, but problem I have now is that when I click save button in the product page, it also executes this code, anyone know how I can separate the button I created and the save button in product page?

It is not clear what do you want to do. If you want to separate request by area (admin, customer) use the following code

if (AREA == 'C') { // customer
   ....
}

or

if (AREA == 'A') { // admin
    ....
}
Sorry, I'll explain better.
I have created a button in the admin product page (backend), this button has to trigger a the server side script.
[attachment=14896:2021-02-24 09_52_06-Products __ Products - Administration panel.png]
The script need to contain the product ID from the product page and run like this.
shell_exec("python3 /var/www/html/python/test.py " . $product_id);
Right now the button I created works and trigger the script, but the problem is when I click the save button on the upper right corner of a products (admin backend) page, it also trigger the script.
How can I make the RUN button I created to run the script, but not the save button? Basically, I need to make the RUN button unique.
I don't know if is possible to add an id something like:
in .tpl file
RUN
and in func.php file:
if (??? == 'my_run_button') { 
   ....
}

2021-02-24 09_52_06-Products __ Products - Administration panel.png

Yes, try to use custom dispatch value (e.g products.my_update)

And in the pre products controller use the following condition

if ($mode == 'my_update') {
    ... your code here...
}

Thank you very mush eComLabs, problem resolved. I mixed up some files in my app/addons/my_changes/controllers/backend/

Thanks again

You are welcome!