0

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.

Elena
  • 83
  • 6
  • AJAX is what you are looking for – B001ᛦ Jul 31 '20 at 20:32
  • I'm not clear on exactly what you are trying to do, so unless you are usong ajax then no, you can't pass jQuery variables to PHP. PHP is server-side and jQuery is client-side. – FluffyKitten Jul 31 '20 at 20:33
  • Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Jeto Jul 31 '20 at 20:33
  • @B001ᛦ, I'm not sure how. Can you please enlighten me? I'm really new to programming. I've used AJAX to post a form without refreshing the page, but that's it. I don't how else to use it. – Elena Jul 31 '20 at 20:34
  • _..I've used AJAX to post a form..._ The same way you send your js/jquery variables/values to the php side and then do what ever you want to do with them - Anyway there are tons of examples and tutorials online :) – B001ᛦ Jul 31 '20 at 20:36
  • @FluffyKitten, if I use AJAX, how would it be? I've only used it for submitting forms without refreshing the page, but not to pass variables. – Elena Jul 31 '20 at 20:36
  • @B001ᛦ, I haven't send any variable from AJAX to PHP before. That's why I am asking on how to use it. I will edit the post to show you my AJAX code. – Elena Jul 31 '20 at 20:38
  • Are you just talking about passing parameters to a new page? And Lawrence has a different interpretation again below... I think you need to edit your question to clearly explain what you are trying to do, because at the moment we don't know how to answer your question! – FluffyKitten Jul 31 '20 at 20:41
  • @FluffyKitten, I'm sorry. I edited the question. – Elena Jul 31 '20 at 20:44
  • 1
    As a side note: Generally if you are setting up and using a basic variable in JS, do without the `$` in front (like `var simple = 123;`. If you are assigning an object element to a variable, then its ok to name the variable with the `$` in front so you know '*that variable is actually an object*'. It helps keep things clear in your code the intent of JS variables. Not required, entirely subjective. – IncredibleHat Jul 31 '20 at 20:58
  • @IncredibleHat, thank you for the advice. I will take away the $ sign so people will understand it better, then. ^^ – Elena Jul 31 '20 at 21:00

1 Answers1

0

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
?>
Riddhijain
  • 487
  • 2
  • 8