douzao1119 2013-07-11 06:48
浏览 56

jQuery .load()函数不能在我的php文件上运行

My particular situation with using the jQuery .load() function is to include a page into my site after clicking a link. I have chosen to write the code inline. What I have is a user control panel with several buttons that would link to pages that you edit your information. I have one for password and one for settings. I'm trying to get this one to work and I will write a for each loop to apply to any more links I click. My php file looks like this:

<?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
    }
?>

I have this php file included in a containing page. I also note that if I include the changepassword.php page using php include, it will load properly. I basically want the same thing as php's include, except with jQuery load.

My logged in included php file looks like this:

<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>

If I'm being an idiot, you can troll me. I don't care. If it's a security block in jQuery, let me know. I read the documentation for load a lot of times to try to find out why it doesn't work. If I just can't do it, I want this community to tell me so I can give up. I really just want a live include of these files. There probably is another solution for me. If so, suggest it. I can try to build it. Thank you.

  • 写回答

2条回答 默认 最新

  • doubiaode0460 2013-07-11 06:53
    关注
    <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>
    

    You'll need to wrap the script so that it can be executed as soon as the DOM is ready.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题