首页 > 解决方案 > 在将表单提交到数据库之前,我的代码没有执行我的 JS 函数

问题描述

processPhase8()在将数据提交到数据库之前,我在执行 javascript 代码时遇到问题。数据被发送到数据库,但 JS 没有被执行,所以“决定”不会显示在数据库上。任何帮助将不胜感激。编程新手。

单击提交按钮后,我希望我的 JS 函数进行一些评分并将结果发送到<h1 class="alert-heading" id="decide" name="decide"></h1>以便if (isset($_POST["submit"))可以发送$decide = mysqli_real_escape_string($connection, $_POST["decide"])到数据库:

// this is the javascript code I want to execute once the submit button is clicked before sending the form data to the database

function processPhase8()
{
    if(creditscore >= possibleScore) {
        _("decide").innerHTML="response1";
        return true;
    }
    else if( creditscore === possibleScore) {
        _("decide").innerHTML= "response2 "
        ;
        return true;
    }
    else {
        _("decide").innerHTML= "response3";
        return true;
    }
}
}

<?php
if(isset($_POST["submit"])) {
    $decide = mysqli_real_escape_string($connection, $_POST["decide"]);
    $query  = "INSERT INTO users(decide) VALUES('$decide')";
    mysqli_query($connection,  $query ); // connect DB and process query
    //create a variable to see if it went through
    $result =  mysqli_query($connection,  $query ); // assign variable

    if ($result) {
        echo "<p style=font-size:30px;>Hurray, It Works!</p>"; } // checking that the query worked
    } else {
        echo " it didn't work";
    }
?>

<head> 
<script src="signupmulti.js"> </script>
</head>
<html>
<form method="post" action="signup.php" id="multiphase" onsubmit="return processPhase8()" enctype="multipart/form-data">
    <div class="alert alert-success" role="alert">
        <h1 class="alert-heading" id="decide" name="decide"></h1>
    </div>
    <button name="submit" type="submit" class="btn btn-lg btn-default">Click here for a decision </button>
</form>
</html>

标签: javascriptphponsubmit

解决方案


推荐阅读