search

Home  >  Q&A  >  body text

Make a button in html delete a row in MySql

I am currently working on a project where I use nodejs to create a user and add the user to MySql in a table called "users" I want to create a button in html that deletes the currently logged in user from mysql table

How do I make a button in html call a function in nodejs to delete a row in MySql

P粉760675452P粉760675452266 days ago459

reply all(1)I'll reply

  • P粉762447363

    P粉7624473632024-04-02 10:38:45

    This might be a good start to understand how to trigger API calls from Js

    fetch('https://reqres.in/api/deleteUser', {
        method: "DELETE",
        headers: {
            'Content-type': 'application/json'
        },
        body: JSON.stringify({
           id: '1'
        })
    })
    .then(res => {
        if (res.ok) { console.log("HTTP request successful") }
        else { console.log("HTTP request unsuccessful") }
        return res
    })
    .then(res => res.json())
    .then(data => console.log(data))
    .catch(error => console.log(error))

    Then NodeJs

    // Module dependencies
    
    var express    = require('express'),
        ejs        = require('ejs'),
        fs         = require('fs'),
        mysql      = require('mysql');
    
    // Application initialization
    
    var connection = mysql.createConnection({
            host     : 'localhost',
            user     : 'root',
            password : '' //
  • Cancelreply