0

I'm trying to troubleshoot my PHP and JQuery but unable to do so. I've tried using an alert box and console.log to capture the error.However, in the former I only receive [object object] and in the latter, nothing get's published to the console.

Jquery is calling a PHP script to register a user, but is unable to do so.:

$.ajax({   
type: 'POST',
url: '/study/scripts/register.php',
data: {firstName:firstName, lastName:lastName, email:email},
cache: false,
error: function(data) 
{
    alert(data);
    alert("Bad");
},
success: function(data) 
{
    alert(data);
    alert("Good");
}
});

PHP:

<?

require ('../include/config.php');

class regForm
    {
        //Constructor Function
        public function __construct() 
        {
            global $db;
            $db = new dbConnect;

            //Error Flag
            global $errorFlag;
            $errorFlag = 0;
        }
    /*other input validation functions here. They work fine and have been tested*/
    }

    $form = new regForm;

    $tableName = 'user';
    $columnName = array('first_name', 'last_name', 'email_id');
    $columnValue = array($firstName, $lastName, $email);
    $db->insert($tableName, $columnName, $columnValue);
?>

Update:

I enabled preserve log and 'LogXMLhttprequests' in the console settings after which i have received this error:

VM174 jquery.min.js:2 XHR failed loading: POST "http://localhost/study/scripts/register.php".<br>
    send    @   VM174 jquery.min.js:2<br>
    ajax    @   VM174 jquery.min.js:2<br>
    Register    @   VM187 register.js:86<br>
    onclick @   (index):121<br>
6
  • 1
    replace this line alert(data); with this console.log(data); and you should read more details about alert function Commented Dec 5, 2018 at 7:07
  • try console.log(data.d); Commented Dec 5, 2018 at 7:08
  • just stringify it only for view purpose JSON.stringify(data) Commented Dec 5, 2018 at 7:11
  • Do as the above comments suggests. alert() can't show complex types while console.log() can. However, I don't actually see your PHP outputting anything so I'm not sure what result you expect your callback to get? Commented Dec 5, 2018 at 7:12
  • thanks @Kiranramchandarn. I replaced data with console.lg(data.d) and it does reflect an error but the error stays on the console only for a second. Is there a way to keep it there for longer or save it so I can read the complete error? Commented Dec 5, 2018 at 7:13

3 Answers 3

1

As per my comment you should replace this line alert(data); with this console.log(data);

Some Details:

alert() function detail: Specifies the text to display in the alert box, or an object converted into a string and displayed (alert definition Reference) (single variable and not display array)

So, when you pass array into alert() function then it's display object object.

If you want to display array then you should used console.log() function

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

1 Comment

Update: I enabled preserve log and 'LogXMLhttprequests' in the console settings after which i have received this error: VM174 jquery.min.js:2 XHR failed loading: POST "localhost/study/scripts/register.php". send @ VM174 jquery.min.js:2 ajax @ VM174 jquery.min.js:2 Register @ VM187 register.js:86 onclick @ (index):121
0

[object Object] is basically an array

Try this code:

success: function( data ) {

       alert(JSON.stringify(data));
       //or
       console.log(data);

    },

This should show you the array, which will give you the ability to better select the elements to output

1 Comment

Hi @fullstack i put the error as an update with the original post. I'm unable to understand what the error is. If you could provide more details, it will be great. Thanks.
0

The form was loaded in a modal so had to prevent it's default behavior. Added event.preventDefault(); to resolve the issue.

Thanks everyone for your input.

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.