0

I made a blogging platform and I have an ajax updated page where I can select which article to display and his comments and add comments. When I leave a comment it takes the logged user info, the article on which the comment has been made but the comment value is not taken to store it in the database. This is the code :

<div align="center">
  <h3>Comentarii:</h3>
  <form method="POST">
    <textarea rows="4" cols="50" name="comentariu" placeholder="Comenteaza">
        </textarea><br>
    <input type="submit" name="submit"><br>
    <hr>
  </form>
</div>
<?php
  $comnou = $_POST['comentariu'];
  $comuser = $_SESSION['user'];
  $conadaugacom = mysqli_connect("localhost", "root", "", "blog");
  $sqladaugacom = "insert into comentarii (continut_comentarii, 
  user_comentarii, articol_comentarii) values ('$comnou', '$comuser', '$ta')";
  mysqli_query($conadaugacom, $sqladaugacom);
  mysqli_close($conadaugacom);
?>

AJAX Code ->

function showUser(str) {
  if (str == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
  } else {
    if (window.XMLHttpRequest) {
      xmlhttp = new XMLHttpRequest();
    } else {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.getElementById("txtHint").innerHTML = this.responseText;
      }
    };
    xmlhttp.open("GET", "getuser.php?q=" + str, true);
    xmlhttp.send();
  }
}
4
  • is $ta defined elsewhere? This is wide open to sql injection btw Commented Mar 17, 2018 at 18:42
  • You are missing to show the Ajax part Commented Mar 17, 2018 at 18:47
  • I inserted the ajax part Commented Mar 17, 2018 at 18:52
  • You are using get but read post Commented Mar 17, 2018 at 19:16

1 Answer 1

1

Make sure that before POST request serialize the data from form.

html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div align="center">
  <h3>Comentarii:</h3>
  <form method="POST">
    <textarea rows="4" cols="50" name="comentariu" placeholder="Comenteaza">
        </textarea><br>
    <input type="submit" name="submit"><br>
    <hr>
  </form>
</div>

Jquery

<script>
     $('input').submit(function(e){
        e.preventDefault();

        var data = $('form').serialize();
        $.ajax({
           url: 'your_url_to_post.php',
           data: data,
           success: function(response){

           },
           type: 'POST'
        });
     });
</script>

php file

add this line to check comentariu not empty

if(isset($_POST['comentariu'])){
     $comentariu = $_POST['comentariu];
}
Sign up to request clarification or add additional context in comments.

6 Comments

Please answer a few questions until you have the 50 rep needed to comment. This is not an answer.
i know i am new here
NOW it’s an answer
You however assume jQuery
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.