php 变量$testing
正确地回显 ajax 文件,但在 html<?php echo $testing;?>
中不显示任何内容。
html:
<?php
get_header();
?>
<script type="text/javascript">
var hrefValue;
jQuery(document).ready(function($) {
$('#bio-box').find('a').mouseover(function() {
hrefValue = ($(this).attr('href'));
$.ajax({
url: '/jakubtrz-portfolio/wp-admin/admin-ajax.php',
data: {
'action': 'php_tutorial',
'php_test': hrefValue
},
success: function(data){
console.log("sent")
}
});
});
});
</script>
<?php echo $testing;?>
<main id="primary" class="site-main">
<div class="container position-relative my-7">
<div class="row">
<div class="col-lg-6" id="bio-box">
<a href="http://localhost:8888/jakubtrz-portfolio/quod-si-ita-se-habeat-non-possit/">Example post link</a>
<a href="http://localhost:8888/jakubtrz-portfolio/quid-ergo-aliud-intellege/">Example post link2</a>
</div>
<div class="col-lg-6">
<div class="featured-image">
<?php
$post_id = url_to_postid($testing);
echo get_the_post_thumbnail($post_id);
?>
</div>
</div>
</div>
</div>
</main><!-- #main -->
<?php
get_footer();
函数.php:
function our_tutorial(){
if(isset($_REQUEST)){
$testing = $_REQUEST['php_test'];
echo $testing;
global $wpdb;
$wpdb->insert(
$wpdb->prefix.'lms_enroll',
[
'ID' => $testing
]
);
}
die();
}
add_action('wp_ajax_php_tutorial', 'our_tutorial');