-1

So I have a PHP file called "dbEmails.php", it basically writes all existing emails from MySQL to an array:

<?php
require_once __DIR__ . '../db.php';

$sth = $dbh->query('SELECT email FROM users');

while($row = $sth->fetch(PDO::FETCH_ASSOC)){
    $emails[] = $row['email'];
}

echo json_encode(array($emails));

I have a NodeJS HTTP server that is used for validating whether email is already registered or not, while in registration page. Here it is:

var http        = require('http');
var querystring = require('querystring');

//var emails = array called "emails" from "dbEmails.php"

function accountExists (email) {
  return emails.indexOf(email) > -1;
}

var server = http.createServer(function(req, res) {
  var params = req.url.split('?')[1];
  var data   = querystring.parse(params);
  var email  = data.email;

  res.statusCode = 200;
  res.setHeader("Content-Type", "application/json");
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

  if (accountExists(email)) {
    res.write('""');
  } else {
    res.write('"true"');
  }

  res.end();
});
server.listen(6666);

As you can see in the code I have already commented what exactly my question is. I need to get $emails array from PHP into var emails of JS.

I searched some but couldn't find a solution. I think I'm not supposed to use AJAX, I don't know much of anything about Node at all.

What would be the best way of going about this?

Question 2:

Will var emails update automatically when there is new email entry in my database? I guess not. If it doesn't update then can someone please tell me how I should go about keeping my var emails up to date?

Alongkorn Chetasumon
  • 2,575
  • 1
  • 20
  • 36
Help Leecher
  • 119
  • 1
  • 9
  • You should be able to find several examples how to use curl to send data to an http server, for example [1](http://stackoverflow.com/a/9065892/5781248) [2](http://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code) – J.J. Hakala Jul 01 '16 at 14:02

1 Answers1

0

From question 1,

From question 2,

  • if you update yiue node app from my suggestion above, it should be updated automatically
Alongkorn Chetasumon
  • 2,575
  • 1
  • 20
  • 36