NO BUG: JavaScript Interaction Argument {echo:php_function} Not Working

Browser: Edge 129
OS: Windows
URL: N/A
Video: N/A

Hello, I am trying to pass dynamic data using {echo} to a JavaScript function using the Bricks interaction argument feature.

Passing the {post_title} works fine, but trying to pass a value using the {echo} dynamic data feature doesn’t work. Console.log shows the arguments passed using the {echo} function as null.

In the screenshot I have changed the name of the function for OPSEC purposes, but the function is used in different areas of the page in working on the page in Rich Text modules. The function returns a related post_id. I’ve added the function name to the “bricks/code/echo_function_names” hook as well.

Thanks in advance!

Jacob Hill

Hello,

thank you for your post. Sadly, I can not replicate your issue. I believe I’ve created the same setup as yours, but please double check:

I have a button, and I’ve set the same {echo:custom_function('aaa', 'bbb')} function as a Button content and as a JS function parameter.

In the Interaction, I’m calling JS function named myCall, that basically just outouts to console and returns whatever the parameter was:

function myCall(data = "Empty"){
    console.log(data);
    return data;
}

And on the PHP side, the custom_function looks like this:

function custom_function($first=null, $second=null)
{
    $message = "Input was:  {$first} {$second}";
    return $message;
}

So, do you see any difference? Can you try with my setup, does it work for you?

Thank you,
Matej

1 Like

Thank you for your response! This is a Bricks Builder Extras element, so I’m assuming that it is a bug with that element. I did try adding your simple PHP function, and it still didn’t output.

This can be closed out. Thanks again for the help!

Jacob Hill

Hi,
I tried this workaround for calling PHP function.
My structure is like this:
image

On the nested Div I have an interaction with Click trigger:

But when I open the page on front end, the PHP function is called for all the posts in query loop at the time of page is loaded. So it doesn’t wait until I click on the Div element.
Does anyone know what am I doing wrong please?

Hi @Shortik,

so, the issue is that the {echo:myPHPfun({post_id})} gets executed on page load, for all items? This looks correct to me because the PHP part is executed on the server side - so before page load.

What is executed on item click, is the interaction, which is JS part.

Please let me know if that’s not it.
Thanks,
Matej

You are right. I want to start PHP function when I click on the item but I don’t know how to do it so I tried this way. :slight_smile:

Hi @Shortik,

I see. You could try to add {post_id} as a data attribute to the element, and you don’t pass this parameter directly to JS function. You can then instead read the parameter (post ID) from the data-attribute.

Would that work for you?
Matej

The problem is something else. I have an query loop and want to call PHP function when I click on the specific element of the query loop.
A don’t know how to achieve this so I tried to workaround that by Interaction “javaScript (function)” but I didn’t know how it works and that’s not the correct way for me.
Now I know that my thougts were bad so it’s not problem of this topic. :slight_smile:

I’ll mark the topic as no-bug, as it’s not a bug.

If you want to execute some PHP code when you click on the element, you will basically have to:

  1. Run a JS function, when element is clicked
  2. Call a REST API or Ajax endpoint, where you will run a PHP function. This call should return something.
  3. You do whatever you need with the response.

For post ID and other dynamic data, you can store them inside element data- attributes.

You will need some custom coding for this.

Best regards,
Matej