-3

i wanted to retrive a piece of python code from the database.

The javascript variable c1 is working properly but the following c2,c3,c4 and c5 is not showing the data wheres all the php variables are showing the data properly.

What might be the reason and what can be the solution?

I have tried to use javascript and php togather to get the desired result as I am going to pass the variables on a javascript function. php variables are showing the data but the javascript variable c1 is getting the data but c2,c3,c4 and c5 are not getting the data

  echo "<script> var c1 = '".$code_1."';  </script>";
  echo "<script> var c2 = '".$code_2."';  </script>";
  echo "<script> var c3 = '".$code_3."';  </script>";
  echo "<script> var c4 = '".$code_4."';  </script>";
  echo "<script> var c5 = '".$code_5."';  </script>";

  // c1 works properly

  echo "<script>alert(c1);</script>";

  // c2,c3,c4,c5 doesn't works

  echo "<script>alert(c2);</script>";
miken32
  • 42,008
  • 16
  • 111
  • 154

1 Answers1

0

Use json_encode() to properly format any special characters in the PHP values, rather than just substituting the PHP values directly into JS.

  echo "<script> var c1 = ".json_encode($code_1).";  </script>";
  echo "<script> var c2 = ".json_encode($code_2).";  </script>";
  echo "<script> var c3 = ".json_encode($code_3).";  </script>";
  echo "<script> var c4 = ".json_encode($code_4).";  </script>";
  echo "<script> var c5 = ".json_encode($code_5).";  </script>";
Barmar
  • 741,623
  • 53
  • 500
  • 612