Getting a string from PHP using jQuery

Tags: ,

Basically, I have a jQuery script which detects when a certain button is pressed. When the button is pressed, the script is meant to make a GET request to a separate PHP file. The PHP file should return a string to the JavaScript file, but I have multiple errors in both my scripts, creating a problem.

<script type="text/javascript">
    $(document).ready(function() {

When logging info, I get nothing. Literally, nothing.

   $f_contents = file("combo.txt");
   $line = $f_contents[array_rand($f_contents)];
   $data = $line;

That’s my PHP script.

If someone can help me out, I would appreciate it. None of the posts on the internet helped me out.


Oh and yes, I know my code is terrible.

Network Tab


  • No, you’re code is not terrible
  • You need to know the difference between $_GET['data'] and echoing out variable called $data .. If you know its Ok

To solve your problem you need to

  • Directly access the php file in the browser something like http://localhost/give.php and play with the php file to get the result printed out .. you may need to check the combo.txt file directory it may ./combo.txt or something else

  • Then copy the fullurl like http://localhost/give.php then paste it to $.get(fullurl....

Some notes

  • You’ve to echo out the data in your php file echo $_GET['data'] or echo $data to echo the file content data

  • With $.get you can use $.get('url' , {} , function(response){ console.log(response); });

Source: stackoverflow