'JQuery bit not working - Need to select appropriate dropdown box

I'm trying to get the 2 dropdown select options etc boxes to preselect something (other than the 1st entry) or at least remember it after the script runs with JQuery and its not working. I'm sure its something simple but I can't see it. There is a connection to a database (not included). Ignore the cookies bit (not in use for this bit)

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<script>
    $(document).ready(function()
    {
        $("#sensor").val($("#sensor option").eq(2).val());
    });
</script>
</head>
<body>
<?php
// Ignore this cookie stuff
if(!isset($_COOKIE[$cookie_name])) {
     echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
     echo "Cookie '" . $cookie_name . "' is set!<br>";
     echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

<p><strong>Note:</strong> You might have to reload the page to see the value of the cookie.</p>
<form method="post">

<b>Sensor to view</b><select name = "sensor" >
<option value =0 >Select sensor</option>

<?php
$counter = 1;
//$sql = "SELECT * FROM `sensors_config`";
$sql = "SELECT * FROM `sensor_view_permissions` JOIN sensors_config ON sensors_config.sensor_id=sensor_view_permissions.sensor_id WHERE `username` ='bob'";
$result = mysqli_query($conn,$sql);
print_r($result);
while ($row = mysqli_fetch_array($result)) {
echo '<option value = '.$counter.' >'.$row['sensor_friendly_name'].'</option>';
$counter = $counter+1;
}
?>

</select>
<br>
<b>Date</b><select name = "datetoview" >
<option>Select date</option>
<?php
$sql = "SELECT * FROM `date_reference`";
//$sql = "SELECT * FROM `sensor_view_permissions` JOIN sensors_config ON sensors_config.sensor_id=sensor_view_permissions.sensor_id WHERE `username` ='bob'";
$result = mysqli_query($conn,$sql);
print_r($result);
while ($row = mysqli_fetch_array($result)) {
echo '<option>'.$row['information'].'</option>';
}
?>
</select>

<input type="submit" name="submit" value="Show me !" />
</form>
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['sensor'];  // Storing Selected Value In Variable
$date_to_view = $_POST['datetoview']; // Storing the date
echo "You have selected :" .$selected_val;  // Displaying Selected Value
echo "<br>The date to view is:".$date_to_view; // Displaying the date
}
?>
</select>
</body>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source