How to link Wordpress heartbeat to ajax form
I have a form that $_POST
s an input value, and user's ID that saves the data to the database:
// frontend form
form id="form"
input type="radio" name="location" id="name1" value="1"
label for="name1"Label 1/label
input type="radio" name="location" id="name2" value="2"
label for="name2"Label 2/label
input type="radio" name="location" id="name3" value="3"
label for="name3"Label 3/label
input type="hidden" name="userid" value="{userid}"
/form
// footer script
script
$(function() {
$( "#form" ).change(function(a) {
a.preventDefault();
var formdata = new FormData($(this)[0]);
$.ajax({
method: "POST",
data: formdata,
contentType: false,
processData: false
});
});
});
/script
// validation snippet
?php
$location = isset( $_POST['location'] ) ? $_POST['location'] : '';
$userID = isset( $_POST['userid'] ) ? $_POST['userid'] : '';
update_user_meta( $userID, 'location_current', $location );
?
When the users sets the radio location from their desktop, it send the form to the server, and saves it.
Each user also has a display (tablet) that has no input
but echoes out the location value.
I currently have the tablet refreshing every 5 minutes, but wanted it to be more of a push with WP heartbeat.
I tried using this example: https://gist.github.com/strangerstudios/5888123
But wasnt sure how to have the value of the data['client'] = 'marco';
be data['client'] = '?= $_POST['location']; ?';
each time as it is printed once and not refreshed once the page is loaded or the location changed.