Page 1 of 1

Send javascript alert message and refresh page using ESP8266

PostPosted: Fri Apr 19, 2019 10:42 am
by Erick Del Mundo
Hi I'm just a newbie and found an online tutorial about basic attendance system https://devcraze.com/tutorials/connecting-rfid-scanner-to-php-using-nodemcu-wifi-module/, I'm using Esp8266 NodeMcu, RFID Rc522 as material then Arduino Core and PHP as programming tool, its working properly but how can I send an alert message like:
echo "<script>alert('Successfully Login');<script>";

or if failed:

echo "<script>alert('Unable Login');<script>";

aside from:

if(payload.equals("success")) {
digitalWrite(SUCCESS_PIN, HIGH);
} else {
digitalWrite(ERROR_PIN, HIGH);

This is the code I used in Arduino core to program ESP8266 (NodeMCU):
Code: Select allvoid sendRfidLog(long cardId) {
  if(WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    String postData = "cardid=" + String(cardId) + "&action=insertRfIdLog";
    http.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
    int httpCode = http.POST(postData);
    String payload = http.getString();
    if(payload.equals("success")) {
     digitalWrite(SUCCESS_PIN, HIGH);
    } else {
     digitalWrite(ERROR_PIN, HIGH);

and the code in PHP:

Code: Select allfunction insertRfIdLog() {
    include 'connect.php';
    $cardid = $_POST['cardid'];
    $time = time();
    $stmt = $conn->prepare("INSERT INTO `tbllogs`(`cardid`, `logdate`) VALUES (:card, :dt)");
    $stmt->bindParam(":card", $cardid);
    $stmt->bindParam(":dt", $time);
   echo "success"; // I want this part to become an alert message like echo "<script>alert('Successfully login');</script>"; then REFRESH the page after clicking the OK button of the alert message

Thank you in advance