├── 1.JPG ├── 2.JPG ├── Capture.JPG ├── package.json ├── public ├── JS │ ├── AJAX2.js │ ├── connection.js │ ├── validate.js │ ├── API2.js │ ├── AJAX.js │ └── API.js ├── CSS │ └── style.css └── HTML │ └── index.html ├── README.md ├── app.js └── LICENSE /1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AsadiAhmad/Simple-Signup-Nodejs/HEAD/1.JPG -------------------------------------------------------------------------------- /2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AsadiAhmad/Simple-Signup-Nodejs/HEAD/2.JPG -------------------------------------------------------------------------------- /Capture.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AsadiAhmad/Simple-Signup-Nodejs/HEAD/Capture.JPG -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "body-parser": "^1.20.2", 4 | "express": "^4.18.2", 5 | "mysql": "^2.18.1", 6 | "path": "^0.12.7" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /public/JS/AJAX2.js: -------------------------------------------------------------------------------- 1 | function getUserInfo() { 2 | var userId = $('#userIdInput').val(); 3 | 4 | $.ajax({ 5 | url: '/getUserInfo/' + userId, 6 | type: 'GET', 7 | success: function(response) { 8 | $('#userInfo').text(response); // Update the content of the
tag
9 | },
10 | error: function(xhr, status, error) {
11 | console.log('Error:', error);
12 | }
13 | });
14 | }
--------------------------------------------------------------------------------
/public/JS/connection.js:
--------------------------------------------------------------------------------
1 | const mysql = require('mysql');
2 |
3 | const connectionMySQL = mysql.createConnection({
4 | host: 'localhost',
5 | user: 'root',
6 | password : '',
7 | database: 'links'
8 | });
9 |
10 | connectionMySQL.connect((error) => {
11 | if (error) {
12 | console.error('Could not connect to database:', error);
13 | } else {
14 | console.log('Connected to the database');
15 | }
16 | });
17 |
18 | module.exports = connectionMySQL;
19 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### Simple-Signup-Nodejs
2 |
3 | ## install xampp and run Apache and MySQL
4 |
5 |
6 |
7 | ## Create table in data base
8 |
9 | ```
10 | CREATE TABLE `user` (
11 | `id` int(11) NOT NULL,
12 | `username` varchar(255) DEFAULT NULL,
13 | `lastname` varchar(255) DEFAULT NULL,
14 | `gender` varchar(255) DEFAULT NULL
15 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
16 | ```
17 |
18 | ## run this command:
19 |
20 | 1. for npm libraries
21 | - ```npm install body-parser```
22 | - ```npm install express```
23 | - ```npm install mysql```
24 | - ```npm install path```
25 | 2. for running server
26 | - ```node app.js```
27 |
28 |
29 |
30 |
31 | ## Webpage
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/public/JS/validate.js:
--------------------------------------------------------------------------------
1 | function validateForm(isAJAX) {
2 | var username = document.getElementById("username").value;
3 | var gender = document.getElementById("gender").value;
4 | var newsletter = document.getElementById("newsletter").checked;
5 |
6 | var regex = /^[a-zA-Z]+$/; // Regular expression to match only text
7 |
8 | if (!username.match(regex)) {
9 | if (!isAJAX) {
10 | alert("Please enter a valid username with no numbers.");
11 | }
12 | return false;
13 | }
14 |
15 | if (username === "" || gender === "" || !newsletter) {
16 | if (!isAJAX) {
17 | alert("Please fill in all the required fields and check the checkbox.");
18 | }
19 | return false;
20 | }
21 |
22 | return true; // Return true if the form is valid
23 | }
24 |
--------------------------------------------------------------------------------
/app.js:
--------------------------------------------------------------------------------
1 | const express = require('express');
2 | const bodyParser = require('body-parser');
3 | const path = require('path');
4 | const app = express();
5 |
6 | const getForm = require(path.join(__dirname, 'public','JS', 'API.js'));
7 | const getInfo2 = require(path.join(__dirname, 'public','JS', 'API2.js'));
8 |
9 | const hostname = '127.0.0.1';
10 | const port = 5050;
11 |
12 | app.use(bodyParser.urlencoded({ extended: false }));
13 | app.use(bodyParser.json());
14 | app.use(express.static(path.join(__dirname, 'public')));
15 |
16 | app.get('/', (req, res) => {
17 | res.sendFile(path.join(__dirname, 'public', 'HTML', 'index.html'));
18 | });
19 |
20 | app.use('/save', getForm);
21 | app.use('/getUserInfo', getInfo2);
22 |
23 |
24 | app.listen(port, hostname, () => {
25 | console.log(`Server running at http://${hostname}:${port}/`);
26 | });
27 |
--------------------------------------------------------------------------------
/public/JS/API2.js:
--------------------------------------------------------------------------------
1 | const express = require('express');
2 | const path = require('path');
3 | const router = express.Router();
4 | const connection = require(path.join(__dirname, 'connection.js'));
5 | const getInformation = (req, res) => {
6 | var userId = req.params.userId;
7 | var query = 'SELECT * FROM user WHERE id = ?';
8 | console.log(userId)
9 | connection.query(query, [userId], function(err, results) {
10 | if (err) {
11 | console.error('Error querying MySQL database: ' + err.stack);
12 | return res.status(500).send('Error querying database');
13 | }
14 |
15 | if (results.length === 0) {
16 | return res.status(404).send('User not found');
17 | }
18 |
19 | var userInfo = 'ID: ' + results[0].id + ', Username: ' + results[0].username +
20 | ', Lastname: ' + results[0].lastname + ', Gender: ' + results[0].gender;
21 |
22 | res.send(userInfo);
23 | });
24 | };
25 |
26 | router.get('/:userId', getInformation);
27 |
28 | module.exports = router;
29 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2024 Ahmad Asadi
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/public/CSS/style.css:
--------------------------------------------------------------------------------
1 | .container {
2 | max-width: 500px;
3 | margin: 0 auto;
4 | padding: 20px;
5 | }
6 |
7 | h2 {
8 | text-align: center;
9 | }
10 |
11 | .form-group {
12 | margin-bottom: 15px;
13 | display: flex;
14 | flex-direction: row;
15 | }
16 |
17 | label {
18 | display: block;
19 | margin-bottom: 5px;
20 | }
21 |
22 | input[type="text"],
23 | select {
24 | width: 100%;
25 | padding: 10px;
26 | border: 1px solid #ccc;
27 | border-radius: 4px;
28 | }
29 |
30 | input[type="submit"] {
31 | width: auto;
32 | padding: 10px 20px;
33 | background-color: #4CAF50;
34 | color: #fff;
35 | border: none;
36 | border-radius: 4px;
37 | cursor: pointer;
38 | }
39 |
40 | input[type="submit"]:hover {
41 | background-color: #45a049;
42 | }
43 |
44 | button{
45 | width: auto;
46 | padding: 10px 20px;
47 | background-color: rgb(230, 164, 42);
48 | color: #fff;
49 | border: none;
50 | border-radius: 4px;
51 | cursor: pointer;
52 | }
53 |
54 | button:hover{
55 | background-color: orange;
56 | }
57 |
58 | input[type="text"] {
59 | padding: 10px;
60 | border: 1px solid #ccc;
61 | border-radius: 4px;
62 | width: 200px;
63 | }
64 |
65 | p {
66 | margin-top: 20px;
67 | font-size: 14px;
68 | }
69 |
--------------------------------------------------------------------------------
/public/HTML/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |