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?