Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8595901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:33:00+00:00 2026-06-12T00:33:00+00:00

Basically I am trying to post a comment via AJAX and then load the

  • 0

Basically I am trying to post a comment via AJAX and then load the new comment into the HTML. I am having a problem with what I think is my AJAX success function. The data gets added to the database fine, but I still get an error “Sorry, unexpected error. Please try again later.” as set inside the success function itself.

JS:

function newComment(event) {        

    event.preventDefault();

    //Get the data from all the fields
    var comment = $('textarea');
    var product_id = $('input[name=product_id]');

    var form_data = {
        comment : comment.val(),
        product_id : product_id.val()
    };

    //Simple validation to make sure user entered something
    if (comment.val()=='') {
        alert('Comment cannot be blank!');
        return false;
    }


    //show the loading sign
    $('#loading').show();

    //start the ajax
    $.ajax({
        //this is the php file that processes the data and send mail
        url: "main/ajax_comment", 

        //GET method is used
        type: "POST",

        //pass the data         
        data: form_data,

        //success
        success: function (c) {              
            //check the status
            if (c.status==1) {           
                $('Posted By: ' + c.username + '<br> Body: ' + c.body + '<br> Posted On: ' + c.date + '<hr>').appendTo('#new_comment');

                $('#loading').hide();

                $('#new_comment').slideDown();

            //0/false (send mail failed)
            } else alert('Sorry, unexpected error. Please try again later.');               
        }
    });
}

PHP:

public function ajax_comment()
{
    sleep(4);
        $this->main_model->add_comment($this->session->userdata('user_id'), $this->input->post('product_id'), $this->input->post('comment'));
        $i = $this->auth_model->get_data_id($this->session->userdata('user_id'), 'user');
        $return = array(
            'status' => '1',
            'username' => $i->username,
            'body' => $this->input->post('comment'),
            'date' => time()
            );
        echo json_encode($return);
}

HTML:

<form action="<?php echo base_url()."main/comment"; ?>" method="post">
<input type="hidden" name="product_id" value="<?php echo $p->u_product_id; ?>">
<textarea name="new_comment"></textarea><br>
<input type="submit" name="submit_comment" id="submit_comment" value="Post">
<div id="loading" style="display: none;">
    <img src="<?php echo base_url()."assets/img/load.gif"; ?>">
</div>

Am I doing something wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:33:01+00:00Added an answer on June 12, 2026 at 12:33 am

    You seem to be sending json back to the success function so you should add a dataType:

    $.ajax({
        //this is the php file that processes the data and send mail
        url: "main/ajax_comment", 
    
        //GET method is used
        type: "POST",
    
        //pass the data         
        data: form_data,
    
        // data type
        dataType: 'json',       /* added */
    
        //success
        ...
    

    If that does not solve the problem, check what your c variable contains, perhaps some php warnings were echoed as well, invalidating the returned json.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm basically trying to do a dropdown jquery plugin. HTML <script type='text/javascript' src='script.js'></script> <div
I've been learning asp.net mvc 3. So, basically I'm trying to create a new
I'm trying to mimic something similar to FB. Basically, users can post comments in
I have a problem with the form I'm trying to create. Basically, it does
I have a problem with the Form I'm trying to create. Basically, it does
I have a pretty specific one here. I'm basically Trying to post an array
So basically I am trying to have div.show fade in on hover and then
Basically i'm trying to set something where a user enters some text into a
So basically I have been trying to insert columns into my third table from
Trying to post a form with jquery and the response data is the html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.