title: Accessing JSON values in JQUERY/AJAX based on condition
body: <p>Please be patient because I know this question might have been answered but I have not been able to find it. I have been working on a project &amp; lately I just started using AJAX.</p>
  
  <p>My JSON is coming from PHP, which includes errors and success, now the issue is how do I access success(if the registrattion is successful to display as Text(not alert)) and display errors when registration fails.</p>
  
  <p>What conditions should be used?</p>
  
  <pre><code>&lt;div class="remodal" data-remodal-id="RegisterModal"&gt;
    &lt;div class="popup-1"&gt;
        &lt;div class="popup-content" id="register_container"&gt;
            &lt;div id="register_title" class="popup-title text-purple"&gt;Sign Up&lt;/div&gt;
            &lt;div class="reg-notification"&gt;
                &lt;p&gt;You Successfully registered to our website and now you can login and use our services&lt;/p&gt;
                &lt;a href="feed.php" class="popup-link"&gt;Continue&lt;/a&gt;
            &lt;/div&gt;
            &lt;div id="json"&gt;&lt;/div&gt;
            &lt;form id="register-form" action="register.php" method="POST"&gt;
                &lt;div class="form-grp"&gt;
                    &lt;!--&lt;label&gt;Username&lt;/label&gt;--&gt;
                    &lt;input type="text" id="username" name="username" placeholder="Username"&gt;
                &lt;/div&gt;
                &lt;div class="form-grp"&gt;
                    &lt;input type="email" name="register_email" id="register_email" placeholder="Email"&gt;
                &lt;/div&gt;
                &lt;div class="form-grp"&gt;
                    &lt;input type="password" id="register_password" name="register_password"  placeholder="Password"&gt;
                &lt;/div&gt;
                &lt;div class="form-grp"&gt;
                    &lt;input type="password" id="confirm_password" name="confirm_password"  placeholder="Retype Password"&gt;
                &lt;/div&gt;
                &lt;div class="btn-grp"&gt;
                    &lt;button type="submit" name="submit" class="button-purple" id="do_register"&gt;Sign Up&lt;/button&gt;
                    &lt;button class="button-white" style="margin-left: 30px;" data-remodal-target="LoginModal"&gt;Login to access&lt;/button&gt;
                &lt;/div&gt;
            &lt;/form&gt;
        &lt;/div&gt;
  </code></pre>
  
  <p>This is my PHP below</p>
  
  <pre><code>  if (strlen($password) &gt;= 8 &amp;&amp; strlen($password) &lt;= 60) {
  
  
                      if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
                          $account-&gt;addUser($username, $password, $email);
                          if ($account-&gt;userExist()) {
                              $message['email'] = "Email Address Is Already Registered!";
                          } else {
                              $account-&gt;create();
                              $message['type'] = "success";
                           }
                      } else {
                          $message = 'Invalid email!, Please enter a valid Email';
                      }
  
  header('Content-Type: application/json');
  $response = ['message' =&gt; $message];
  echo json_encode($response);
   // echo json_encode($message);
  //echo $message;
  </code></pre>
  
  <p>and this is my AJAX</p>
  
  <pre><code>$.ajax({
              type: 'POST',
              url: 'register.php',
              dataType: 'json',
              data: formData,
              success: function (data) {
                  $("#json").html(data["message"]);
                  //response = response.slice(0, -1);
                  //response = JSON.parse(response);
                  //var json = JSON.parse(response);
                  //var resdata = response;
                  //var json = $.parseJSON(response);
                  //if (resdata) {
  
                      //alert(resdata['success']);
                      //alert(json['success']);
                      // $("#register-form").addClass("remove-form");
                      // $("#register_container").addClass("register-container-active");
                      // $("#register_title").html("Register was Successful");
                      // $(".reg-notification").addClass("show-reg-notification");
                  //}else if (resdata['email']) {
                      //alert(resdata['email']);
                  //}
                      //alert(json['email']);
                      //$("#msg").html(json.email);
                  //}
                  console.log(response);
              },
              error:function(error){
                  console.log(error);
              }
          });
  </code></pre>
  
  <p>As you can see all the codes I commented are failed codes, I like to display the message coming from PHP in my #json ID.</p>
  
  <p>What I like to do is get the 'success' encoded from PHP to my HTML through AJAX, if user registration is successful, also get the 'email' error out if user exists. </p>
  
  <p>I have no idea what condition to use in AJAX to test this or how to go about it and I know it will be something simple. </p>
  
  <p>But I may be to clustered in the head to figure it ..as I keep looking at :(</p>