Javascript do both If and Else condition (PHP) : <p>I have a weird question about Javascript and PHP.</p>

<p>These are my codes</p>

<pre><code>&lt;script&gt;
&lt;?php
        $servername = "localhost";
        $username = "root";
        $password = "rootroot";
        $dbname = "onlinerecruitment";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn-&gt;connect_error) {
            die("Connection failed: " . $conn-&gt;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);

?&gt;
var sum = '&lt;?php array_sum($s_wg); ?&gt;';

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

        }else{

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

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

                $resultt = "";

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

        }

        $conn-&gt;close();
?&gt;


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

    }           
</code></pre>

<p></p>

<p>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.</p>
