weixin_33721344 2015-07-05 12:32 采纳率: 0%
浏览 96

Ajax回调中的if语句

Hi im having a problem refreshing my page when a condition is met in php using ajax, if the php script is successful i want it to reload the page but when it is not then i only want to echo the errors.

The following is the part of my PHP code that returns the 'success' or error message:

if ($login) {
    echo "success";
} else {
    echo '<p>Username and Password does not match</p>';
};

This is working as i have tested it separately and the user gets logged in even though ajax

The problem i think comes in the ajax call back with the if statement. Here is the my js script that is running the ajax call back.

$('form.ajax').on('submit', function(){

    var that = $(this),
        url = that.attr('action'),
        type = that.attr('method'),
        data = {};

    that.find('[name]').each(function(index, value){
        var that = $(this),
            name = that.attr('name'),
            value = that.val();

        data[name] = value;
    });

    $.ajax({
        url: url,
        type: type,
        data: data,
        success: function(response){
            if(response == "success") {
                                //location.reload();
                window.location = "index.php";
            } else {
                $('#loginError').html(response);
            };
        }
    });

    return false;
});

I have tried everything on the net but every time the PHP script is successful it just echo's out the word success rather then redirecting the page or reloading

  • 写回答

1条回答 默认 最新

  • weixin_33698823 2015-07-05 14:20
    关注

    Thanks chris85 for the perfect advice worked like gold and was able to fix the problem, that was that there was a extra line at the top of my PHP script that I missed and saw it when i used console.log(response), also read the post you linked and it will help with future problems like this for me so thanks again.

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改