Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created May 31, 2023 02:34
Show Gist options
  • Save code-boxx/6e287165e68b961a6d0f152a3d7a2c4e to your computer and use it in GitHub Desktop.
Save code-boxx/6e287165e68b961a6d0f152a3d7a2c4e to your computer and use it in GitHub Desktop.
Javascript Call PHP Script

CALL PHP SCRIPT IN JAVASCRIPT

https://code-boxx.com/call-php-file-from-javascript/

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>AJAX Call PHP</title>
</head>
<body>
<!-- (A) HTML FORM -->
<form id="myForm" onsubmit="return ajaxcall();">
<input type="text" name="name" value="Jon" required>
<input type="email" name="email" value="jon@doe.com" required>
<input type="submit" value="Save">
</form>
<!-- (B) JAVASCRIPT -->
<script>
function ajaxcall () {
// (B1) GET FORM DATA
var data = new FormData(document.getElementById("myForm"));
// (B2) AJAX CALL
var xhr = new XMLHttpRequest();
xhr.open("POST", "x-dummy.php");
xhr.onload = function () {
console.log(this.response);
};
xhr.send(data);
return false;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Fetch PHP</title>
</head>
<body>
<!-- (A) HTML FORM -->
<form id="myForm" onsubmit="return fetchcall();">
<input type="text" name="name" value="Jon" required>
<input type="email" name="email" value="jon@doe.com" required>
<input type="submit" value="Save">
</form>
<!-- (B) JAVASCRIPT -->
<script>
function fetchcall () {
// (B1) GET FORM DATA
var data = new FormData(document.getElementById("myForm"));
// (B2) FETCH
fetch("x-dummy.php", { method: "POST", body: data })
.then(res => res.text())
.then(txt => console.log(txt))
.catch(err => console.error(err));
return false;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Redirection with Query String</title>
</head>
<body>
<!-- (A) HTML FORM -->
<form onsubmit="return redirect();">
<input type="number" id="numA" value="123" required>
<input type="number" id="numB" value="456" required>
<input type="submit" value="Go!">
</form>
<!-- (B) JAVASCRIPT -->
<script>
function redirect () {
// (B1) URL SEARCH PARAMS (QUERY STRING)
var params = new URLSearchParams();
params.set("numA", document.getElementById("numA").value);
params.set("numB", document.getElementById("numB").value);
// (B2) REDIRECTION
window.location.href = "3-redirect.php?" + params.toString();
return false;
}
</script>
<!-- (C) RESULTS -->
<?php
if (isset($_GET["numA"])) { require "x-dummy.php"; }
?>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>NINJA Form Submission</title>
</head>
<body>
<!-- (A) HIDDEN HTML FORM -->
<form id="ninja" method="post" style="display:none;">
<input type="hidden" id="numA" name="numA" required>
<input type="hidden" id="numB" name="numB" required>
</form>
<!-- (B) JAVASCRIPT -->
<input type="button" value="Go!" onclick="go()">
<script>
function go () {
document.getElementById("numA").value = "123";
document.getElementById("numB").value = "456";
document.getElementById("ninja").submit();
}
</script>
<!-- (C) RESULTS -->
<?php
if (isset($_POST["numA"])) { require "x-dummy.php"; }
?>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Unorthodox - Insert Script Tag</title>
</head>
<body>
<!-- (A) HTML FORM -->
<form onsubmit="return bad();">
<input type="number" id="numA" value="123" required>
<input type="number" id="numB" value="456" required>
<input type="submit" value="Add">
</form>
<!-- (B) JAVASCRIPT -->
<script>
function bad () {
// (B1) URL SEARCH PARAMS (QUERY STRING)
var params = new URLSearchParams();
params.set("numA", document.getElementById("numA").value);
params.set("numB", document.getElementById("numB").value);
params.set("BAD", 1);
// (B2) CREATE NEW SCRIPT & INJECT INTO HEAD
// NOT A GOOD WAY, AND NOT RECOMMENDED.
var tag = document.createElement("script");
tag.src = encodeURI("x-dummy.php?" + params.toString());
document.head.appendChild(tag);
return false;
}
</script>
</body>
</html>
<?php
if (isset($_GET["BAD"])) {
$total = $_GET["numA"] + $_GET["numB"];
echo "alert($total);";
} else {
print_r($_POST);
print_r($_GET);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment