0

Hi I am trying to send a long string of numbers from a file to Javascript. One I get it in Javascript as a string the rest will be easy.

This is the PHP code that grabs the series of numbers. It does grab them properly because it prints it out perfectly if I instruct it to do so.

$data = readfile($dataFile);

This is the Javascript Code:

var data = <?php echo json_encode($data, JSON_NUMERIC_CHECK); ?>;

When ever I run it the variable "data" is set to a random number. I assume it is the the number of characters in the sequence. I am relatively new to PHP and Javascript so a nice and clear explanation would be greatly appreciated. Thanks in advance.

2
  • what does console.log(data) gives you? Commented Aug 5, 2014 at 17:45
  • JSON is a string, so I suspect no quotes around the PHP output is the issue. Commented Aug 5, 2014 at 17:47

3 Answers 3

3

Readfile reads the contents of the file and outputs them. It returns the byte length of the file, so $data is the number of the read bytes. link.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 It would be great if you could show a solution using file_get_contents instead.
You want the js-variable data to have the content of the file?
I believe that is what the OP is expecting
Ok, than he has to replace readfile($dataFile) with file_get_contents($dataFile)
1

The number that is set in the variable $data is the number of bytes read by readfile. This is because readfile only reads the file and outputs its content to the standard output. To get the actual data in the file and assign it to a viariable, you can use file_get_contents(), like this:

$data = file_get_contents($dataFile);

Then the javascript assignment is good ;)

Comments

0

You need quotes if you want it to be a string in Javascript:

var data = '<?php echo json_encode($data, JSON_NUMERIC_CHECK); ?>;';

4 Comments

No, you should not add quotes since json_encode adds them automatically if the variable $data is a string. Also, if you add quotes you will have problems if the $data variable contains a string that includes quotes.
@ClaudioVenturini It doesn't add the quotes with JSON_NUMERIC_CHECK. Try it: echo json_encode('123', JSON_NUMERIC_CHECK); will output 123, not '123'.
Ok, but that it's fine because 123 it's a number, and so it's perfectly valid to assign it to a javascript variable without quotes. If you do json_encode('a_string'), you get "a_string" and not a_string
@ClaudioVenturini It's valid Javascript to do var data = 123;, but then typeof data will be 'number', and the OP specifically asked for a string in Javascript. I would say that an answer combining our two answers gives the OP exactly what they want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.