PHP JS

HOW TO CALL PHP SCRIPT FROM JAVASCRIPT

DATA TO SEND var data = new FormData(); data.append("KEY", "VALUE");

AJAX CALL

01

AJAX CALL PHP var xhr = new XMLHTttpRequest(); xhr.open("POST", "SCRIPT.PHP"); xhr.onload = function () { alert(this.response); }; xhr.send(data);

FETCH API

02

DATA TO SEND var data = new FormData(); data.append("KEY", "VALUE");

FETCH PHP SCRIPT fetch("SCRIPT.PHP",    { method: "post", body: data }) .then(res => res.text()) .then((txt) => { console.log(txt); }) .catch((err) => { console.log(err); });

REDIRECT URL

03

SET URL var src = "SCRIPT.PHP?KEY=VALUE"; src = encodeURI(src); REDIRECT window.location.href = src;

SUBMIT FORM

04

HTML FORM <form id="ninja" action="SCRIPT.PHP">   <input type="hidden" name="KEY"                value="VALUE"/> </form>

SUBMIT FORM WITH JS document.getElementById("ninja")   .submit();

SCRIPT TAG TO PHP

05

var tag = document.createElement("script"); var src = "SCRIPT.PHP?KEY=VALUE"; tag.src = encodeURI(src); document.head.appendChild(tag);

NOT RECOMMENDED - UNORTHODOX METHOD.