1

I have a php file and Node API. The php file in server which return the random number by user given max value. The user given maximun value is given from Node Js to php. Then the php returns the random value, then the values should be save in MongoDB

php code

<?php
    $min = 1111;
    $max = '';  // This is given by user from Node API

    $number = mt_rand($min, $max); //Generate a random number using the rand function.

    echo $number;
?>

Node API

router.post("/findMax", upload.single('image'), function (req, res, next)
{
  var enteredMaxNum = req.body.maxNum;
  if(!enteredMaxNum)
  {
    return res.status(400).send("Maximum number should be given");
  }

  const objNumber = new Number(
    {
      max : enteredMaxNum,  // User given, should pass to php variable
      randNum : ????,       // php file return value
    });

    objNumber.save((err) =>
      {
        if (err)
        {
          console.log("New Error : " + err);
          return next(err);
        }
        res.status(201).send("Numbers saved");
      });
})
  • you should connect to your server (with the php script) from within your node code and pass the value – sietse85 Oct 04 '18 at 09:31
  • How can I connect the php script with Node? –  Oct 04 '18 at 09:34
  • for examples take a look here: https://stackoverflow.com/questions/16298161/how-to-run-php-script-on-node-js – sietse85 Oct 04 '18 at 09:36
  • btw why do you need to use php for this? and not do everything in node? – sietse85 Oct 04 '18 at 09:39
  • Actually the php is used to send SMS by given SMS gateway with generating random number. There is no option to send SMS by Node . –  Oct 04 '18 at 09:42
  • And as your above link, how to pass the values? –  Oct 04 '18 at 09:46
  • Is there any solutions ? –  Oct 04 '18 at 15:02
  • This is an XY post, Post has been answered here https://stackoverflow.com/questions/52661805/does-not-work-passing-values-to-php-from-node-js-and-back , OP lacks basic knowledge of both Javascript and PHP – darklightcode Oct 09 '18 at 05:49
  • The OP had just started to learn by do for Node Js and php –  Oct 09 '18 at 06:02

1 Answers1

0

Why did not you use axios. Try this code hopefully will work as you want

node

router.post("/findMax", upload.single('image'), function (req, res, next)
{
  var enteredMaxNum = req.body.maxNum;
  if(!enteredMaxNum)
  {
    return res.status(400).send("Maximum number should be given");
  }

  axios.post('your-php-file-path', require('querystring').stringify({getNodeNum: enteredMaxNum}))
  .then(response => {
    if (response)
    {
      const objNumber = new Number({max: enteredMaxNum, randNum: response.data.random});
      objNumber.save((err) => {
        if (err)
        {
          console.log("New Error : " + err);
          res.status(500).send(err);
        }
        res.status(201).send("Numbers saved");
        });
    }
      })
  .catch(error => res.status(500).send(error));
})

php

<?php

    $min = 1111;

    if(isset($_POST['getNodeNum'])) // It should be $_POST
    {
        $max = $_POST['getNodeNum'];

        $number = mt_rand($min, $max);

        print(json_encode(['random' => $number]));
        exit();
    }
?>
S.Sakthybaalan
  • 499
  • 6
  • 20