Według "internetów", skrypt umożliwiający wykonanie get zmiennych php. Wygląda następująco:
var phpText = <?php include("class/prolog.php"); prolog() ?>
JS pobiera zgodnie instrukcja wywołowanie funkcji prolog () wraz instrukcją:
<?php
function prolog ()
{
$name_file1 = "http://localhost/Agitta/class/text/chapter0/prolog_text_1.txt";
$name_file2 = "http://localhost/Agitta/class/text/chapter0/prolog_text_2.txt";
$name_file3 = "http://localhost/Agitta/class/text/chapter0/prolog_text_3.txt";
// Show Text
$show1 = file_get_contents($name_file1);
$show2 = file_get_contents($name_file2);
$show3 = file_get_contents($name_file3);
// Test Function
echo $show1;
}
?>
i "echuje zawartość pliku"(jest to string) , jednakże zwraca błąd.
"Nie jestem w stanie zdefinować zawartości zmiennej"
Co ciekawe raz działa ten fragment kodu:
document.getElementById("showText").innerHTML = phpText;
Ale wtedy nie działają, funkcje obiektów klasy (showProlog) z zapisanym eventem onload strony. Gdy się ta posypała, pozostałe również padły.
Pierw myślałem, że problem jest znacznik końca lini. Jednakże, nie akceptuje ani: ani tego znacznika " (by wskazać, że zmienna jest stringem) ani tego ";"
Niestety dalej zwracało błąd, Związku z tym mam pytanie: Czy da radę, zmusić do właściwego interpretowania JS stringa wieloliniowego z elementam HTML (między innymi <br>), bez użycia AJAX?.
Poniżej przedstawiam pełny kod:
<script>
var text = document.getElementById("showText");
var next = document.getElementById("nextButton");
var prologText = document.getElementById("prologText");
var phpText = <?php include("class/prolog.php"); prolog() ?>
function showProlog ()
{
text.classList.add("jsPrologclass");
next.classList.add("jsPrologclass");
prologText.classList.add("jsPrologShowClass");
};
function CloseProlog ()
{
//Show TEXT
text.classList.remove ("jsPrologclass");
text.classList.add ("jsPrologShowClass-1");
next.classList.remove ("jsPrologclass");
next.classList.add ("jsPrologShowClass-1");
prologText.classList.remove ("jsPrologShowClass");
prologText.classList.add ("jsPrologclass");
document.getElementById("showText").innerHTML = phpText;
};
</script>
PHP:
<?php
// Agita first game[FLAG]= YES [TRUE]
// name files
function prolog ()
{
$name_file1 = "http://localhost/Agitta/class/text/chapter0/prolog_text_1.txt";
$name_file2 = "http://localhost/Agitta/class/text/chapter0/prolog_text_2.txt";
$name_file3 = "http://localhost/Agitta/class/text/chapter0/prolog_text_3.txt";
// Show Text
$show1 = file_get_contents($name_file1);
$show2 = file_get_contents($name_file2);
$show3 = file_get_contents($name_file3);
// Test Function
echo $show1;
}
?>