-1

ANSWER: This is correctly closed as server-side/client-side. I'm using AJAX for this instead.

I need a woocommerce function to generate output when a span is clicked. This JS creates a collapse-able action in the div and attempts to take the div id and do get_attributes with it. Then it fills the div with it's output.

<script>
 var coll = document.getElementsByClassName("collapsible");
 var i;

 for (i = 0; i < coll.length; i++) {
   coll[i].addEventListener("click", function() {
     this.classList.toggle("active");
     var content = this.nextElementSibling;
     if (content.style.display === "block") {
       content.style.display = "none";
     } else {
       content.style.display = "block";  
       var uhh = content.getAttribute("id");
       var uhh2 = "<?php get_term_by('term_id', $uhh, 'pa_producer'); ?>";
       content.innerHTML = uhh2 ;     
       }
   });
 }
</script>

Also var uhh2 = "<?php echo $uhh; ?>"; doesn't work

I can't get anything returned from this php, either with echo or print_r or var_dump. The div class is "collapseable" if that makes it clearer how I'm handling the current element. When I use content.innerHTML = uhh then it returns the correct div id, so I know that this function is firing.

How am I improperly handing these vars?

Realce
  • 1
  • 1
  • You can't mix JS and PHP variables or call PHP from JS like this, they run at completely different times in the page request life cycle. PHP runs server side, and is done executing by the time the page is sent to the client. – jmoerdyk Dec 15 '18 at 00:15
  • Did you tried to enable and read PHP error log? – Teemu Dec 15 '18 at 00:15
  • https://stackoverflow.com/questions/3345457/how-to-put-php-inside-javascript – Ayoub Benayache Dec 15 '18 at 00:15
  • 2
    What does `get_term_by` do? Does it echo anything? Also, you're missing a quote. – Jeto Dec 15 '18 at 00:17
  • @jmoerdyk That is somethng we can't judge without knowing what `get_term_by` returns. – Teemu Dec 15 '18 at 00:19
  • @Teemu Then he's not showing all the necessary code, because it looks like they're trying to pass the JS variable `uhh` (which doesn't exist when the PHP executes) as the second parameter to the PHP function `get_term_by`. To send a JS variable to a PHP function would require some sort of AJAX call. – jmoerdyk Dec 15 '18 at 00:25
  • 1
    If you're expecting JS `uhh2 ` getting its value from the PHP function call, you're missing the [basics](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming). And we can't see any `echo`, `print_r` or `var_dump` in the code, hence it's not possible to say what is going wrong with your code. Please add the relevant parts of the `get_term_by`PHP function to the post. – Teemu Dec 15 '18 at 00:29
  • https://stackoverflow.com/questions/15461786/pass-javascript-variable-to-php-via-ajax – jmoerdyk Dec 15 '18 at 00:30
  • @jmoerdyk -- that's the point of this actually. I don't want these hidden divs to be populated when the page is rendered, as there are about 2000 items on the page. The way I'm using the ID means I don't do anything JS when PHP is running, I believe it's kosher. – Realce Dec 15 '18 at 00:31
  • 1
    Then where are you defining `$uhh`? Because it looks like you're trying to use the value from the previouse line of JS – jmoerdyk Dec 15 '18 at 00:33
  • Sorry if I've not been clearer... So the content div has a div class of "collapseable" and an id generated by PHP when the page is rendered. This div ID is the term_id of that particular producer, so I'm not passing $uhh between the two. The ID is read from the
    of the HTML.
    – Realce Dec 15 '18 at 00:34
  • @jmoerdyk I see your point maybe. $uhh is not defined before the JS, so I thought it was ok b/c it's 100% inside. According to Ayoub's link and other, if it's only inside the JS then it's OK. – Realce Dec 15 '18 at 00:40
  • Hmm, I wonder what the best way of showing what I'm working with is... So I'm using this JS for the collapseable div action: https://www.w3schools.com/howto/howto_js_collapsible.asp By the time this JS is hit, all the other server side php is finished and I have an HTML page full of spans and divs. This works: else { content.style.display = "block"; var uhh = content.getAttribute("id"); content.innerHTML = uhh; } – Realce Dec 15 '18 at 00:47
  • @teemu As I've tried to state, it's a variable problem considering that I can't echo $uhh either. – Realce Dec 15 '18 at 01:09
  • That doesn't answer my question ("_what exactly `get_term_by` returns?_"). I can only assume, that you're expecting PHP to be aware of the values of the JS variables, hence ... CAD – Teemu Dec 15 '18 at 01:16
  • I wish I wouldn't have used this in my example, as get_term_by can return many things. Let's replace it with term_description( $uhh, 'pa_producer' ); which returns a string. According to https://stackoverflow.com/questions/3345457/how-to-put-php-inside-javascript I'm using the variable correctly, no? – Realce Dec 15 '18 at 01:30
  • Why do you do `var uhh = content.getAttribute("id");`? It is useless. What did you expect to achieve with that assignment? – trincot Dec 16 '18 at 20:37
  • I have lot of divs with the class of "content" and dynamically assigned "id" numbers. I need to send the div id information to php script when the div content is clicked on. – Realce Dec 17 '18 at 02:44

1 Answers1

0

you forget to add ' at your php function after pa_producer

   var uhh2 = "<?php get_term_by('term_id', $uhh, 'pa_producer'); ?>";
Ayoub Benayache
  • 1,046
  • 12
  • 28
  • 1
    This seems like a correct answer - it's missing the final single quote after "pa_producer" – Mark Wheeler Dec 15 '18 at 00:17
  • That was kind of just an example with get_term, I'll try it now but other actions wouldn't fire either... Yes, still returns nothing. var uhh2 = ""; will return nothing as well, but I know that $uhh is correctly set. – Realce Dec 15 '18 at 00:20