获取Alert的值并在php中将其设置为变量

I want to make my alert value as a variable in php. This is my code

<html>    
<body>


    <form action="" method="post">

    <select name="kat" id="kat" >
        <option></option>
        <option>UNANG TAE</option>
        <option>PANGALAWANG TAE</option>

    </select>

        <input type="text" value="" name="baliw">

    </form>


<script src="js/jquery.js"></script>

<script>

$('#kat').on('change', function () {

<?php

/*
mysql_connect('localhost','root','');
mysql_select_db('perens');
mysql_query(" INSERT INTO table (sample) VALUES ('".$alert_value."') ");
*/
    ?>

alert(this.value);


});


</script>

    </body>
</html>

There's a comment inside my PHP where I want to make the alert value as a variable but I don't know how.

You can't do that directly.

The javascript event listener that raises the alert message is executed by the web browser (client side) and the php is executed in the server side before than page is loaded by the web browser (server side).

What you can do is to send the alert message to a PHP script using an AJAX request.

Example:

$('#kat').on('change', function() {

  $.ajax({
    url: "myscript.php",
    type: "post",
    data: {
      message: this.value
    }
  });

  alert(this.value);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Type whatever you want here:
<input type="text" id="kat" />

Now you can capture the messages from a php script like the following one:

<?php

// myscript.php

$alert_message = $_POST['message'];

// !!! Importat, sanitize the input in order to avoid a classic
// SQLInjection
// $alert_message =  mySanitizationFunction($_POST['message']);

mysql_connect('localhost','root','');
mysql_select_db('perens');
mysql_query(" INSERT INTO table (sample) VALUES ('".$alert_value."') ");

?>

Remember that it is important to sanitize the input in order to avoid a SQL Injection, I recommend you to use a database abstration layer like PDO or an ORM that can facilitate the input sanitization.

</div>

http://api.jquery.com/jquery.ajax/

Take a look at this to use ajax to send javascript variables to php.

JavaScript runs on the client side, after the PHP code is executed on the server side. So you cannot work with the JavaScript variables in PHP. However, you can make an Ajax call to a PHP file, posting the JavaScript values. These values can then be stored in a database by the PHP script.

Using jQuery, you can easy send Ajax requests:

http://api.jquery.com/jquery.ajax/

You can then access these values using PHP $_POST and $_GET.