0

Kind of a newbie in using PHP/Ajax/SQL.

I am currently working in a small project where in we're asked to create a Facebook-esque website using the languages above. I am trying to create pages wherein the user can view the individual posts and the comments in each post. The user can also post comments on the same page. I also have a database to store the different details needed for the project (posts, user details, comments, etc.)

The data acquired from the page displaying the posts is passed through a GET method. There is also an ajax function for inserting the comment data into my database. The problem is, every time I try to insert the data into the database (through AJAX), the following error appears:

Notice: Undefined index: blog_title in C:\xampp\htdocs\postpage.php on line 24

I also noticed that the data I passed through the GET method disappears when I try to call the AJAX function. What can I do to solve the error?

Post data is acquired through here(line 24):

    $blog_title = $_GET['blog_title'];

HTML:

    <!DOCTYPE html>
<html>
    <head>
        <title><?=$blog_title?></title>
    </head>

    <body>
        <div class = "details">
            <h2><?=$row['title']?></h2>
            <h3>Published by: <a link href = "<?=$location?>"><?=$row['author_name']?></a></h3>
            <h4>Date Posted: <?=$row['date']?> at <?=$row['time']?></h4>
        </div>
        <br>
        <p>
            <?=$row['content']?>
        </p><br><br>
        <div class = "commentarea">
            <form>
                <label>Commenting as: <?=$my_username?></label><br>
                <textarea rows = "10" cols = "20" id = "comment" required></textarea>
                <button name = "comment" id = "post" onclick="Comment($('#comment').val(), <?=$my_username?>, <?=$blog_title?>)">Submit Comment</button>
            </form>
        <div>

AJAX Function:

    <script src = "js/jquery.js"></script> 
    <script>
        function Comment(comment, username, title){
        //alert(search + " " + filter);
            $.ajax({
                method:"POST",
                url:"comment.php",
                data:{
                    "comment":comment,
                    "commenter_name":username,
                    "commented_post":title,
                },
                dataType = "json",
                success: function(result){
                    alert("Comment posted.");
                    $("#record").html(result);
                }
            });
        }

    </script>

Thank you in advance!

lol
  • 1
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Professor Abronsius Dec 25 '19 at 09:57

2 Answers2

1

In your Ajax call, you are specifying the method as POST. But in your PHP page, you are trying to get the request using $_GET['blog_title'] and that is why you are getting undefined variable error.

Instead of $_GET you should either use $_POST or $_REQUEST. That is, replace

$blog_title = $_GET['blog_title'];

with

$blog_title = $_POST['blog_title'];

or

$blog_title = $_REQUEST['blog_title'];

Read more about $_REQUEST and $_POST in the docs.

OR

you can change your ajax request as below.

       $.ajax({
            method:"GET",   //changed method to GET
            url:"comment.php",
            data:{
                "comment":comment,
                "commenter_name":username,
                "commented_post":title,
            },
            dataType = "json",
            success: function(result){
                alert("Comment posted.");
                $("#record").html(result);
            }
        });
Lal
  • 14,726
  • 4
  • 45
  • 70
  • Still have the same error. I should have clarified that the POST methods are defined in another PHP file, namely the "comment.php" url specified in the Ajax function. – lol Dec 25 '19 at 10:30
  • On checking your code again, its seems that, you don't have a parameter named `blog_title` in your ajax data. Can you please verify it. – Lal Dec 25 '19 at 10:51
  • I don't have an ajax code for blogs. The functions for posting blogs and accessing blogs are on different files. No parameter named 'blog_title' in ajax data either. The data for the 'blog_title' parameter comes from another file, in which the php file uses the GET method to transfer to the code above. – lol Dec 25 '19 at 12:12
  • Actually, can't understand what exactly you are trying to achieve. Anyway, can you just try to check if you have anything in your GET request. Just try to print everything using `var_dump($_GET)`. – Lal Dec 25 '19 at 12:17
0

Like Lal answer, you first need to convert to the GET method, then you need to declare the blog_title parameter.

<script src = "js/jquery.js"></script> 
        <script>
            function Comment(comment, username, title){
            //alert(search + " " + filter);
                $.ajax({
                    method:"GET", // Changed method to GET
                    url:"comment.php",
                    data:{
                        "comment":comment,
                        "commenter_name":username,
                        "commented_post":title,
                        "blog_title":title  //You must declare the blog_title parameter here
                    },
                    dataType = "json",
                    success: function(result){
                        alert("Comment posted.");
                        $("#record").html(result);
                    }
                });
            }

        </script>
newsshare24h
  • 69
  • 1
  • 11
  • Still have the same error. I should have clarified that the POST methods are defined in another PHP file, namely the "comment.php" url specified in the Ajax function. – lol Dec 25 '19 at 10:34