I am replacing the text of a div with a form so the user can edit it with it, but I don’t know how to pass the content of the variables from jQuery to PHP so the form can get the previous text and the id of the post.
$(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>'; ?>'); });
EDIT:
What I need is to pass the jQuery postId and text variables into PHP, so the $edited_text and $edited_id variables will have the same information. The values that I have right now for those PHP variables are temporary so the page doesn’t call an error when displaying the code. :c
I am not trying to pass the variables to a different file, but to the “value” inside of the form that is placed with the replaceWith(); inside the jQuery.
Advertisement
Answer
Make ajax call in your javascript like this:
$(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 } }); }); });
And in your php code :
<?php $text = $_GET['edited_text']; $post_id =$_GET['edited_id]; //Your code ?>