0

I'm trying to pass 2 variables through a php function's parameters to be use by the javascript's trigger function but the javascript's trigger function is not accepting the 2 variables. Not sure what I did wrong. Please take a look:

trigger.php

<?php

function trigger($type,$name) {

echo "<script>";

echo "

$(function(){
  $('$type[name=$name]').trigger('change');
});

";

echo "</script>";

}

?>
4
  • what does the output look like on your page? Commented Jun 17, 2015 at 18:20
  • @cmorrissey Parse error: syntax error, unexpected '=', expecting ']' Commented Jun 17, 2015 at 18:23
  • @cmorrissey I assume it's talking about the name=$name, part. But not sure what I did wrong there. Commented Jun 17, 2015 at 18:24
  • try to escape $(function(){ to \$(function(){ and $('$type[name to \$('$type[name ?I'm not sure the $ can be escaped or not. Commented Jun 17, 2015 at 18:27

1 Answer 1

2

The problem is that you are using " which attempt to parse the variables in the string, if you switch to ' the $ of the jQuery won't be parsed and you can concat your variables with .

function trigger($type,$name) {

echo '<script>';

echo '

$(function(){
  $(\'' . $type . '[name=' . $name . ']\').trigger(\'change\');
});

';

echo '</script>';

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer! It works. I have to admit, at this point it gets really confusing when mixing php and javascript types into a single function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.