1

I have the following code within the <script> tags, which are within the <head> tags of my HTML file;

function generateUMR($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
} 

Within the <body> tags, I have the following line of code in order to output the randomly generated string;

echo '<script type="text/javascript"> generateUMR(); </script>';

I have been trying for a few hours now but I am receiving no output from this code, yet no errors, could someone please tell me where I am going wrong?

Thanks in advance.

Cerbrus
  • 60,471
  • 15
  • 115
  • 132
user3266484
  • 115
  • 1
  • 2
  • 12
  • In javascript string concatenations is done with + operator, not with . like in php. it should be `$randomString += ...`. also consider declaring the variables with `var $characters`. If you don't do the variables will go to the global scope which could cause troubles in the future. – Igor Malyk Jun 25 '14 at 13:07
  • @IgorMalyk: That code block is php. (`strlen($characters)` and the dollar-sign variables are a big hint) – Cerbrus Jun 25 '14 at 13:09
  • So how do you call a function in PHP from javascript ? – Igor Malyk Jun 25 '14 at 13:10
  • possible duplicate of [Generate a string of 5 random characters in Javascript](http://stackoverflow.com/questions/1349404/generate-a-string-of-5-random-characters-in-javascript) – Chris Forrence Jun 25 '14 at 14:55

1 Answers1

5

Your function is treated as a string, you need to concatenate the function call like so.

echo '<script type="text/javascript"> ' . generateUMR() . ' </script>';

In order to see the result in the browser, you will need to view the HTML source because the browser won't show the content of a script tag. It doesn't really make sense to output this into a script tag, because it's just a random string, try some other tag such as <p></p> instead.

If you are trying to call the PHP function from the client side, then that won't work. You'll need to use AJAX to do that.

MrCode
  • 59,851
  • 9
  • 76
  • 106
  • I altered my code as you suggested but still no output :( – user3266484 Jun 25 '14 at 13:06
  • ... and no one noticed that `generateUMR` is a PHP function... `:)` – VisioN Jun 25 '14 at 13:08
  • @user3266484 what kind of output are you expecting? your PHP function will just output a random string, no JS logic... – Reinder Wit Jun 25 '14 at 13:10
  • For some reason I can't comment on Reinder Wits comment below, but when I tried his suggestion I got this error - Fatal error: Call to undefined function generateUMR(), any ideas? Thanks for ye're help so far anyway.. – user3266484 Jun 25 '14 at 13:11
  • I just want it to output the random string so that I can ensure it is working before using in it my furthur code..but it seems the function is outputting nothing? – user3266484 Jun 25 '14 at 13:12
  • that is because javascript has no idea of you server side PHP functions – Igor Malyk Jun 25 '14 at 13:13
  • @user3266484 it looks like your function is out of scope from when the echo is calling it. Is the function in an include or somewhere else? – MrCode Jun 25 '14 at 13:14
  • Sorry I had placed the code within the script tags rather than in the PHP block, when I moved it down it works now, thanks a million for ye're help, my javascript skills are not great as you can probably tell, but I am improving with ye're help! :) – user3266484 Jun 25 '14 at 13:18