Jump to content

dropdown dynamic using php and mongodb


subendi

Recommended Posts

file menuUser.php

!DOCTYPE html>

<html>

<head>

</head>

<body>

<?php

require 'vendor/autoload.php';

$conn = new MongoDB\Client();

$db = $conn->cucian1;

$collection = $db->cabang_kota;

$datacabangkota=$collection->find();

?>

<select name="cabangKota" onchange="getId(this.value);">>

<?php foreach($datacabangkota as $cabangkota){?>

<option value="<?php $id= $cabangkota["_id"]; $kota= $cabangkota["cabang"]; echo $id;

?>"><?php echo $kota;} ?></option>

</select>

<select name="cabangDesa" id="cabangDesa"></select>

<script   src="https://code.jquery.com/jquery-3.1.1.js"   integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="   crossorigin="anonymous">  

</script>

<script>

    function getId(val){

        $.ajax({

            type: "POST",

            url: "cabangDesa.php",

            data: "cid="+val,

            success: function(data){

                $("#cabangDesa").html(data);

            }

        });

    }

</script>

</body>

</html>

file cabangDesa.php

<?php

require 'vendor/autoload.php';

$conn = new MongoDB\Client();

$db = $conn->cucian1;

$collection = $db->cabang_desa;

$cid = $_POST["cid"];

$query = array("cabang_kota_id"=>$cid);

$datacabangDesa=$collection->find($query);

?>

<option value="">Select State</option>

<?php foreach($datacabangDesa as $cabangdesa){?>

<option value="<?php $id= $cabangdesa["_id"]; $desa= $cabangdesa["cabang"]; echo $id;

?>"><?php echo $desa;}?></option>



The first category of dropdown is working fine but I am wondering why my 2nd dropdown is not working. I just cant trace any error in the code and yet this is happening.

Edited by subendi
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.