Help me please! There is a script (java script) embedded in the php-page

(script should change text smiles to graphic ones) Java script

<script type="text/javascript"> /* запускаСм скрипт, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ замСняСт тСкстовыС смайлики Π½Π° графичСскиС послС Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ страницы */ window.onload = function(){ smiley_replace(); } /* Π’ ΠΌΠ½ΠΎΠ³ΠΎΠΌΠ΅Ρ€Π½ΠΎΠΌ массивС smiley хранится информация, нСобходимая для Ρ€Π°Π±ΠΎΡ‚Ρ‹ скрипта: - рСгулярноС Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅, с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ скрипт Π±ΡƒΠ΄Π΅Ρ‚ Π½Π°Ρ…ΠΎΠ΄ΠΈΡ‚ΡŒ тСкстовыС смайлики; - имя ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ с графичСским смайликом; - Π°Π»ΡŒΡ‚Π΅Ρ€Π½Π°Ρ‚ΠΈΠ²Π½Ρ‹ΠΉ тСкст, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠΎΠΊΠ°Π·Ρ‹Π²Π°Ρ‚ΡŒΡΡ, Ссли ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ° ΠΏΠΎ ΠΊΠ°ΠΊΠΈΠΌ-Ρ‚ΠΎ ΠΏΡ€ΠΈΡ‡ΠΈΠ½Π°ΠΌ Π½Π΅ загрузится. */ smiley = new Array([/:-?\)+/gi,"smiles/1.gif",":-)"],[/:-?\(+/gi,"sad.gif",":-("]); /* функция, которая ΠΈ Π±ΡƒΠ΄Π΅Ρ‚ Π·Π°ΠΌΠ΅Π½ΡΡ‚ΡŒ тСкстовыС смайлы Π½Π° графичСскиС */ function smiley_replace(){ for(i=0;i<smiley.length;i++){ document.getElementById("mytxt").innerHTML = document.getElementById("mytxt").innerHTML.replace(smiley[i][0], '<img src="'+smiley[i][1]+'" alt="'+smiley[i][0]+'"/>'); } } </script> 

The script itself works fine in the html page, but does not work in php. Please help find out what the problem is and if there is a way out of it.

Closed due to the fact that off-topic participants Grundy , cheops , user194374, Streletz , aleksandr barakin 23 Jun '16 at 4:56 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • β€œQuestions asking for help with debugging (β€œ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Grundy, cheops, community spirit, Streletz, aleksandr barakin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • <script type = "text / javascript"> window.onload = function () {smiley_replace (); } smiley = new Array ([/: -?) + / gi, "smiles / 1.gif", ":-)"], [/: -? (+ / gi, "sad.gif", ": - ("]); function smiley_replace () {for (i = 0; i <smiley.length; i ++) {document.getElementById (" mytxt "). innerHTML = document.getElementById (" mytxt "). innerHTML.replace (smiley [i] [0], '<img src = "' + smiley [i] [1] + '" alt = "' + smiley [i] [0] + '" />');}} </ script > - Daniel
  • And run the page on the server? Or not? -_- - Aleksey Shimansky
  • Of course I launch the page on the server :-) - Daniel
  • What errors in the console writes? - Alexey Shimansky
  • no, everything is empty (you just need a symbol for example ;-) to be replaced by a smiley (picture) - Daniel

1 answer 1

If your PHP script, which you, by the way, did not show, does some sort of database selection, and your text, framed in id="mytext" , is some kind of record from the table (for example, a comment), and it is not one, then your script will not work in principle, because id can be used to define one, and only one element for the whole page , and a class can be used to define several elements, or a group of elements .

This is one of the potential problems. If I'm wrong, let me know. And, interestingly, the browser console will not issue any errors about this

  • well at least one element would be replaced - Grundy