How to pass variable with ajax?

Good evening to all!
The data come from a form button but the data with ajax is not.
Get the following results:
action close
id 1

Code page



 <meta charset="UTF-8">
 <title>View applications</title>
 <link href="style.css" rel="stylesheet" type="text/css">
<?php require_once ('includes/db.php') ?>
 <script type="text/javascript" src="module/jquery-3.2.1.min.js"></script>

the <script>
$(document).ready(function(){
$("#rat").submit(function(){
 var ocenka = prompt ("Enter text: ")
$.ajax({
url:"includes/functions.php",
type:"POST",
 data:({rat: ocenka, action: "close"}),
 dataType: "html"
});
});
});
</script>




<?php require_once ('includes/header.php'); ?>

<?php 
$qr_result = mysqli_query($connection, "SELECT * FROM `applications` WHERE `id` =" . (int) $_GET['id']);

$data = mysqli_fetch_assoc($qr_result);

?>
 <form action="includes/functions.php" method="POST">

 <div class="buttons">
 <input type="hidden" name="action" value="delete">
 <input type="hidden" name="id" value="<?php" echo $_get['id'] ?> />
 <input type="submit" value="Delete application">
</div>
</form>
 <form action="includes/functions.php" method="POST">
the <div>
 <input type="hidden" name="action" value="close">
 <input type="hidden" name="id" value="<?php" echo $_get['id'] ?> />
 <input type="submit" id="rat" value="Close the application">
</div>
</form>
 <div class="head">
 <label class="bold"> application Status </label> <br>
 <label id="change"> <?php echo $data['status']; ?> </label><br><br>
</div>
 <div class="head">
 <label class="bold"> Subject application </label> <br>
 <label> <?php echo $data['theme']; ?> </label><br><br>
</div>

 <div class="head">
 <label> Description </label> <br>
 <label> <?php echo $data['text']; ?> </label><br><br>
</div>
 <div class="head">
 <label class="bold"> Assigned to </label> <br>
 <form action="includes/functions.php" method="POST">
<?php require_once ('includes/db.php');
$usersel = mysqli_query($connection, "SELECT * FROM `user`");
echo "<select id='selUser' name = 'selUser'-->";
while ($data =mysqli_fetch_assoc($usersel)){
 echo '<option>' . $data['fullname'] . '</option>';
 } 
echo "<input type="hidden" name="action" value="update">";
echo "<input type="hidden" name="id" value="" . $_GET[" id'] . "'>";
echo "<input type="submit" value="Accept">";
 ?> 
</form>
br><br>
</div>




Code page handler
<?php

$location = '/';
echo "Ocenka:" . $_POST['rat'] . "</br-->";
if (isset($_POST['action']))
{
require_once('db.php');

 $query = ";

switch($_POST['action'])
{
 case 'addApp':
 $theme = $_REQUEST['theme'];
 $text = $_REQUEST['text']; 
 $query = "INSERT INTO `applications` (`theme`, `text`) VALUE('$theme', '$text')";
break;

 case 'delete':
 $query = "DELETE FROM `applications` WHERE `id` =" . $_POST['id'];
break;

 case 'addUser';
 $fullname = $_REQUEST['fullname'];
 $query = "INSERT INTO `user` (`fullname`) VALUE('$fullname')";
break;

 case 'update';
 $query = "UPDATE `applications` SET `associate` = '" . $_POST['selUser'] . "' WHERE `id` = " . $_POST['id'];
break;

 case 'close';
 $query = "UPDATE `applications` SET `status` = 'Closed' `assessment` = ". $_POST['rat'] . "WHERE `id` =" . $_POST['id'];
break;
}

 if (!empty($query)) mysqli_query($connection, $query);
}
print_r($query);
//header ('Location:' . $location);
exit();
June 14th 19 at 18:58
1 answer
June 14th 19 at 19:00
$("#rat").submit(function(){ // WTF
#rat you have a button and not the form. And there is no preventDefault, so the intercept form submission is not happening.

And would be better if you didn't change the name of the field id for rat to make things work even with disabled js.

PS: rat

Find more questions by tags JavaScriptPHP