亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 jQuery 變量傳遞給 PHP

如何將 jQuery 變量傳遞給 PHP

侃侃爾雅 2023-09-08 17:15:06
我正在用表單替換 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

?>


查看完整回答
反對 回復 2023-09-08
  • 1 回答
  • 0 關注
  • 287 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號