I have an iframe (I'm embedding YouTube videos) in my post.php template. What I need to happen is that everytime the iframe is mouseover'd, a value is passed via Ajax to a custom field in order store how many times the video has been mousovered. Here's my code for the mouseover:
$("iframe").mouseover(function(){
<?php
global $post;
$id = $post->ID;
$dataString = 'pid=' . $id;
?>
$.ajax({
type: "POST",
url: "<?php bloginfo('template_directory'); ?>/ajax.php",
data: "<?php echo $dataString; ?>",
cache: false,
success: function(html) {
console.log(html);
}
});
});
This section of the code appears to work fine. Here's the code for ajax.php
<?php
define( 'WP_USE_THEMES', false );
require_once( '../../../wp-load.php' );
$pid = $_POST['pid'];
$counter_value = get_post_meta( $pid, 'counter' );
$counter_value = $counter_value + 1;
update_post_meta( $post_id = $pid, $key = 'counter', $value = '$counter_value' );
echo ("success");
?>
The problem seems to be that ajax.php isn't detecting $_POST['pid']. Or maybe that's not the problem, I'm at a bit of a loss now. If anyone has any suggestions, I'd be grateful!
I have an iframe (I'm embedding YouTube videos) in my post.php template. What I need to happen is that everytime the iframe is mouseover'd, a value is passed via Ajax to a custom field in order store how many times the video has been mousovered. Here's my code for the mouseover:
$("iframe").mouseover(function(){
<?php
global $post;
$id = $post->ID;
$dataString = 'pid=' . $id;
?>
$.ajax({
type: "POST",
url: "<?php bloginfo('template_directory'); ?>/ajax.php",
data: "<?php echo $dataString; ?>",
cache: false,
success: function(html) {
console.log(html);
}
});
});
This section of the code appears to work fine. Here's the code for ajax.php
<?php
define( 'WP_USE_THEMES', false );
require_once( '../../../wp-load.php' );
$pid = $_POST['pid'];
$counter_value = get_post_meta( $pid, 'counter' );
$counter_value = $counter_value + 1;
update_post_meta( $post_id = $pid, $key = 'counter', $value = '$counter_value' );
echo ("success");
?>
The problem seems to be that ajax.php isn't detecting $_POST['pid']. Or maybe that's not the problem, I'm at a bit of a loss now. If anyone has any suggestions, I'd be grateful!
Share Improve this question asked Apr 16, 2020 at 14:27 KevKev 572 silver badges12 bronze badges 5 |1 Answer
Reset to default 0The problem was with my counter value. It was initially being set as a string so I converted it to an integer and everything worked perfectly.
Finished code:
Page data being sent from
$("iframe").mouseover(function(){
<?php
global $post;
$id = $post->ID;
?>
$.ajax({
type: "POST",
url: "<?php bloginfo('template_directory'); ?>/ajax.php",
data: "pid=<?php echo $id; ?>",
cache: false,
success: function(html) {
console.log(html);
}
});
});
On the Ajax processing page:
<?php
define( 'WP_USE_THEMES', false );
require_once( '../../../wp-load.php' );
$pid = $_POST['pid'];
$counter_val = get_post_meta( $pid, 'counter', true );
$counter_val = floatval($counter_val);
$counter_val = $counter_val + 1;
echo $counter_val;
update_post_meta( $post_id = $pid, $key = 'counter', $value = $counter_val );
echo "done";
?>
update_post_meta
call? Does this not generate a PHP fatal error for you? You've also left out the 3rd parameter ofget_post_meta
, it might return an array of values rather than a single value – Tom J Nowell ♦ Commented Apr 16, 2020 at 16:38