【问题标题】:jQuery .load() function not working on my php filejQuery .load() 函数不适用于我的 php 文件
【发布时间】:2013-07-09 08:03:57
【问题描述】:

我使用 jQuery .load() 函数的特殊情况是在单击链接后将页面包含到我的站点中。我选择内联编写代码。我所拥有的是一个带有几个按钮的用户控制面板,这些按钮可以链接到您编辑信息的页面。我有一个用于密码,一个用于设置。我试图让这个工作,我将为每个循环编写一个以应用于我单击的任何更多链接。我的 php 文件如下所示:

<?php
    include_once("template/main_site/core/init.php");
    protect_page();

    if (empty($_POST) === false) {
        $required_fields = array('current_password', 'password', 'password_again');
        foreach ($_POST as $key=>$value) {
            if (empty($value) && in_array($key, $required_fields) === true) {
                $errors[] = 'Fields lighted red are required.';
                break 1;
            }
        }

        if (encryptBetter($_POST['current_password']) === $user_data['password']) {
            if (trim($_POST['password']) !== trim($_POST['password_again'])) {
                $errors[] = 'Your new passwords do not match';
            }
            if (strlen($_POST['password']) < 6 || strlen($_POST['password']) >= 32) {
                $errors[] = 'Your new password must be less than 32 characters and more than 6 characters.';
            }
        } else {
            $errors[] = 'Your current password is entered incorrectly.';
        }

    }
?>
    <h1>Change Password</h1>
    <?php
    if (isset($_GET['success']) === true && empty($_GET['success']) === true) {
        echo 'Your password has been changed!';
    } else {
        if (isset($_GET['force']) === true && empty($_GET['force']) === true) {
    ?>
        <p>You must change your password now that you've requested.</p>
    <?php
        }
        if (empty($_POST) === false && empty($errors) === true) {
            change_password($session_user_id, trim($_POST['password']));
            header('Location: changepassword.php?success');
            exit();
        } else if (empty($errors) === false) {
            echo output_errors($errors);
        }
    ?>
    <form action = "" method = "post">
        <ul>
            <li>
                <label>Current Password</label>
                <input required type = "password" name = "current_password" pattern=".{6,32}"/>
            </li>
            <li>
                <label>New Password</label>
                <input required type = "password" name = "password" pattern=".{6,32}"/>
            </li>
            <li>
                <label>Repeat New Password</label>
                <input required type = "password" name = "password_again" pattern=".{6,32}"/>
            </li>
            <input type = "submit" value = "change password"/>
        </ul>
    </form>

<?php
    }
?>

我在包含页面中包含了这个 php 文件。我还注意到,如果我使用 php include 包含 changepassword.php 页面,它将正确加载。我基本上想要和 php 的 include 一样的东西,除了 jQuery 加载。

我登录的包含的 php 文件如下所示:

<div id="controls">
    <ul>
        <a id = "changepassword" href = "#login"><li>Change Password</li></a>
        <a href = "#"><li>Edit Profile</li></a>
        <?php if (is_admin()) { ?><a href = "#admin"><li>Administrate</li></a><?php }?>
        <a href = "logout.php"><li>Log out</li></a>
        <div id = "panel"></div>
    </ul>
</div>
<div id = "panel"><?php # include_once('template/main_site/includes/widgets/changepassword.php'); ?></div>

<script>
    $('#changepassword').click(function() {
        var phpFile = 'template/main_site/includes/widgets/' + $(this).attr('id') +'.php';
        $('#panel').html('loading...').load(phpFile);
    });
</script>

如果我是个白痴,你可以骗我。我不在乎。如果它是 jQuery 中的安全块,请告诉我。我阅读了很多次 load 的文档,试图找出它为什么不起作用。如果我做不到,我希望这个社区告诉我,这样我就可以放弃了。我真的只是想要这些文件的实时包含。对我来说可能还有另一种解决方案。如果是这样,建议它。我可以尝试构建它。谢谢。

【问题讨论】:

  • 这是template/main_site/includes/widgets/ 真实路径吗?你能在浏览器中打开你尝试使用 jquery 加载它的 URL 吗?
  • `
  • 管理
  • `

标签: php javascript jquery web-applications web


【解决方案1】:
<script>
$(function() {
    $('#changepassword').click(function(e) {
        e.preventDefault();
        var phpFile = 'template/main_site/includes/widgets/' + $(this).attr('id') +'.php';
        $.get(phpFile,{},function(response) {
            $('#panel').html(response);
        });
    });
});
</script>

您需要包装脚本,以便在 DOM 准备好后立即执行它。

【讨论】:

  • 尝试在事件中添加.preventDefault()方法。
  • 你知道这个脚本在它包含的.php文件被加载之前不会加载到DOM中,对吧?
  • 好的..还有一个变化 - 你需要监控控制台看看发生了什么......
  • 听起来您确实没有从 changepassword.php 脚本中得到预期的响应。
【解决方案2】:

您没有使用 jQuery .ready() 函数。一旦 DOM 完全加载,此函数就会执行您的脚本。所以把你的整个脚本包在里面。

<script>
$(document).ready(function() {
    $('#changepassword').click(function() {
        var phpFile = 'template/main_site/includes/widgets/' + $(this).attr('id') +'.php';
        $('#panel').html('loading...').load(phpFile);
    });
});
</script>

【讨论】:

  • @bjames105: 当你使用这个脚本时,点击事件的结果是什么?
  • 你检查浏览器控制台了吗?你有什么错误吗?
  • 使用 jQuery 选项卡不是一个坏主意... :)
猜你喜欢
相关资源
最近更新 更多
热门标签