0

I want to die. To feel nothing will be better then now. Mathjax is undefined but only if my2 session variables exist:

<?php
if(isset($_SESSION['odpowiedzi']) && isset($_SESSION['gdzieblad']))
    {
        echo '<script type="text/javascript">window.odpowiedzi = '.json_encode($_SESSION['odpowiedzi']).'; alert(window.odpowiedzi);</script>';
        unset($_SESSION['odpowiedzi']);
        echo '<script type="text/javascript">var gdzieblad = '.$_SESSION['gdzieblad'].';</script>';
        unset($_SESSION['gdzieblad']);
    }
?>

<script>
if(typeof gdzieblad !== 'undefined')
{
    $('.code').each(function(index,value)
    {
        var str1 = window.odpowiedzi[index];
        var str2 = "$$";
        var res = str2.concat(str1);
        var res2 = res.concat(str2);
        $(this).html(res2);
    });
}
</script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML"></script>
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({tex2jax:{inlineMath: [['$','$'], ['\\(','\\)']]}});
    var QUEUE = MathJax.Hub.queue;
</script>
<script type="text/javascript" src="js/funkcjesprawdzian2.js"></script>

And in last js in thaat line:

window.UpdateMath = function(TeX){var math = MathJax.Hub.getAllJax(id)[0];QUEUE.Push(["Text",math,TeX]);}

...I have critical JS errorthat MathJax is undefined. What is more when $_SESSION['odpowiedzi'] and $_SESSION['gdzieblad'] is unset, everything is working and I dont get that error. WHAT IS MORE mathjax is generating good math symbols with that error but then I cant continue other js code and I can't use that fucnction from line with error. Please help me.

EDIT: I know from console that: window.odpowiedzi = ["\frac{1}{1}","\frac{1}{2}","\frac{1}{3}444","\frac{1}{4}"]; alert(window.odpowiedzi); It don't look like a JSON

Artimal
  • 651
  • 7
  • 24

1 Answers1

0
echo sprintf("'%s'", json_encode($_SESSION['odpowiedzi']));

will print json. Now in javascript you need to parse it to the object. See this answer how to do this.

Don't forget to put " or ' for $_SESSION['gdzieblad'] too.

Community
  • 1
  • 1
Artyom Sokolov
  • 2,385
  • 3
  • 23
  • 34