JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://github.com/douglascrockford/JSON-js/raw/8e0b15cb492f63067a88ad786e4d5fc0fa89a241/json2.js"></script>
<form id="form1">
<input id="code" type="text" name="code" />
<input id="name" type ="text" name="name" />
<input type="submit" id="submit" />
</form>
JavaScript
$(function() {
$('#submit').click(function(evt) {
//Stop submit button from posting form (default behaviour)
evt.preventDefault();
//Create a javascript object literal
var p = {
Name: $('#name').val(),
Code: $('#code').val()
};
/*
JSON.stringify built in function to IE9+ / FireFox / Chrome browsers that
converts JavaScript Object Literals into JSON. Include JSON2.js for all others found at
https://github.com/douglascrockford/JSON-js/blob/master/json2.js
I prefer using stringify as it reduces the need to manually construct JSON
however for this simple example you could do this
Replace "json" : JSON.stringify(p) with
{ "Name" : $('#name').val(), "Code" : $('#code').val() }
*/
// Replace echo/json with url of php class
$.post('/echo/json/', {
"json": JSON.stringify(p)
}, function(result) {
//Result from Server
alert(result.Name);
});
// Server Side
// <?php echo $_POST["Name"]; ?>
// <?php echo $_POST["Code"]; ?>
});
});