fshock Posted June 17, 2011 Share Posted June 17, 2011 Hello :)This topic fits in cross section of ajax,javascript and php. But i think the biggest part where I need help is js.Using ajax i store data to $_sessions e.g. "This is a text line and there are like 10 more lines like this<br />".How should I load that text to browser and then display with javascript without reloading page?My javascript also contains delay after every line (that's the problem where I'm having. Echo works fine and shows everything at once, but i need it to be delayed). //var lines -> are var'ed inside ajax file like this:// <script>//// var lines ='<?php echo $text ; ?>'.split("||"); //or $text -> $_SESSION['text'];// </script>var c = 0;var speed = 5; function addLines(){ if(c < lines.length-1) { par = document.createElement('p'); par.setAttribute('id','p'+c); par.appendChild(document.createTextNode(lines[c++])); par.appendChild(document.createElement("br")); //added a br element dynamically instead of storing it in the text document.getElementById('ajaxtext').appendChild(par); setTimeout(addLines,speed*200); } } Link to comment Share on other sites More sharing options...
justsomeguy Posted June 17, 2011 Share Posted June 17, 2011 You can either use ajax to request that data from PHP and then display it on the page, or have PHP print the data in Javascript code when it's creating the page. Link to comment Share on other sites More sharing options...
fshock Posted June 17, 2011 Author Share Posted June 17, 2011 Its working this way:Choose 3 actions.Main.php -> ajax.php and returns -> main.php. Action1 is set.When 3 actions are set, then we calculate what text to show. it goes this way.main.php -> ajax.php -> calc.php (and some others)-> ajax.php, var $text as javascript then call javascript (or echo all text) -> main.phpI can echo $text anytime without delay. But with javascript something not working. Is it enough to put include <script> text show script</script> inside main.php?Is it possible that my ajax and javascript somehow interferes, becouse both have same document.getElementById? My ajax is this type ('element_id', page.php?do=something');I'm learning both ajax and javascritps now and can't get this solved.' Link to comment Share on other sites More sharing options...
birbal Posted June 18, 2011 Share Posted June 18, 2011 I can echo $text anytime without delay. But with javascript something not workingcan you tell what exactly is not working?Is it possible that my ajax and javascript somehow interferes, becouse both have same document.getElementById?No it will not interfere. the response of the ajax call is separate document, thw response object will be stored in different variable. Link to comment Share on other sites More sharing options...
fshock Posted June 18, 2011 Author Share Posted June 18, 2011 I can't explain it. I made an example, please, take a look.Main page -> text.php as txt file: http://www.cursed-dragon.eu/test/text-txt.txtas page: http://www.cursed-dragon.eu/test/text.phpAjax test page -> check.php, problem here i guess. <?php if (empty($_SESSION)){session_start();}header("Content-Type: application/x-javascript; charset=iso-8859-1");header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");header("Cache-Control: no-store, no-cache, must-revalidate");header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");header("Cache-Control: post-check=0, pre-check=0", false);header("Pragma: no-cache");?><script type="text/javascript" language="javascript" src="display_text.js"></script><?php if(empty($_SESSION['action1'])){ $actionx = action1; } elseif(empty($_SESSION['action2'])){ $actionx = action2; } elseif(empty($_SESSION['action3'])){ $actionx = action3; }switch ($_GET['letter']) { case 'a': $_SESSION[$actionx] = 'a'; echo "<p>A letter is selected.</p>"; break; case 'b': $_SESSION[$actionx] = 'b'; echo "<p>B letter is selected.</p>"; break; case 'c': $_SESSION[$actionx] = 'c'; echo "<p>C letter is selected.</p>"; break; case 'reset': $_SESSION['action1'] = $_SESSION['action2'] = $_SESSION['action3'] = ""; echo "<p>Actions cleared.</p>"; break; case 'start': //set variable in which displayed text is stored. $display ="Starting to display your choices:<br />||"; if(!empty($_SESSION['action1'])) { $display .="Your first chosen letter is..<br />|| Letter <b>".$_SESSION['action1']." </b><br />||"; }else { $display .="You have not chosen first letter.<br />||"; } if(!empty($_SESSION['action2'])) { $display .="Your second chosen letter is..<br />|| Letter <b>".$_SESSION['action2']." </b><br />||"; }else { $display .="You have not chosen second letter.<br />||"; } if(!empty($_SESSION['action3'])) { $display .="Your third chosen letter is..<br />|| Letter <b>".$_SESSION['action3']." </b><br />||"; }else { $display .="You have not chosen third letter.<br />||"; } $display .= "...<br />|| Finished. <br />|| The end. <br />"; //remove chosen actions unset($_SESSION['action1']); unset($_SESSION['action2']); unset($_SESSION['action3']); //var_dump($display); //var javascript variable as $display from php ?> <script> var lines = "<?= $display ?>"; var lines = lines.split("||"); </script> <?php //and start displaying with javascript echo "<script language='javascript'>addLines();</script>"; //echo $display; break; } if (!empty($_SESSION['action1'])){echo "First letter is '".$_SESSION['action1']."'<br />";}if (!empty($_SESSION['action2'])){echo "Second letter is '".$_SESSION['action2']."'<br />";}if (!empty($_SESSION['action3'])){echo "Third letter is '".$_SESSION['action3']."'<br />";}?> Big view of this check.php : http://www.cursed-dragon.eu/test/check-txt.txtas page: http://www.cursed-dragon.eu/test/check.phpAlso my javascript to display text: http://www.cursed-dragon.eu/test/display_text.jsajax script -> ajaxrequest.js -> http://www.cursed-dragon.eu/test/ajaxrequest.js Link to comment Share on other sites More sharing options...
fshock Posted June 18, 2011 Author Share Posted June 18, 2011 Oh.. and text should start to display when button 'Start' is pressed. It echo'es it (just remove // near echo $display), but with javascript its not working (though the javascript itself works if pressed on the button 'check if addlines is working). Link to comment Share on other sites More sharing options...
fshock Posted June 19, 2011 Author Share Posted June 19, 2011 No ideas guys? I somehow think the problem is here, but i don't know how to change it.. Dunno how to make javascript be displayed in other page, outside ajax page..//var javascript variable as $display from php?><script>var lines = "<?= $display ?>";var lines = lines.split("||");</script><?php//and start displaying with javascriptecho "<script language='javascript'>addLines();</script>"; Link to comment Share on other sites More sharing options...
justsomeguy Posted June 19, 2011 Share Posted June 19, 2011 It doesn't work to return script tags in an ajax response and then try to add them to the page. The response from the server is text, not HTML code. Return the data only, and keep your logic in Javascript in the page instead of returning it from the server. You can use eval to execute a string as code, but doing so is slow and inefficient, and there's usually a better way. If you want to include a script dynamically have the server return only the filename if necessary, and use DOM methods to create a new script element with the selected src and append it to the page. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.