我正在用表單替換 div 的文本,以便用戶可以用它來編輯它,但我不知道如何將變量的內容從 jQuery 傳遞到 PHP,以便表單可以獲取之前的文本和 id帖子。$(document).on('click', '.editar', function(e) { var that = $(this); //Post id var box = that.closest('.comment-parent'); var link = box.find('.postId'); var postId = link.attr('name'); //This is the id //Text var parent = that.parent().parent(); var sibling = parent.siblings('.text'); var text = sibling.text(); //This is the text sibling.replaceWith('<?php $edited_text = 'Text'; $edited_id = 0; echo '<form action="Includes/comment-edit.inc.php" method="post"><input type="hidden" name="postId" value="'.$edited_id.'"><textarea name="edited-text">'.$edited_text.'</textarea><br><button type="submit" name="edited-submit" style="float: right;">Update</button></form>'; ?>');});編輯:我需要的是將 jQuery postId和text變量傳遞到 PHP 中,因此 $edited_text 和 $edited_id 變量將具有相同的信息。我現在擁有的這些 PHP 變量的值是臨時的,因此頁面在顯示代碼時不會調用錯誤。:C我并不是試圖將變量傳遞到不同的文件,而是傳遞到使用replaceWith();放置的表單內部的“值”。在 jQuery 里面。
1 回答

jeck貓
TA貢獻1909條經驗 獲得超7個贊
在 JavaScript 中進行 ajax 調用,如下所示:
$(document).ready(function() {
$('.editar').click(function() {
$.ajax({
url: 'yourUrl.php',
type: 'GET',
data: { edited_text: text,
edited_id=postId },
success: function() {
//Do something here
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
//case error }
});
});
});
在你的 php 代碼中:
<?php
$text = $_GET['edited_text'];
$post_id =$_GET['edited_id];
//Your code
?>
添加回答
舉報
0/150
提交
取消