dongqi6486 2016-03-17 02:11
浏览 51
已采纳

Ajax,而不是发布或获取?

I have been using Ajax mainly to be used as a POST, to take care of thinks, but now wanted just to open a simple login page into a DIV (and is doing it) but now the problem comes when I have added this to the PHP code:

<?php
if (isset($_POST))
{

}
else
{
    echo "
    <html>
        <head>
            <meta charset='utf-8'>
            <link rel='stylesheet' type='text/css' href='assets/css/main.css'>
        </head>
        <body>
            <div class='page-header'>
                <h1>Preencha os campos e carregue em enviar</h1>
            </div>
            <form>
                <div class='form-group'>
                    <label for='exampleInputEmail1'>Endereço Email</label>
                    <input type='email' class='form-control' id='exampleInputEmail1' placeholder='Email'>
                </div>
                <div class='form-group'>
                    <label for='exampleInputPassword1'>Palavra-Pass</label>
                    <input type='password' class='form-control' id='exampleInputPassword1' placeholder='Palavra-Pass'>
                </div>
                <button type='submit' class='btn btn-default'>Enviar</button>
            </form>
        </body>
    </html>";
}

and this is the Ajax code:

$(function() {
    $('.navmenu a').bind('click', function(event) {
        event.preventDefault();
        var urlToGo = $(this).attr('href');
        if ($( window ).width() <= "992" && !$(this).hasClass('dropdown-toggle'))
        {
            $('.navmenu').offcanvas('hide');
        }
        $.ajax({
            url: urlToGo,
            dataType: 'html',
            success: function(result)
            {
                $(".container").html(result);
            }
        });
        $('html,body').animate({
            scrollTop: 0
        }, 700);
        console.log(urlToGo);
    });
});

When I press the link to be used to use the Ajax, it will put a empty page on the div container, what I'm doing wrong in here? If I Change the (isset($_POST)) to (!isset($_POST)) it will show the content right, and I don't wanted this to be a post.

What is wrong in the logic here ?!

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dqrnsg6439 2016-03-17 02:21
    关注

    Because $_POST is a global variable of PHP, it exists in every php thread, so isset($_POST) will return true, in your example the value of $_POST is an empty array. You can have a try like blow:

    if (!empty($_POST)) {
    
    } else {
        echo 'your html'
    }
    

    By the way , I think $_SERVER['REQUEST_METHOD'] is a more precise way to judge what kind of http request method the request is used.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择