The error in the ajax request always displays the contents (data == '1'), that is, it says that the data is not transmitted itself. The ajax request is triggered; the page does not reload; help 2 day I suffer

$(document).ready(function() { $("#clickButtonForm").bind("click", function () { $.ajax ({ url: "ajax.php", type: "POST", data: {login: $("#login").val(), password: $("#password").val(), email: $("#email").val()}, dataType: "html", beforeSend: function (){ $("#information").text ("Expectaton data...") }, success: function (data){ // в случае, когда пришло success. Отработало без ошибок if (data == '0') $("#error").text("Occured error speak administraor").removeClass("error").addClass("success").show().delay(8000).fadeOut(3000); // $("#error").text("You success registration").removeClass("error").addClass("success").show().delay(3000).fadeOut(3000); // в случа ошибок else (data == '1') $("#error").text("Occured error speak administraor").removeClass("success").addClass("error").show().delay(8000).fadeOut(3000); } }); }); }); 

here is the html template

 <form role="form" class="formForMe" id="register_form" method="post"> <div class="input-group"> <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span> <input type="text" class="input form-control" id="login" placeholder="Введите Ваш будующий логин для входа" required autofocus /> </div> <div class="input-group"> <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span> <input type="password" id="password" class="input form-control" placeholder="Введите Ваш пароль для входа" required /> </div> <div class="input-group"> <span class="input-group-addon"><span class="add-on">@</span></span> <input type="text" class="input form-control" id="email" placeholder="Ваш существующий email" required autofocus /> </div> <div class="row"> <div class="col-xs-6 col-sm-6 col-md-6 col-lg-12"> </div> <div class="col-xs-6 col-sm-6 col-md-6 col-lg-12"> <button type="button" class="clickButtonForm btn btn-labeled btn-success" id="clickButtonForm"> <span class="btn-label"><i class="glyphicon glyphicon-ok"></i></span>Войти</button> <button onclick="location.href='../index.html'" type="submit" class="btn btn-labeled btn-danger"> <span class="btn-label"><i class="glyphicon glyphicon-remove"></i></span>Назад</button> </div> </div> <p> <a href="#">Забыли свой пароль?</a></p> </form> 

and php handler ajax.php

 <?php if(isset(($_POST['login'], $_POST['password'], $_POST['email'])) { echo 0; } else { echo 1; } ?> 

Closed due to the fact that off-topic participants Pavel Mayorov , user207618, user194374, Denis , sercxjo Aug 21 '16 at 7:39 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Pavel Mayorov, Community Spirit, Community Spirit, Denis, sercxjo
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Error here: else (data == '1') . It seems you forgot to add a couple of letters and a space. - Pavel Mayorov
  • I did not understand what kind of letters - Koly
  • And in php bracket extra. Doesn't the code editor show? Must be if(isset($_POST['login'], $_POST['password'], $_POST['email'])) { - Ivan Pshenitsyn
  • @Koly after else always goes block { ... } . else - "then" - does not imply any conditions like yours. And you need to add if to get else if(...){ ... } . тогда если (условие){действия} - Ivan Pshenitsyn
  • if (data == '0') {$ ("# error"). text ("Occured error speak administraor"). removeClass ("error"). addClass ("success"). show (). delay (8000) .fadeOut (3000); // $ ("# error"). text ("You success registration"). removeClass ("error"). addClass ("success"). show (). delay (3000) .fadeOut (3000); // in case of errors} else (data == '1') {$ ("# error"). text ("Occured error speak administraor"). removeClass ("success") addClass ("error"). show ( ) .delay (8000) .fadeOut (3000);} - Koly

1 answer 1

Need to fix the condition in ajax.php. Logically, the server should return "0" if the fields are filled and "1" if not. But at the moment, the server will always return "0", because the condition checks not the fullness of the variables, but simply their presence. Those. empty login, password and email will also be accepted.

Therefore, the condition needs to be corrected for this:

 if(!empty($_POST['login']) and !empty($_POST['password']) and !empty($_POST['email'])) { 

Now it will be checked not only the presence of fields, but also their fullness.

In addition, the following code should be corrected in the presented code:

in ajax.php remove the extra bracket. It should be like this:

 if(isset($_POST['login'], $_POST['password'], $_POST['email'])) { 

In js, you need to fix the incorrect else . Correctly:

 else if (data == '1') 

You can read about the conditional statements in javascript in detail and with examples here: https://learn.javascript.ru/ifelse