-1

I am working on a website, now i want to move all the scripts in each file to one file, so created core.js . I included in head of the index.php but problem is that some functions works and some not. have a look at part of my code:

JQuery (a part of 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();
});  

i have also tried including the function in $(document).ready(function(){...});. Plus i also tried to include core.js before </body>tag in html.

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

When I put the same script in html file between script tags, everything works properly. And one interesting thing is that when i put the script in external file the $.post('includes/mypwdchange.php'... function works but does not return the expected answer, also see

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.";
}
}
?>

The problem is it always return "Old Password is not valid." when i put the jQuery in core.js but everything works when i put the jQuery inside myaccount.php

Imran Aslam
  • 208
  • 2
  • 15

1 Answers1

0

The <?php echo $myuserid; ?> include lines do not get executed in a .js file.

You either need to pull them out and use variable references to it in the php file. Or you can make you js files php files and make sure to return the right content type.

epascarello
  • 204,599
  • 20
  • 195
  • 236