HTML
<form method="post" action="https://php.tomgdow.com" id="form_one">
<div>
<!-- <label for="name">Name</label> -->
<input type="hidden" id="name" name="param1" value="Hello">
<input type="hidden" id="hidden" name="param2" value="You clicked the button!">
</div>
<div>
<input id="mybtn" type="image" src="../images/myButton2.png" alt="Blue Button">
</div>
<fieldset class="output">
<div>
<output for="hidden1" id="out-hidden1"></output>
</div>
<div>
<output for="hidden2" id="out-hidden2"></output>
</div>
</fieldset>
</form>
JavaScript
async function sendData() {
const formData = new FormData(form_one);
try {
const response = await fetch("https://php.tomgdow.com", {
method: "POST",
body: formData
});
let result = await response.text();
console.log(result);
let responseArray = result.split('&');
document.getElementById('out-hidden1').value = responseArray[0];
document.getElementById('out-hidden2').value = responseArray[1];
} catch (e) {
console.error(e);
}
}
document.getElementById('form_one').addEventListener('submit', function (event) {
event.preventDefault();
sendData();
});
document.getElementById("mybtn").addEventListener('mouseleave', function () {
document.getElementById('out-hidden1').style.backgroundColor = "black"
document.getElementById('out-hidden1').style.color = "white"
document.getElementById('out-hidden2').style.backgroundColor = "black"
document.getElementById('out-hidden2').style.color = "white"
})
document.getElementById("mybtn").addEventListener('mouseover', function () {
document.getElementById('out-hidden1').style.backgroundColor = "revert"
document.getElementById('out-hidden1').style.color = "revert"
document.getElementById('out-hidden1').value = "";
document.getElementById('out-hidden2').style.backgroundColor = "revert"
document.getElementById('out-hidden2').style.color = "revert"
document.getElementById('out-hidden2').value = "";
})
PHP
// index.php (server)
<?php
echo
htmlspecialchars($_POST["param1"])."&"
.htmlspecialchars($_POST["param2"])."&"
.htmlspecialchars($_SERVER['HTTP_HOST']);
?>