Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 1, 2023 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/69c5d0655bdda49f843a7b41dfc981c1 to your computer and use it in GitHub Desktop.
Save code-boxx/69c5d0655bdda49f843a7b41dfc981c1 to your computer and use it in GitHub Desktop.
Javascript Send JSON Data To PHP

JAVASCRIPT SEND JSON DATA TO PHP

https://code-boxx.com/javascript-send-json-php/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>JSON TO PHP</title>
<meta charset="utf-8">
</head>
<body>
<!-- (A) HTML FORM -->
<form id="demoA" onsubmit="return demoA()">
<input type="text" name="name" required value="Job Doe">
<input type="email" name="email" required value="job@doe.com">
<input type="submit" value="Go">
</form>
<!-- (B) JAVASCRIPT -->
<script>
function demoA () {
// (B1) GET FORM DATA, CONVERT TO JSON
var form = new FormData(document.getElementById("demoA")),
data = JSON.stringify(Object.fromEntries(form.entries()));
console.log(data); // this is a string!
// (B2) DATA TO SEND TO PHP
var send = new FormData();
send.append("data", data);
// (B3) SEND VIA POST
fetch("x-dummy.php", { method:"post", body:send })
.then(res => res.text())
.then(txt => {
console.log(txt); // server response
// do your stuff after server response
})
.catch(err => console.error(err));
// (B4) PREVENT FORM SUBMIT
return false;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JSON TO PHP</title>
<meta charset="utf-8">
</head>
<body>
<!-- (A) HTML FORM -->
<form id="demoB" onsubmit="return demoB()">
<input type="text" name="name" required value="Joe Doe">
<input type="email" name="email" required value="joe@doe.com">
<input type="submit" value="Go">
</form>
<!-- (B) JAVASCRIPT -->
<script>
function demoB () {
// (B1) GET FORM DATA, CONVERT TO JSON
var form = new FormData(document.getElementById("demoB")),
data = JSON.stringify(Object.fromEntries(form.entries()));
console.log(data); // this is a string!
// (B2) DATA TO SEND TO PHP
var send = new URLSearchParams();
send.append("data", data);
// (B3) SEND VIA GET
fetch("x-dummy.php?" + send.toString())
.then(res => res.text())
.then(txt => {
console.log(txt); // server response
// do your stuff after server response
})
.catch(err => console.error(err));
// (B4) PREVENT FORM SUBMIT
return false;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JSON TO PHP</title>
</head>
<body>
<!-- (A) TEST BUTTON -->
<input type="button" value="Go" onclick="demoC()">
<!-- (B) JAVASCRIPT -->
<script>
function demoC () {
// (B1) JSON ENCODE DATA TO SEND
var data = JSON.stringify({
name : "Joy Doe",
email : "joy@doe.com"
});
// (B2) FORM DATA
var send = new FormData();
send.append("data", data);
// (B3) SEND DATA TO PHP VIA POST
fetch("x-dummy.php", { method:"post", body:send })
.then(res => res.text())
.then(txt => {
console.log(txt); // server response
// do your stuff after server response
})
.catch(err => console.error(err));
}
</script>
</body>
</html>
<?php
// (A) ECHO POST & GET DATA
print_r($_POST);
print_r($_GET);
// (B) JSON DECODE TO GET ARRAY
// $data = json_decode($_POST["data"], true);
// $data = json_decode($_GET["data"], true);
// print_r($data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment