【问题标题】:Including jQuery from external file, some functions works and some not包括来自外部文件的 jQuery,有些函数可以工作,有些不能
【发布时间】:2016-02-17 14:22:51
【问题描述】:

我正在开发一个网站,现在我想将每个文件中的所有脚本移动到一个文件中,因此创建了 core.js 。我包含在 index.php 的头部 但问题是有些功能有效,有些无效。看看我的部分代码:

JQuery(core.js 的一部分):

//jQuery of myaccount.php
$('button#mypwdchange').on('click', function(e){
    var oldpword = $('input#oldpword').val();
    var newpword = $('input#newpword').val();
    var cnewpword = $('input#cnewpword').val();

        if (newpword == cnewpword){             
            $.post('includes/mypwdchange.php',{mypwdchange: 'dochange', myuserid : '<?php echo $myuserid; ?>', oldpword: oldpword, newpword: cnewpword},function(data){
                $('div#mypwdchangealert').text(data).hide().fadeIn(300);
            });
        }else{
            $('div#mypwdchangealert').text('Your new passwords doesnot match').hide().fadeIn(300);
        }

 e.preventDefault();
});  

我也尝试在$(document).ready(function(){...}); 中包含该函数。另外,我还尝试在 html 中的 &lt;/body&gt;tag 之前包含 core.js。

myaccount.php 的内容

<h3>My Account</h3>

<div class="col-md-4">
<h4>Change Password</h4> <hr />
<form role="form">
<div class="form-group">
    <label for="oldpword">Old Password</label>
    <input type="password" class="form-control" id="oldpword" required/>
</div>
<div class="form-group">
    <label for="newpword">New Password</label>
    <input type="password" class="form-control" id="newpword" required/>
</div>
<div class="form-group">
    <label for="cnewpword">Confirm Password</label>
    <input type="password" class="form-control" id="cnewpword" required/>
</div>
<button type="submit" class="btn btn-default" id="mypwdchange">Change</button>
</form>
<div class="alert alert-info" id="mypwdchangealert">

</div>
</div>

当我将相同的脚本放在脚本标签之间的 html 文件中时,一切正常。一件有趣的事情是,当我将脚本放在外部文件中时,$.post('includes/mypwdchange.php'... 函数可以工作但不会返回预期的答案,另请参阅

mypwdchange.php

<?php 
include "../dbconfig.php";
if (isset($_POST['mypwdchange'])){
$changeid = $_POST['myuserid'];
$oldpword = $_POST['oldpword'];
$newpword = $_POST['newpword'];
$query0 = $con->prepare("select * from users where id = :id");
$query0->execute(array('id'=>$changeid));
$result0 = $query0->fetch(PDO::FETCH_ASSOC);
$oldpwordhash = $result0['password'];
if (password_verify($oldpword,$oldpwordhash)){
    $newpwordhash = password_hash($newpword, PASSWORD_DEFAULT);
    $query1 = $con->prepare("update users set password=:newpwordhash where id = :changeid");
    $query1->execute(array('newpwordhash'=>$newpwordhash, 'changeid'=>$changeid));
    if ($query1){
        echo "Password Changed Successfully!";
    }
}else{
    echo "Old Password is not valid.";
}
}
?>

问题是它总是返回“旧密码无效”。当我将 jQuery 放入 core.js 时,但当我将 jQuery 放入 myaccount.php 时一切正常

【问题讨论】:

    标签: javascript php jquery html


    【解决方案1】:

    &lt;?php echo $myuserid; ?&gt; 包含行不会在 .js 文件中执行。

    您要么需要将它们拉出并在 php 文件中使用对它的变量引用。或者您可以将 js 文件设为 php 文件并确保返回正确的内容类型。

    【讨论】:

    • 当我将 jQuery 放在 myaccount.php 中时它可以工作。无论如何,我会用 javascript 变量替换它并告诉它。
    • 因为 php echo 语句在 .php 文件中起作用。
    • 谢谢,这就是我在 html &lt;input type="hidden" name="mypwdchangeid" id="mypwdchangeid" value="&lt;?php echo $myuserid; ?&gt;"&gt; 和 jQuery: var userid = $('input#mypwdchangeid').val(); 中所做的,并在 $.post 中传递了这个变量。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2019-09-05
    • 2019-08-09
    • 2023-03-07
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多