I have a form I created for editing style on my html page. I pull the data into this form with ajax. And in my select box list, I want whatever data in the database is selected. Others will not disappear. The list will remain the same, but the data in the database will be selected. Somehow I couldn't. How can I do it?
read.php
case "magaza":
if(isset($_REQUEST["id"])){
$result = $mk_db->oku("magaza", " id='$id' ", "", "", "");
if(!empty($result)) {
$responseArray["marka_adi"] = $result[0]["marka_adi"];
$responseArray["resim"] = $result[0]["resim"];
$responseArray["durum"] = $result[0]["durum"];
$responseArray["sira"] = $result[0]["sira"];
echo json_encode($responseArray);
}
}
break;
$(document).ready(function() {
/********************** MODAL AÇMA **********************/
$(document).on('click', '.bn-edit', function() {
var id = this.id;
console.log("id:" + id + ",type:magaza");
$.ajax({
type: "GET",
url: "read.php?id=" + id + "&type=magaza",
success: function(response) {
$("#edit-modal").modal('show');
$.each(response, function(key, value) {
console.log("key:" + key + ",value:" + value);
})
$("#duz_sira").val(response.sira);
$("#duz_durum").val(response.durum);
$("#id").val(id);
}
});
});
});
<div class="modal fade duzenle" id="edit-modal">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title pl-3" id="exampleModalScrollableTitle">Markayı Düzenle</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true" class="text-white">×</span>
</button>
</div>
<form method="post" id="frmedit" enctype="multipart/form-data">
<div class="modal-body pre-scrollable">
<input type="hidden" name="id" id="id" class="form-control">
<div class="form-group form-float mt-3">
<div class="form-line">
<input type="text" class="form-control" id="duz_sira" name="duz_sira" required>
<label class="form-label">Sıra</label>
</div>
</div>
<select class="form-control" name="duz_durum" id="duz_durum" required>
<option>Durum Seçiniz</option>
<option value="aktif">Aktif</option>
<option value="pasif">Pasif</option>
</select>
</div>
<div class="modal-footer">
<button type="button" class="btn bg-grey waves-effect iptal-butonu" data-dismiss="modal">İptal</button>
<input type="submit" id="update" class="btn bg-purple waves-effect kaydet-butonu" value="Kaydet">
</div>
</form>
</div>
</div>
</div>
Please study the post I made - it is a [mcve] without any needless AJAX if the PHP works, it works and is not needed in the question
You need an ID on the select:
$("#duz_durum")
uses the id. if you need name, you need $("[name=duz_durum]")
const response = { "marka_adi": "Marka 2", "resim": "", "durum": "aktif", "sira": "3" } // $("#edit-modal").modal('show'); $.each(response, function(key, value) { console.log("key:" + key + ",value:" + value); }) $("#duz_sira").val(response.sira); $("#duz_durum").val(response.durum); // $("#id").val(id);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="modal fade duzenle" id="edit-modal"> <div class="modal-dialog modal-lg" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title pl-3" id="exampleModalScrollableTitle">Markayı Düzenle</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true" class="text-white">×</span> </button> </div> <form method="post" id="frmedit" enctype="multipart/form-data"> <div class="modal-body pre-scrollable"> <input type="hidden" name="id" id="id" class="form-control"> <div class="form-group form-float mt-3"> <div class="form-line"> <input type="text" class="form-control" id="duz_sira" name="duz_sira" required> <label class="form-label">Sıra</label> </div> </div> <select class="form-control" name="duz_durum" id="duz_durum" required> <option>Durum Seçiniz</option> <option value="aktif">Aktif</option> <option value="pasif">Pasif</option> </select> </div> <div class="modal-footer"> <button type="button" class="btn bg-grey waves-effect iptal-butonu" data-dismiss="modal">İptal</button> <input type="submit" id="update" class="btn bg-purple waves-effect kaydet-butonu" value="Kaydet"> </div> </form> </div> </div> </div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.