On the same page I have several types of code: PHP, JS, and HTML. I want to take the information from the HTML form and go through PHP processing without reloading the page once the send button has been clicked.
PHP gets a value (from the form) that it sends through an API and the API response is displayed on the page
HTML (first on page)
<form method="post">
<input class="display-inline form-postcode" type="text" name="postcode" id="postcode" placeholder="Add your postcode for delivery estimate">
<input class="form-postcode-submit" type="submit" value="Get estimate!">
</form>
PHP (second)
<?php
if(isset($_POST['postcode'])) {
$toPostcode = $_POST['postcode'];
}
// do stuff with $toPostcode
// use the API
// get response
echo $response;
?>
AJAX (third - on the page bottom)
<script>
function submitdata()
{
var postcode = document.getElementById( "postcode" );
$.ajax({
type: 'post',
data: {
postcode:postcode
},
// ???
});
}
</script>
I have to use PHP in the same file because i am working with woocommerce and i get many errors trying to put the file outside
Now i am wondering how can i use all of those in the same page