0

Let me begin by saying that my knowledge of PHP is limited at best, so I am not really sure if what I have tried is correct.

Essentially, I need PHP to run a specific script depending on a variable (language in this case). From what I have found online, this requires the use of a switch statement, but it doesn't seem to run properly. Below is the beginning of the code

<?php
$lang = "en-US";

switch ($lang) {
    case "en-US":
        <script type="text/javascript">
            (function() {
                var se = document.createElement('script'); se.type = 'text/javascript'; se.async = true;
                se.src = '//storage.googleapis.com/code.rival.com/js/3ca9-4824-43d0-81ec-0c04fb80901b.js';
                var done = false;
                se.onload = se.onreadystatechange = function() {
                    if (!done&&(!this.readyState||this.readyState==='loaded'||this.readyState==='complete')) {
                        done = true;
                    }
                };
                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(se, s);
             })();
       </script>
;
break;
case "de-DE":
    <script type="text/javascript">
        (function() {
            var se = document.createElement('script'); se.type = 'text/javascript'; se.async = true;
            se.src = '//storage.googleapis.com/code.rival.com/js/d8d2-9653-427d-bcb6-13e135d6195e.js';
            var done = false;
            se.onload = se.onreadystatechange = function() {
                if (!done&&(!this.readyState||this.readyState==='loaded'||this.readyState==='complete')) {
                    done = true;
                }
            };
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(se, s);
         })();
</script>
;
break;

There is a fair bit more code, but I didn't want to place a wall of code here. The default at the end uses the same JavaScript as "case "en-US"".

I hope the problem makes sense.

PaulRey
  • 13
  • 5
  • You are mixing PHP code with HTML. It doesn't work this way. Read about [PHP tags](http://php.net/manual/en/language.basic-syntax.phptags.php) and [escaping from HTML](http://php.net/manual/en/language.basic-syntax.phpmode.php). – axiac Mar 08 '18 at 15:22
  • REF: _I need PHP to run a specific script_ ; you should start by understanding the PHP doesn't _run_ anything in the browser. All PHP will do is _send_ text to the browser. It is up to the browser to read,interpret and execute a script (that is sent by PHP). So with that background, you are not sending text to the browser. Each language `case` in your code should `echo` that text (in the form of a script) out to the browser. – Randy Casburn Mar 08 '18 at 15:23
  • 1
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Rotimi Mar 08 '18 at 15:43
  • @RandyCasburn Thanks for the info. Our dev left, so I'm kind of taking a crash course here. – PaulRey Mar 09 '18 at 10:38
  • @axiac I definitely will be looking at those. – PaulRey Mar 09 '18 at 10:38

1 Answers1

1

You cannot mix javascript and php like that. If you need to output javascript, you should echo it or get out of php.

Also, I'm not sure if there are more differences, but if it is just the name of the script, you could get rid of all the duplicate javascript using an array.

For example:

<?php
$lang = "en-US";

// Store all combinations
$languageScripts = [
  'en-US' => '3ca9-4824-43d0-81ec-0c04fb80901b.js',
  'de-DE' => 'd8d2-9653-427d-bcb6-13e135d6195e.js',
];

// Output the javascript
?>
    <script type="text/javascript">
        (function() {
            var se = document.createElement('script'); se.type = 'text/javascript'; se.async = true;

            // Here you add the script you want to run:
            se.src = '//storage.googleapis.com/code.rival.com/js/<?php echo $languageScripts[$lang]; ?>';

            var done = false;
            se.onload = se.onreadystatechange = function() {
                if (!done&&(!this.readyState||this.readyState==='loaded'||this.readyState==='complete')) {
                    done = true;
                }
            };
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(se, s);
         })();
    </script>
<?php
// continue with your php script
jeroen
  • 91,079
  • 21
  • 114
  • 132