从php表单提交调用javascript函数

时间:2017-03-15 05:41:56

标签: javascript php jquery

美好的一天,有人愿意帮助我。我有这段代码:

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

当点击提交按钮时,它将处理更改密码程序,这是代码:

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(); }

我想要做的是在设置“$ _SESSION ['passChange']”时,我想调用这个javascript函数:

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

我试过了:

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

但没有任何事情发生..请伙计们,帮助我。

1 个答案:

答案 0 :(得分:1)

请勿忘记在每页开头使用session_start();

在致电function之前,只需使用echo $_SESSION['passConfirm'];确保会话已设置。

现在根据以上2项内容纠正您的代码并检查