5

I am trying to fetch values from php file

api.php

      <?php
      // want to fetch this value 
      $a = 'come!! fetch this value';

      ?>

my javascript page has code like this.(this code is not on page api.php)

            fetch('http://localhost/react_task/react-webpack-boilerplate/php/api.php', {
        method: 'get',
        // may be some code of fetching comes here
    }).then(function(response) {
            if (response.status >= 200 && response.status < 300) {
                return response.text()
            }
            throw new Error(response.statusText)
        })
        .then(function(response) {
            console.log(response);
        })

can you please guide me how to fetch value of variable from php file by using fetch.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Manpreet Oberoi
  • 385
  • 3
  • 4
  • 13
  • 1
    Simple. In your PHP page, put `echo $a;` at the end. Then when you fetch the page, it will grab the text content of the page, which is what you echo'ed. – RK. Sep 09 '16 at 14:36
  • Possible duplicate of [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – David R Sep 09 '16 at 14:37
  • no its dubpicate man.. @DavidR – Manpreet Oberoi Sep 09 '16 at 14:42

3 Answers3

4

You are not echoing the value, so it won't be send.

<?php
// want to fetch this value 
$a = 'come!! fetch this value';
echo $a;
?>
MarcHoH
  • 340
  • 1
  • 6
  • if i echo the value .. then definitely it will echo..... that is right .... but i want to see value of $a on javascript page not on api page. – Manpreet Oberoi Sep 09 '16 at 14:41
  • That's not possible, the only thing JavaScript can see is what's been echoed by PHP or what's is served by the webserver. – MarcHoH Sep 09 '16 at 14:47
  • basically i am working on security app.. if it echo the data.. then its not uite safe.. – Manpreet Oberoi Sep 09 '16 at 14:54
  • Can you maybe explain a bit more about it? For now it looks like you need some kind of authentication? – MarcHoH Sep 09 '16 at 15:01
  • Basically i am working with 2 servers right now,, apache and node .js.... my apache server(php file) contain public key.. and i want to fetch that public key from apache server.. this is basically i am trying to do.. just explained short of bit.... its quite long security procedure on some website. – Manpreet Oberoi Sep 09 '16 at 15:04
  • Oke thx, than a simple echo will be sufficient, it's a public key you can give that to everyone. This wouldn't let to a security issue. – MarcHoH Sep 09 '16 at 15:09
  • thanks man................... i got some new concept for my project just by explainibng my situation.. that is wierd.. but fine.. thanks – Manpreet Oberoi Sep 09 '16 at 15:17
0

Your php needs to output the value you want. A simple way to do this is to use echo. For example:

echo 'come!! fetch this value';
RickCoxDev
  • 165
  • 1
  • 9
-1

You should use echo to display it in php then it will be available in you JavaScript reponse.

Okechukwu Obi
  • 409
  • 4
  • 8