我覺得我很接近,但我一直在堅持。這是一個小背景。如何使用從PHP函數返回的jQuery變量
我正在使用Wordpress,並且我有一個圖庫插件。每個畫廊都有一個ID,我試圖在用戶點擊畫廊鏈接時完成,畫廊將更改爲相應的畫廊。畫廊正在顯示像這樣:
<?php echo do_shortcode('[slideshow gallery_id="1"]'); ?>
或
<?php if (function_exists('slideshow')) { slideshow(true, "1", false, array()); } ?>
都做同樣的事情 -
其中gallery_id
指定了正確的畫廊。當用戶點擊鏈接時,我使用AJAX運行一些代碼以獲取正確的圖庫ID。在gallery.php
jQuery('.gallery-btn').click(function() {
var galleryId = jQuery(this).data('gallery');
jQuery.ajax({
url: "/wp-content/themes/thetheme/gallery.php",
data: {action: galleryId},
type: 'post',
success: function(output) {
console.log(output);
}
})
})
PHP代碼:
<?php
if(isset($_POST['action']) && !empty($_POST['action'])) {
$galleryId = $_POST['action'];
passId($galleryId);
}
function passId($id) {
echo $id;
}
?>
output
返回我需要再通入PHP代碼[slideshow gallery_id="$theID"]
的ID。
如何在我的PHP代碼中使用成功函數中的output
?
什麼php代碼你想使用JavaScript變量'輸出'? – Pitchinnate
<?php echo do_shortcode('[slideshow gallery_id =「$ ID」]'); ?> – Romes