2016-02-17 56 views
-1

我正在一個網站上工作,現在我想將每個文件中的所有腳本移動到一個文件中,因此創建了core.js。我包含在index.php 的頭部,但問題是有些函數有效,有些則不適用。看看我的代碼部分:從外部文件中包含jQuery,某些函數有效,有些不是

的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中包含</body>標記之前的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時,一切正常。

回答

0

<?php echo $myuserid; ?>包含的行不會在.js文件中執行。

你或者需要把它們拉出來,並在php文件中使用變量引用。或者你可以讓你的js文件的PHP文件,並確保返回正確的內容類型。

+0

它工作時,我把jQuery放入myaccount.php。無論如何,我會用JavaScript變量替換它並告訴。 –

+1

因爲php echo語句在.php文件中起作用。 – epascarello

+0

http://stackoverflow.com/questions/9653651/including-php-variables-in-an-external-js-file – epascarello

相關問題