-3

I have a weird question about Javascript and PHP.

These are my codes

<script>
<?php
        $servername = "localhost";
        $username = "root";
        $password = "rootroot";
        $dbname = "onlinerecruitment";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $s_id = NULL;
        $pos_id = $_POST['position'];
        $s_name = $_POST['skill_name'];
        $s_wg = $_POST['skill_weight'];
        $click = $_POST['clicks'];

        $sum = array_sum($s_wg);

?>
var sum = '<?php array_sum($s_wg); ?>';

        if( sum != 10){
                    alert("Weight-score is over or lower than 10.");

        }else{

<?php
        for($i=0; $i <= $click; $i++){

                $sql = ("INSERT INTO required_skills VALUES ('".$s_id."' , '".$pos_id."' , '".$s_name[$i]."' ,'".$s_wg[$i]."')");

                $resultt = "";

                if ($conn->query($sql) == TRUE) {
                   $resultt = "FINISH";
                } else {
                   $resultt = "ERROR";
                }

        }

        $conn->close();
?>


        var Message = ' <?php  echo $resultt; ?>';
        if((Message + "") == "ERROR"){
        alert("Something wrong, please contract web admin.");
        }else{
        alert("Weight-score setup successful.");
        location.href = "Weight_Score_Setup.php"
        } 

    }           

As you can see there is If and Else conditions, which I test this page to get the "If" condition. The result turns to be "If" but INSERT query also working which it should not to because they are all in "Else" condition. I have no idea how to fix this. Please help.

gznero
  • 193
  • 11

1 Answers1

1

IF ELSE transfered to PHP logic

<script>
<?php
        $servername = "localhost";
        $username = "root";
        $password = "rootroot";
        $dbname = "onlinerecruitment";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $s_id = NULL;
        $pos_id = $_POST['position'];
        $s_name = $_POST['skill_name'];
        $s_wg = $_POST['skill_weight'];
        $click = $_POST['clicks'];

        $sum = array_sum($s_wg);
        if( $sum != 10){
                    echo 'alert("Weight-score is over or lower than 10.");';

        }else{

        for($i=0; $i <= $click; $i++){

                $sql = ("INSERT INTO required_skills VALUES ('".$s_id."' , '".$pos_id."' , '".$s_name[$i]."' ,'".$s_wg[$i]."')");

                $resultt = "";

                if ($conn->query($sql) == TRUE) {
                   $resultt = "FINISH";
                } else {
                   $resultt = "ERROR";
                }

        }

        $conn->close(); 
?>

        var Message = ' <?php  echo $resultt; ?>';
        if((Message + "") == "ERROR"){
        alert("Something wrong, please contract web admin.");
        }else{
        alert("Weight-score setup successful.");
        location.href = "Weight_Score_Setup.php"
        } 

    }   
Marius
  • 399
  • 2
  • 14