PHP JS

AJAX CALL TO PHP - SIMPLE EXAMPLES

HTML WRAPPER <div id="demo"></div>

AJAX GET CONTENT

01

AJAX FETCH CONTENT fetch("SCRIPT.PHP") .then(res=>res.text()) .then((txt) =. {   document.getElementById("demo")   .innerHTML = txt; });

AJAX SUBMIT FORM (A)

02

HTML FORM <form id="myForm"             onsubmit="return demo()">   <input type="text" name="foo"               value="bar"/>   <input type="submit" value="GO!"/> </form>

AJAX SUBMIT FORM (B)

03

GET FORM DATA var form = document.getElementById                     ("myForm"); var data = new FormData(form);

AJAX SUBMIT fetch("SCRIPT.PHP", {   method:"POST", body:data }) .then(res => res.text()) .then((txt) => { console.log(txt); });

AJAX SEND JSON DATA

04

FORM DATA var data = new FormData(); var demo = JSON.stringify(["FOO", "BAR"]); data.append("DATA" , demo);

SEND fetch("SERVER.PHP", {   method:"POST", body:data }) .then(res => res.text()) .then((txt) => { console.log(txt); });