Send HTML Form Data as JSON via Ajax

Today, We want to share with you Send HTML Form Data as JSON via Ajax.In this post we will show you form submit json without ajax, hear for save html form data to json file we will give you demo and example for implement.In this post, we will learn about how to convert html form field values to a json object with an example.

Send HTML Form Data as JSON via Ajax

There are the Following The simple About Send HTML Form Data as JSON via Ajax Full Information With Example and source code.

As I will cover this Post with live Working example to develop html form to json object, so the html form to json object for this example is following below.

save html form data to json file

JSON Part

var userData = {};
 var formData = $("#myform").serializeArray();
	 $.each(formData, function() {
	 if (userData[this.name]) {
	 if (!userData[this.name].push) {
		userData[this.name] = [userData[this.name]];
	 }
		userData[this.name].push(this.value || '');
	 } else {
		userData[this.name] = this.value || '';
	 }
 });
 console.log(userData);

index.html

<!DOCTYPE html>
<html lang="en">
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="jquery.serializeJSON.min.js"></script>
<script>
$(document).ready(function(){
$("#saveUser").click(function(e)
{
var ProductForm = $("#live_frmData").serializeJSON();
console.log(ProductForm);
 $.ajax(
 {
 url : "do_submit.php",
 type: "POST",
 data : {valArray:ProductForm},
 success:function(maindta)
 {

alert(maindta);

 },
 error: function(jqXHR, textStatus, errorThrown)
 {
 }
 });
 e.preventDefault(); //STOP default action

});
});</script>
</body>
<div id="message"> </div>
<form name="live_frmData" id="live_frmData" method="POST">
User First Name: <input type="text" name="usename" value =""/> <br/>
User Last Name: <input type="text" name="lastname" value ="" /> <br/>
Email : <input type="text" name="email" value=""/> <br/>
<input type="button" class="btn btn-info" id="saveUser" value="Save User"></form>
</html>

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Send HTML Form Data as JSON via Ajax.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   PHP Creating Dynamic Directory (Folder) Example
  2.   PHP Create PDF file using mpdf | tcpdf | fpdf | php html to pdf
  3.   Simple Laravel Form Validation Example
  4.   jQuery Delete Confirm in Laravel 5.7 Example
  5.   PHP - MySQLi Insert Update Delete CRUD Operation using AngularJS
  6.   Send email via Gmail SMTP server in Laravel 7/6
  7.   Bitbucket repositories | Create repositories & add files
  8.   File Upload - Auto Resize Compress-Reduce image using PHP
  9.   PHP Laravel 5.6 Dynamically Add Active class
  10.   Image Uploads with Angular 9 Tutorial