i have 2 tables
office1 > id,off1
office2 > id,off2_id,off2
i want to insert the choosen into table "saveoffice" > offices1,offices2
drop.php
<!DOCTYPE html>
<html dir="rtl"
lang="ar">
<head>
<meta charset="utf-8">
<title>dependent dropdown list using jquery Ajax?</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://demo.itsolutionstuff.com/plugin/bootstrap-3.min.css">
</head>
<body>
<form action="savedata.php" name="myForm" method="POST"/>
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">Select office 1 and get bellow Related office 2</div>
<div class="panel-body">
<div class="form-group">
<label for="title">offices 1:</label>
<select name="office1" id="office2" class="form-control">
<option value="">--- choose office 1 ---</option>
<?php
require('config.php');
$sql = "SELECT * FROM office1";
$result = $con->query($sql);
while($row = $result->fetch_assoc()){
echo "<option value='".$row['id']."'>".$row['off1']."</option>";
}
?>
</select>
</div>
<div class="form-group">
<label for="title">offices 2</label>
<select name="office2" id="office2" class="form-control" style="width:350px">
</select>
</div>
</br>
<div class="col-md-4">
<input type="submit" name="submit" value="Insert Data" class="btn btn-danger">
</div>
</div>
</div>
</div>
<script>
$( "select[name='office1']" ).change(function () {
var stateID = $(this).val();
if(stateID) {
$.ajax({
url: "ajaxpro.php",
dataType: 'Json',
type:'POST',
data: {'id':stateID},
success: function(data) {
$('select[name="office2"]').empty();
$.each(data, function(key, value) {
$('select[name="office2"]').append('<option value="'+ key +'">'+ value +'</option>');
});
}
});
}else{
$('select[name="office2"]').empty();
}
});
</script>
</body>
</html>
ajaxpro.php
<?php
require('config.php');
$sql = "SELECT * FROM office2
WHERE off2_id LIKE '%".$_POST['id']."%'";
$result = $con->query($sql);
$json = [];
while($row = $result->fetch_assoc()){
$json[$row['id']] = $row['off2'];
}
echo json_encode($json);
?>
savedata.php > it does not work
<?php
include('config.php')
$off1 = $_POST['$offices1'];
$off2 = $_POST['$offices2'];
$query = " INSERT INTO `saveoffice`(`id`, `offices1`, `offices2`) VALUES (0,".$offices1.",".$offices2.") ";
$query_execute=mysqli_query($con,$query);
if($query_execute)
{
header("location:drop.php?q=success");
}
else
{
echo mysqli_error($con);
header("location:drop.php?e=error");
}
?>