2

Hello i have an ajax form submit and i want to return json data. For some reason it doesnt work as it should. When data.error is return it should give me the message Email is incorect. Same for the other responses. What did i do wrong? My php has json header and also datatype is json.

  $(function() {

    $("form#login").on('submit', function(e){
    e.preventDefault();
            $.ajax({
            type: "POST",
            url: "log.php",
            data: $('form#login').serialize(),
            dataType:"json",
            success: function(data){

                    if(data.error == "yes")
                        {
                        $("#msg").html('Email is incorect.')
                        }
                    else if (data.mandatory == "yes")
                        {
                        $("#msg").html('please complete email and pass')
                        }
                        else if (data.tip =='user')
                        {

                   alert('it works'+ data.id);
                   }
                                   },

        error: function(){
            alert("failure");
            }
              });
    });
});

my php

<?php
header('Content-Type: application/json');
session_start();
include ('core/dbconfig.php');  
$password=$_POST['password'];
$usernume=$_POST['email'];
$hash = hash('sha512', $password);

if ($password=='' or $usernume=='')
{
     $arr[] = array('mandatory'  => 'yes');
     echo json_encode($arr);
}


else
{
$stmt = $dbh->prepare("SELECT * FROM Users where Email=:username and Password= :hashed");   
                $stmt->bindParam(':username', $usernume);
                $stmt->bindParam(':hashed', $hash);
                $stmt->execute();
            if ($row = $stmt->fetch())
            {
                $_SESSION['id_user']=$row['ID_User'];
                 $arr[] = array(     
                'tip'  => 'user',
                'id'   => '3'     
                                );
                echo json_encode($arr);
            }

            else

            {   
                 $arr[] = array('error'  => 'yes',);
                echo json_encode($arr);
            }

}           
?>
user3463807
  • 87
  • 2
  • 14
  • What responses are you getting? – Benjamin Ray Apr 02 '15 at 15:23
  • Do a `console.log(data);` and have a look at the structure of your data. It is not what you expect (hint: `data` is an **array**). See [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) for more help. – Felix Kling Apr 02 '15 at 15:23

2 Answers2

2

turn all your php instances of $arr[] = to $arr =

cmorrissey
  • 8,493
  • 2
  • 23
  • 27
0
               if(data.error != undefined)  ///i think this is the right way
                {
                            $("#msg").html('Email is incorect.')


                }else if(data.length == 0){

                         alert("No users available");

                }else {

                       /* 
                         you will have to do an iteration here of your
                        "data" parent object through your child objects                             
                       */

                      for(var x in data){

                          if (data[x].mandatory == "yes")
                          {
                               $("#msg").html('please complete email and pass')
                          }
                          else if (data[x].tip =='user')
                          {

                              alert('it works'+ data[x].id);

                          }

                      } //close for                        


               } //close else
CG_DEV
  • 788
  • 7
  • 7