Logo

dev-resources.site

for different kinds of informations.

Send Form Data With Ajax

Published at
9/16/2023
Categories
javascript
webdev
jquery
ajax
Author
yubaeloualidi
Categories
4 categories in total
javascript
open
webdev
open
jquery
open
ajax
open
Author
13 person written this
yubaeloualidi
open
Send Form Data With Ajax

Hello For Everyone !

Steps:

  1. Create an index.html file (HTML)
  2. Create a app.js file (JavaScript)

// You Can Also Style Your Form If You Need That !

HTML Codes:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>
       Send Form Data With Ajax 
       By (https://dev.to/yubaeloualidi)
    </title>
</head>
<body>
    <div id="message"></div>
    <form id="form">
        <input type="text" name="username" autocomplete="off" required="required" />
        <input type="password" name="password" autocomplete="new-password" required="required" />
        <button id="login" type="submit">Login</button>
    </form>

    <script src="app.js"></script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

JavaScript Codes:

const request = new XMLHttpRequest(),
    form = document.getElementById("form"),
    login = document.getElementById("login"),
    message = document.getElementById("message");

form.onsubmit = event => {
    event.preventDefault();
    const formData = new FormData(form);
    var body = "";

    for (const [key, value] of formData) {
        body += `${key}=${value}&`;
    }

    body = body.substring(0, body.length - 1);

    request.addEventListener('readystatechange', () => {
        if (request.readyState === 4 && request.status === 200) {
            console.log(JSON.parse(request.responseText));
        }
    })

    request.open('POST', 'read.php');
    // Warning: you need to change the request URL (read.php) with 
       your personal

    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
    request.send(body);
}

Enter fullscreen mode Exit fullscreen mode

When You Click on The Button (login), You Can Check if The request is sended in (web developers tool -> network -> your file)

Image description

ajax Article's
30 articles in total
Favicon
How to Load More data using ajax pagination on scroll in laravel 11 Example
Favicon
Ajax: Revolutionizing Web Interaction - A Comprehensive Guide
Favicon
How to create ajax How to create ajax dependent dropdown in laravel 11
Favicon
A Comprehensive Guide with XHR, Fetch API, Axios and jQuery AJAX
Favicon
Vaadin, the battery-included server-side AJAX framework
Favicon
Augmenting the client with Alpine.js
Favicon
Augmenting the client with Vue.js
Favicon
A short history of AJAX and SSR
Favicon
Experimenting with AJAX and APIs: A Comprehensive Guide for Beginners
Favicon
Asynchronous Requests in JavaScript: AJAX, JQUERY, FETCH and AXIOS.
Favicon
JS: Geek Out with AJAX
Favicon
Exploring django-ajax: Simplifying AJAX Integration in Django
Favicon
Exploring The Power Of AJAX Flex In Web Development
Favicon
Laravel 11 Ajax Form Submit With Validation
Favicon
com.girl.brashcrab
Favicon
Some uncommon Ajax techniques that most people don't know
Favicon
A Simple Guide to Developing AJAX-Driven Plugins for WordPress
Favicon
Do you know AJAX?
Favicon
419 Page expired Laravel 10|9 postman, Ajax Post, Form Submit Login
Favicon
crud operation using ajax in laravel 10 with popup modal
Favicon
How do I get my text to connect to the ajax call I am trying to connect from the ajax call to the controller?
Favicon
Trending Tech: HTMX
Favicon
Send Form Data With Ajax
Favicon
Dependent Country State City Dropdown using jQuery Ajax in Laravel 10
Favicon
What is AJAX in JavaScript ?
Favicon
Laravel 10 Ajax Crop and Upload Image using Croppie js
Favicon
Laravel 10 Ajax CRUD with Image Upload Tutorial
Favicon
Tab Content Structure with Ajax and PHP
Favicon
Create a mini facebook clone in laravel and ajax
Favicon
How To Cancel an AJAX Request in JavaScript Using the AbortController

Featured ones: