繁体   English   中英

如何将 jQuery 变量传递给 PHP

[英]How to pass jQuery variables to PHP

我正在用表单替换 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 postIdtext变量传递给 PHP,因此 $edited_text 和 $edited_id 变量将具有相同的信息。 我现在拥有的 PHP 变量的值是临时的,因此页面在显示代码时不会调用错误。 :c

我不是想将变量传递给不同的文件,而是传递给与 replaceWith() 一起放置的表单内的“值”; 在 jQuery 里面。

像这样在您的 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
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM