javascript - Issue calling JS function from PHP echo with a specific variable

825

I have a JS function that will add keys with values to the current URL. There are two almost identical links below, only difference is the variable being passed to the JS function. One link passes $month, the other passes $event_category. For some reason, when passing $event_category, the JS function doesn't even get called. Anyone know what I'm doing wrong?

You'll have to scroll to the right to see where the difference is.

  $month = 1;
  $event_category = (string) ($eventCategories[$k]["event_category"]);

  echo gettype($event_category); // prints "string"


  // doesn't work?
  echo '<div ><a href="javascript:void(0);" onclick="javascript:insertParam('. "'event_category'" .', '. $event_category .');" role="button">
  <image width="100" height="60" src="images/'. $images_list[$eventCategories[$k]["event_category"]].'"></a></div>'; 

  // works
  echo '<div ><a href="javascript:void(0);" onclick="javascript:insertParam('. "'event_category'" .', '. $month .');" role="button">
  <image width="100" height="60" src="images/'. $images_list[$eventCategories[$k]["event_category"]].'"></a></div>';   
437

Answer

Solution:

You should put quotes around the $event_category, otherwise it will be interpreted by javascript as a variable. So, convert

. $event_category .

to

. '"' . $event_category . '"' .

People are also looking for solutions to the problem: php - Zend Framework 2 Module to Sub domain mapping

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.