如何将 jQuery 变量传递给 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 postId和text变量传递到 PHP 中,因此 $edited_text 和 $edited_id 变量将具有相同的信息。我现在拥有的这些 PHP 变量的值是临时的,因此页面在显示代码时不会调用错误。:C


我并不是试图将变量传递到不同的文件,而是传递到使用replaceWith();放置的表单内部的“值”。在 jQuery 里面。


侃侃尔雅
浏览 121回答 1
1回答

jeck猫

在 JavaScript 中进行 ajax 调用,如下所示:&nbsp; &nbsp; $(document).ready(function() {&nbsp; &nbsp; $('.editar').click(function() {&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: 'yourUrl.php',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'GET',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: { edited_text: text,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;edited_id=postId },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Do something here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error: function(XMLHttpRequest, textStatus, errorThrown)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //case error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp;});&nbsp; &nbsp; &nbsp; &nbsp;});在你的 php 代码中:<?php$text = $_GET['edited_text'];$post_id =$_GET['edited_id];&nbsp;//Your code?>
打开App,查看更多内容
随时随地看视频慕课网APP