【发布时间】:2022-01-25 15:21:52
【问题描述】:
我目前正在学习基本的 php 和 jQuery。
我创建了在鼠标悬停时获取 url 的脚本,并将其发送到 php。
问题是,如果我想将此数据传递给 php 变量,它似乎不起作用,因为它只回显“'这是我们的 JS 变量:'”
脚本:
<script type="text/javascript">
var hrefValue;
jQuery(document).ready(function($) {
$('#bio-box').find('a').mouseover(function() {
hrefValue = ($(this).attr('href'))
console.log(hrefValue)
});
$.ajax({
url: 'jakubtrz-portfolio/wp-admin/admin-ajax.php',
data: {
'action': 'php_tutorial',
'php_test': hrefValue
},
success: function(data){
console.log("happy")
}
});
});
</script>
functions.php:
function our_tutorial(){
if(isset($_REQUEST)){
$testing = $_REQUEST['php_test'];
echo 'This is our JS Variable :'.$testing;
global $wpdb;
$wpdb->insert(
$wpdb->prefix.'lms_enroll',
[
'ID' => $testing
]
);
}
die();
}
add_action('wp_ajax_php_tutorial', 'our_tutorial');
【问题讨论】:
-
$_GET['php_test']? -
还是同样的问题@Hast
-
请显示
console.log(hrefValue);和print_r($_REQUEST);的结果。 -
@trz
var_dump($_REQUEST)这里的调试比较重要。