3
<script type="text/javascript">
  $("#sign_up").on('click', function() {
    $.post('./includes/ajax.php', { action: 'register' } , function(result) {
      var result = JSON.parse(result);
        if(result ) { $("#register_result") = result; document.write(result); }
    });
  });
$("#register_form").submit(function() {
  return false;
});
</script>

In the console it's returning "All inputs must be entered" - which is what I want it to return.

However, the alert is returning [object Object]. Why is this?

1

2 Answers 2

10

console.log will give you a debugging view of an object.

alert will give you a string view of an object.

Objects are converted to strings by calling .toString() on them.

The default toString() function on a basic object will return "[Object object]"

Sign up to request clarification or add additional context in comments.

1 Comment

I edited the original question check my comment on Ancarius' answer.
1

First off, it seems you're trying to get JSON data back. If that's the case why not simply set dataType (the 4th parameter in the $.post function to 'json')?

Also JSON is an object and alert isn't really good at returning objects. If you're looking to debug your code, might I suggest using console.log(result)? It's much more informative and less intrusive.

3 Comments

It's already logging it in the console and I want it to be displayed to the user. I will change the alert(); I edited the original question, how can I have it write to #register_result so that the user can see it say "All inputs must be entered"?
@JohnSmith document.write() has the same "problem" as alert() as described by Quentin. You need to print the object to the console so that you'll see which element contains the message you want to print and print that (document.write( result.msg ) for example if the message happens to be in element msg).
@JohnSmith Since you're going with jQuery, you can use <code>$('#register_result').html(result.msg);</code> described here. While the content returned in the AJAX response is an object, the end elements should be parsable text.

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.