As we have now resolved this to be more of a PHP issue, I’m moving this thread back to the PHP folder so that they can help to finish things here off properly.
@cprradio
Here are the updated codes, the third line is the codes you suggested.
<script>
//var answer = "<?php echo $ans; ?>";
var answer = "<?php echo implode('\n', preg_split("/(\r\n|\r|\n)/", $ans)); ?>";
$(function () {
$("#right-answer2").hide();
$("#reference").hide();
$("#done").click(function(){
$("#right-answer2").val(answer).show();
$("#reference").show();
});
});
</script>
For the html generated codes, you can see it live.
I uploaded it to live website.
Just visit the link below
http://www.codesmastery.com/language/landing/codeigniter/
On that link you will see this below,
General: 24
Now just click the link 24
it will redirect you to another page.
On this new page you can use Google Chrome Tool/Console to see the error.
Thanks in advance for helping me.
You could echo out \n
by escaping it with a preceding backslash.
echo "\\n" ;
You’re issue is that comment.
//var answer = "<?php echo $ans; ?>";
The PHP code is still executing in that creating invalid JavaScript, delete that line and it should work.
This is your HTML output:
//var answer = "class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}";
var answer = "class Blog extends CI_Controller {\n\n public function index()\n {\n echo 'Hello World!';\n }\n}";
$(function () {
$("#right-answer2").hide();
$("#reference").hide();
$("#done").click(function(){
$("#right-answer2").val(answer).show();
$("#reference").show();
});
});
You can see the comment is writing new lines, thus breaking out of the comment and causing an error.
This isn’t needed as I used it as a literal in PHP, so the \n
is being written properly, the issue is using a JavaScript comment to comment out executed PHP code will never work. That code runs before JavaScript so it is breaking out of the single line comment. He could use a block comment though.
/*
var answer = "<?php echo $ans; ?>";
*/
WOW, It really did worked.
Thank you very much cpradio.
Off topic:
By the way what is your specialty javascript or php?
By the way what is your specialty javascript or php?
Neither? Well, maybe PHP, I consider my specialty backend development work. Be it .NET, Node.js, PHP, whatever, so long as it runs on the server and doesn’t deal with any front-end, that is where I typically work.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.