php - AJAX/Javascript 无法更新网页

标签 php javascript html ajax

我写了一些ajax代码,但它没有更新网页。它应该从文件夹中获取表单并替换网页上显示的表单。然而,当我按下链接到 javascript 的按钮时,什么也没有发生。

Javascript:

function LoadManagerForm()
{
    document.getElementById('insert_response').innerHTML = "Loading..."
    nocache = Math.random()
    http.open('get', '../forms/managerform.php?nocache='+nocache);
    http.onreadystatechange = insertReply;
    http.send(null);
}
function insertReply() 
{
    if(http.readyState == 4){ 
    var response = http.responseText;
    document.getElementById('insert_response').innerHTML = response;
}

网页:

    <section class="grid col-three-quarters mq2-col-two-thirds mq3-col-full">
        <h2>Login</h2>
        <div id="insert_response">
        <p class="warning">Are you a manager? <a onclick="javascript:LoadManagerForm()" class="button" href="javascript:void(0);">Click here to login!</a></p>

        <form id="emplogin" class="contact_form" action="#" method="post" name="emplogin">  
            <ul>
                <li>
                    <label for="store">Store ID:</label>
                    <input type="text" name="store" id="store" required class="required" >
                </li>
                <li>
                    <label for="email">Email:</label>
                    <input type="email" name="email" id="email" required placeholder="JohnDoe@GMail.com" class="required email">
                </li>   
                <li>
                    <label for="password">Password:</label>
                    <input type="password" name="password" id="password" required class="required">
                </li>
                <li>
                    <button type="submit" id="submit" name="submit" class="button fright">Login</button>
                </li>   
            </ul>           
        </form>
        </div>
    </section>

最佳答案

我推荐你使用Jquery,它很简单:

将 html 包含在 head 标记内:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

然后通过以下方式更改您的 javascript 函数:

function LoadManagerForm()
{
    document.getElementById('insert_response').innerHTML = "Loading..."
    nocache = Math.random();
    $.post(
        '../forms/managerform.php?nocache='+nocache,
        {
            nocache : nocache
        },
        function(response)
        {
            document.getElementById('insert_response').innerHTML = response;
        }
    );
}

关于php - AJAX/Javascript 无法更新网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18690722/

相关文章:

php - 如何阻止特定文件夹向访客查看其内容?

php - 有没有办法在调用构造函数之前设置属性?

javascript - 使用重置默认值的 jQuery 上传图像预览

php - 评估给定文本 block 的关键字密度

php - 单击链接时使用 Jquery 更新 PHP session 变量

Javascript 不检查组合网格更改的文本输入值

php - 如何在网站主体上按下 CTRL+V 时加载 php 文件

javascript - CSS 和 HTML 用户界面问题

javascript - IE 11.0.10 无法显示使用 Javascript 更新的 HTML,除非打开控制台

javascript - 使用 javascript <button>NEXT</button> 将 html 元素完全更改为另一个 &lt;input type ="submit">