I'm currently working on an IP-Address-Management. I created an auto-complete function into a textbox that shows ip-addresses that are in my SQL Database.
But I want to only show ip-addresses that are NOT in the Database. I want an ip address to be suggested to the user as soon as he starts writing the first octets, the suggested ip addresses should not be in the table and thus be freely selectable. For example, the user writes 168.0.0. and then all ip addresses that are not already in dspia_main should be available for selection via autocomplete.
I think it has to be a loop that goes from 1 to 255 (IP-Address rules) and checks if the ipv4 is in the database or not, if yes, than put it in the output array.
Here's my current code:
newip.php:
<!DOCTYPE html>
<html lang="en">
<head>
.......
<script>
$(function() {
$("#ip_v4").autocomplete({
source: "fetchData.php",
});
});
</script>
</head>
<body>
<form action="newip.php" autocomplete="off" method="post">
<label><b>IPv4-Adresse:*</b></label>
<input type="text" pattern="^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$" placeholder="z.B. 212.227.142.131" name="ip_v4" id="ip_v4" value="<?php echo $_POST['ip_v4'];?>" maxlength="15" required><br>
.......
EDIT:
- I still need to check if $term . $val is in Database
- I added a range from 0 to 255, but it also shows 192.1001, how can the range only apply per octet? fetchData.php:
<?php
if(!empty($_GET['term']))
{
include('db_conn.php');
$term = $_GET['term'];
$term = mysqli_real_escape_string($conn, $term);
$output = array();
$result = $conn->query("SELECT ip_v4 FROM dspia_main WHERE ip_v4 LIKE '" . $term . "%' AND state_id = '1' LIMIT 10");
$row=mysqli_fetch_array($result);
foreach(range(0,255) as $val)
{
$output[] = $term . $val;
}
mysqli_close($conn);
// output our results as JSON as jQuery expects
echo json_encode($output);
}
?>