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");
                    }

?>

Use prepared statement instead of direct insert to prevent from SQL injection!
You try to save into table saveoffice?
To save into tables office1 and office2 try something like this:

<?php

$link = mysqli_connect("localhost", "username", "password", "database");

$result = [];
for($i=1; $i<=2; $i++){
    $office = filter_input(INPUT_POST, 'offices'.$i);
    $result[$i] = 0;
    if($office){
        $stmt = mysqli_prepare($link,"INSERT INTO `office$i`(`off$i`) VALUES (?)");
        mysqli_stmt_bind_param($stmt, "s", $office);
        mysqli_stmt_execute($stmt);
        $result[$i] = (string)(mysqli_stmt_affected_rows($stmt));
    }
    echo 'off'.$i.' inserted '.$result[$i].' rows<br/>';
}

?>

... or you want to crete new table "saveoffices" and migrate all data from "office1" and "office2"? Show your table structures and relations.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.