1

I'm trying to get data from the database using ajax to insert it in other element but the post data not passing to get-data.php so what the reason can be and the solution enter image description here passed data shown here but no resposns

addBuilding.php

<?php
require_once("./dbConfig.php");
$selectIL = "SELECT * FROM  iller ";
$selectIL = $db->prepare($selectIL);
$selectIL->execute();
$res = $selectIL->get_result();
?>

<form action="" method="post">
    <select name="pp" id="cites">
        <option value="">-select state-</option>
        <?php
        while ($row = $res->fetch_assoc()) {
        ?>
            <option value="<?= $row['id'] ?>"><?= $row['il_adi'] ?></option>
        <?php
        }
        ?>
    </select>
    <select name="district" id="district">
         <option value="">-select district-</option>
    </select>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
   <script src="getdata.js"></script>

getdata.js

$(document).ready(function() { 
    $("#cites").change(function() {
        if ( $("#cites").val()!="") {
            $("#district").prop("disabled",false);
        }else  {
            $("#district").prop("disabled",true);
        }
        var city = $("#cites").val(); 
        $.ajax({ 
            type: "POST",    
            url:"get-data.php",
            data:$(city).serialize(),
            success: function(result) {
                $("#district").append(result); 
            }
        });

    });
});

get-data.php I can see the form data in network inspection put no data passing to get-data.php

<?php
require_once("./dbConfig.php"); 

if (isset($_POST['pp'])) {
   
    $cites = $_POST['cites'];
    $selectIlce = "SELECT * FROM  ilceler where il_id=? ";
    $selectIlce = $db->prepare($selectIlce);
    $selectIlce->bind_param("i", $cites);
    $selectIlce->execute();
    $res = $selectIlce->get_result();
?>
    <?php
    while ($row = $res->fetch_assoc()) {
    ?>
        <option value="<?= $row['id'] ?>"><?= $row['ilce_adi'] ?></option>
<?php
    }
}
?>
1
  • var city = $("#cites").val(); - that gets the currently selected option value. data:$(city).serialize() - what element on your page do you think you are selecting here now, with $(city) ...? Commented Jan 24, 2022 at 11:36

2 Answers 2

1

You need to echo the results in get-data.php

<?php
    while ($row = $res->fetch_assoc()) {
    ?>
        echo "<option value='". $row["id"]."'>".$row['ilce_adi']."</option>";
<?php
    }
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

not exactly because the post doesn't go to ger-data.php I tried print post array using print_r function its returns a null array
try to give proper address in ajax, your url can't be just get-data.php....? it should be something like this localhost/project-name/get-data.php
0

1- Get data by serialize from form:

$("form").serialize()

2- Add dataType: "json" to ajax option:

$.ajax({ 
    type: "POST",    
    url:"get-data.php",
    data:$(city).serialize(),
    dataType: "json",
    success: function(result) {
        $("#district").append(result); 
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.