Rk Malbacias
Rk Malbacias

Reputation: 49

calling javascript function from php form submit

Good day, will anybody be willing to help me out here. I have this code:

<form id="form_advanced_validation" method="POST" action="">
<div class="row clearfix">
    <div class="col-md-10">
        <div class="form-group form-float">
            <div class="form-line">
                <input type="password" class="form-control" name="oldPass" required>
                <label class="form-label">Old Password</label>
            </div>
        </div>
    </div>
</div>
<div class="row clearfix">
    <div class="col-md-10">
        <div class="form-group form-float">
            <div class="form-line">
                <input type="password" class="form-control" name="newPass" required>
                <label class="form-label">New Password</label>
            </div>
        </div>
    </div>
</div>
<div class="row clearfix">
    <div class="col-md-10">
        <div class="form-group form-float">
            <div class="form-line">
                <input type="password" class="form-control" name="confPass" required>
                <label class="form-label">Confirm Password</label>
            </div>
        </div>
    </div>
</div>
<button class="btn btn-primary waves-effect" type="submit" name="sChanges">SUBMIT</button>

When submit button will be clicked, it will process the change password procedure, this is the code:

if (isset($_POST['sChanges'])) {
$dbC = new imsFunction();

$oldPasswrd = $dbC->txtInput($_POST['oldPass']);
$newPasswrd = $dbC->txtInput($_POST['newPass']);
$confPasswrd = $dbC->txtInput($_POST['confPass']);
$idNumber = $_SESSION['eID'];

if ($newPasswrd <> $confPasswrd) {
    $_SESSION['passConfirm'] = FALSE;
    echo '<script language="javascript">';
    echo 'window.location = "change_Passwd.php";';
    echo '</script>';
} else 

    $dbQry = $dbC->sRowQry('Select * From tbl_endUser Where id_number = ? and id_key = ?',[$idNumber, $oldPasswrd]);

    if ($dbQry->rowCount() > 0) {
        $changePass = $dbC->qryExec('Update tbl_endUser Set id_key=? Where id_number=?',[$newPasswrd, $idNumber]);
        $_SESSION['passChange'] = TRUE;
        echo '<script language="javascript">';
        echo 'window.location = "change_Passwd.php";';
        echo '</script>';
    } else {
        $_SESSION['wrongPass'] = FALSE;
        echo '<script language="javascript">';
        echo 'window.location = "change_Passwd.php";';
        echo '</script>';
    }

    $dbC->disConnect(); }

What I want to do is when "$_SESSION['passChange']" has been set, I would like to call this javascript function:

function showSuccessMessage() {
swal("Good job!", "Password has been changed!", "success");}

I tried:

if (isset($_SESSION['passConfirm'])) { echo '<script type="javascript"> showSuccessMessage(); </script>'; }

but nothing happened.. Please guys, help me.

Upvotes: 1

Views: 262

Answers (1)

Sagar V
Sagar V

Reputation: 12478

Don't forget to use session_start(); at beginning of each page.

Before calling the function, just use echo $_SESSION['passConfirm']; to ensure the session is set.

Now correct your code based on above 2 things and check

Upvotes: 1

Related Questions