0

I am searching this and other sites for hours now, so I'm getting pretty desperate. No code from many questions with the same topic here works.

I need to insert data into the database and display a message after it is done. Also, I am using AJAX with jQuery so it would be asynchronous. It works just fine, the data gets inserted, but no response message shows.

I am a beginner at PHP and can't understend why this won't work. Relevant code below.

PHP function call:

if(isset($_POST["function"]) && !empty($_POST["function"]) && $_POST["function"] == "cl-add") {
        $dbb->addMember("MyUsername", $_POST["name"]);
        //$dbb is a DataBaseBroker instance
}

PHP function from the Broker:

function addMember($username, $ime) {
    $query = "INSERT INTO clan";
    $query.=" (username, ime) ";
    $query.="VALUES ('".$username."','".$ime."');";

    $result = $this->mysqli->query($query);

    if ($result) {
        echo("You added a member: ".$ime);
    } else {
        $response = "An error occured. Please try again.";
        $response .= "<br>";
        $response .= "Error: ".mysqli_error($connection);
        echo $response;
    }
}

JQuery function declarations:

var addMember = function(name, responseFn) {
    if (name === "") {
        alert("Please enter a name");
        return;
    }
    $.ajax({
        type : 'POST',
        url: '../includes/layout/cl.php',
        dataType : 'json',
        data : {
            'name' : name,
            'function' : 'cl-add'
        },
        success : function(data) {
            responseFn(data); //not working, should alert
        }
    });
}

var responseCallback = function(data) {
    alert(data);
}

And inside $(document).ready():

$(document).on('click', '#cl-add', function(evt) {
    var name = $("#cl_frm input").val();
    addMember(name, responseCallback);
});
6
  • the issue is probably that you should be using datatype : 'text'. If you simply echo out a string in the php code you should handle it as such in the ajax datatype. If you echo it as echo json_encode($my_variable_or_array); you can use it as a json object. Commented Apr 23, 2016 at 16:00
  • Have you tried listening to complete instead of success callback? Commented Apr 23, 2016 at 16:00
  • dataType : 'text', and success : function(data) { alert(data); } check once Commented Apr 23, 2016 at 16:03
  • 22 minutes to check these simple changes? Commented Apr 23, 2016 at 16:26
  • @bucketman yes, now it kind of works. I made so many changes trying to do it on my own I forgot to change 'json' back to 'text'. But now it alerts the whole HTML form structure, too :) Thank you for your help. Commented Apr 23, 2016 at 16:29

2 Answers 2

2

On your code:

dataType : 'json',

The Ajax request is waiting for a JSON response but you are giving a text response.

You should change the dataType: to text or html depending on your needs.

dataType : 'html',

or

dataType : 'text',

PHP changes:

<?php
function addMember($username, $ime)
{
    $query = "INSERT INTO clan";
    $query .= " (username, ime) ";
    $query .= "VALUES ('" . $username . "','" . $ime . "');";

    $result = $this->mysqli->query($query);

    $response = null;

    if ($result) {
        $response = "You added a member: " . $ime;
    } else {
        $response = "An error occured. Please try again.";
        $response .= "<br>";
        $response .= "Error: " . mysqli_error($connection);
    }

    echo $response;
    exit;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that works. But now it displays the entire HTML code of the form in alert. The message is there, but below HTML. Have you ever encountered this problem? thanks for your help.
@NikolaMarkovic I didn't know you were outputting something else on that file cl.php besided that having only functions or php logic. try to separate view template from logic. or add exit; after echo the message response like the code I updated on my answer
0

Change dataType parameter to 'text'. Also you can alert an object in JavaScript, actually you are not trying to alert an object but i just wanted to mention it.

Comments

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.