0

I am getting the following error while passing two php variable inside the javascript function.

Error:

Uncaught SyntaxError: Unexpected token ,

My code is given below.

<script>
 editQuestionField(<?php echo $GLOBALS['ques'] ?>,<?php echo $GLOBALS['id'] ?>);
</script>

its generating the html output in browser console which is given below.

editQuestionField(,5742d88fe4017af412000030);

Please help me to resolve this error.

1
  • Your first argument is emtpy echo $GLOBALS['ques'] Commented May 25, 2016 at 12:06

2 Answers 2

3

Use json_encode() to pass values to javascript.

editQuestionField(<?php echo json_encode($GLOBALS['ques']) ?>,<?php echo json_encode($GLOBALS['id']) ?>);

Also $GLOBALS['ques'] is most likely empty/not defined.

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

1 Comment

This is a better suggestion. As double quotes will get escaped.
0

write values in '' quotes like this editQuestionField('val1','val2');

<script>
 editQuestionField('<?php echo $GLOBALS['ques'] ?>','<?php echo $GLOBALS['id'] ?>');
</script>

Comments

Your Answer

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