Ответ / Успех / Неудачи (AJAX / JSON + PHP) - PullRequest
0 голосов
/ 05 марта 2019

У меня есть файл .JS.

Как бы вы дали data['status'] значения в соответствии с различными переменными в файле .PHP?(Мне нужно добавить некоторые условия).Мне нужно показать различные сообщения об ошибках, как вы можете видеть в следующем скрипте:

$.ajax({
      type: 'POST',
      dataType: 'json',
      url: '/dashboard/check/',
      data: {
        insta_user: insta_u,
        insta_country: insta_country_code,
      },
      success: function (response) {
        console.log(data);
        if (data['status']) {

          $.ajax({
            type: 'POST',
            dataType: 'json',
            url: '/dashboard/action-add-account.php',
            data: {
              insta_user: insta_u,
              insta_pass: insta_p,
              insta_country: insta_country_code,
            },
            success: function (data) {
              got_response = true;
              if (data['status']) {
                setTimeout(function () {
                  $.dialog({
                    title: 'Done',
                    content: "Now you can edit your activity",
                    icon: 'fab fa-instagram',
                    theme: 'modern',
                    closeIcon: true,
                    animation: 'scale',
                    type: 'green',
                  });
                }, 200);
                setTimeout(function () {
                  location.reload();
                }, 4500);
              }
              else if (data['step_two']) {

                setTimeout(function () {
                  $.dialog({
                    title: 'Great',
                    content: "Follow steps",
                    icon: 'fas fa-check',
                    theme: 'modern',
                    closeIcon: true,
                    animation: 'scale',
                    type: 'green',
                  });
                }, 200);
                setTimeout(function () {
                  location.reload();
                }, 4500);

              }
              else {
                if (data['error']) {

                  var error_title = 'Error!';
                  if (data['error_title']) {
                    error_title = data['error_title'];
                  }

                  setTimeout(function () {
                    $.dialog({
                      title: error_title,
                      content: data['error'],
                      icon: 'fas fa-exclamation-triangle',
                      theme: 'modern',
                      closeIcon: true,
                      animation: 'scale',
                      type: 'red'
                    });
                  }, 200);

                }

                else {
                  setTimeout(function () {
                    $.alert('Server error, try again later.');
                  }, 200);

                }
              }
              setTimeout(function () {
                location.reload();
              }, 6000);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
              got_response = true;
              $.alert('Server error, try again later.');
              setTimeout(function () {
                location.reload();
              }, 4000);
            }
          });

        }
        else {
          if (data['error'] == 'country') {
            got_response = true;
            setTimeout(function () {
              $.dialog({
                title: 'Choose your country',
                content: "We need to set your country first!",
                icon: 'fas fa-exclamation-triangle',
                theme: 'modern',
                closeIcon: true,
                animation: 'scale',
                type: 'red'
              });
            }, 200);
            setTimeout(function () {
              location.reload();
            }, 4000);
          }
          else if (data['error'] == 'exists') {
            got_response = true;
            setTimeout(function () {
              $.dialog({
                title: 'Existent user',
                content: "We already have an account with that name/email",
                icon: 'fas fa-exclamation-triangle',
                theme: 'modern',
                closeIcon: true,
                animation: 'scale',
                type: 'red'
              });
            }, 200);
            setTimeout(function () {
              location.reload();
            }, 4000);
          }
          else {
            got_response = true;
            setTimeout(function () {
              $.dialog({
                title: 'Unknown user',
                content: "There's no accounts with that name.",
                icon: 'fas fa-exclamation-triangle',
                theme: 'modern',
                closeIcon: true,
                animation: 'scale',
                type: 'red'
              });
            }, 200);
            setTimeout(function () {
              location.reload();
            }, 4000);
          }
        }
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        got_response = true;
        $.alert('Server error, try again later.');
        setTimeout(function () {
          location.reload();
        }, 4000);
      }
    });
  });
});

Например:

Как мне получить else if (data['error'] == 'exists') {, если мой запрос внутри .PHP файла$sql_un = mysql_query("SELECT * FROM users WHERE Email='".$_POST['Email']."'"); $num_un = mysql_fetch_array($sql_un); if($num_un == 0){?

Спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...