|
|
|
รบกวนสอบถามครับ เราสามารถใช้ ajax ส่งข้อมูลพร้อม file ไปพร้อมกันผ่าน form ได้มั้ยครับ |
|
|
|
|
|
|
|
ตอนนี้สามารถส่งค่าต่าง ๆ ไปได้แล้วครับ แต่ยังส่งข้อมูลจาก input type="file" ไม่ได้ตัวเดียว มันสามารถทำได้มั้ยครับ
Form
<div class="modal fade" id="addVM" tabindex="-1" aria-labelledby="addVMModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addVMModalLabel">ADD VM</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="post" class="row g-3 p-1" id="addVM-form" enctype="multipart/form-data">
<div class="col-md-6">
<label class="form-label">เลือกประเทศ : </label>
<select name="sel-c" class="form-select" id="country-list">
<option selected>Open this select COUNTRY</option>
<?php foreach($result_c as $row) { ?>
<option value="<?php echo $row["country_id"]; ?>">
<?php echo $row["country_name"]; ?>
</option>
<?php } ?>
</select>
</div>
<div class="col-md-6">
<label class="form-label">เลือกโซน : </label>
<select class="form-select" id="zone-list" name="sel-z" disabled>
<option selected>Open this select ZONE</option>
</select>
</div>
<div class="col-md-6">
<label class="form-label">เลือกพูล : </label>
<select class="form-select" id="pool-list" name="sel-p" disabled>
<option selected>Open this select POOL</option>
</select>
</div>
<div class="col-md-6">
<label class="form-label">ผู้ดูแล : </label>
<select class="form-select" id="admin-list" name="sel-a" disabled>
<option selected>Open this select ADMIN</option>
<?php foreach($result_a as $row) { ?>
<option value="<?php echo $row["admin_id"] ?>">
<?php echo $row["admin_name"]; ?>
</option>
<?php } ?>
</select>
</div>
<div class="col-md-6">
<label class="form-label">วันที่เบิก : </label>
<input type="date" class="form-control" name="date" value="">
</div>
<div class="col-12">
<div class="input-group pt-2">
<div class="col-12">
<span class="input-group-text">Spec Details</span>
<input type="text" class="form-control" name="game" placeholder="Game*">
<input type="text" class="form-control" name="service" placeholder="Service*">
<select class="form-select" name="vcpu">
<option selected>vCPU</option>
<?php
for($i=2;$i<=128;$i = $i*2){
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
<select class="form-select" name="vram">
<option selected>RAM</option>
<?php
for($i=2;$i<=128;$i = $i*2){
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
<input type="text" class="form-control" name="vhdd" placeholder="HDD*">
<input type="text" class="form-control" name="vos" placeholder="OS*">
</div>
</div>
<div class="input-group pt-3">
<div class="col-12">
<span class="input-group-text">IP Details</span>
<select class="form-select" name="exip" id="serch_ipdd">
<option selected>EX_IP</option>
<?php foreach($result_e as $row) { ?>
<option value="<?php echo $row["exip_id"]; ?>">
<?php echo $row["exip"]; ?>
</option>
<?php } ?>
</select>
<input type="text" class="form-control" name="inip" placeholder="IN_IP">
<input type="text" class="form-control" name="description" placeholder="Description">
<input type="hidden" id="hidden_status" name="hidden_status" value="Unavailable">
</div>
</div>
<div class="col-12 pt-4">
<input type="file" name="fileupload[]" multiple>
</div>
</div>
<div class="modal-footer">
<input type="submit" id="insertVM" class="btn btn-success" value="Add">
<input type="reset" id="clearVM" class="btn btn-danger" value="Clear">
<button type="button" class="btn btn-dark" data-bs-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
</div>
Code ajax
<script>
$(document).ready(function(){
$('#addVM-form').on('submit',function(e){
e.preventDefault();
$.ajax({
url:"insertVM.php",
method:"post",
data:$('#addVM-form').serialize(),
success:function(data){
$('#addVM-form')[0].reset();
$('#addVM').modal('hide');
// location.reload();
}
});
});
});
</script>
Code (PHP)
$country = mysqli_real_escape_string($con,$_POST['sel-c']);
$zone = mysqli_real_escape_string($con,$_POST['sel-z']);
$pool = mysqli_real_escape_string($con,$_POST['sel-p']);
$admin = mysqli_real_escape_string($con,$_POST['sel-a']);
$date = mysqli_real_escape_string($con,$_POST['date']);
$game = mysqli_real_escape_string($con,$_POST['game']);
$service = mysqli_real_escape_string($con,$_POST['service']);
$vcpu = mysqli_real_escape_string($con,$_POST['vcpu']);
$vram = mysqli_real_escape_string($con,$_POST['vram']);
$vhdd = mysqli_real_escape_string($con,$_POST['vhdd']);
$vos = mysqli_real_escape_string($con,$_POST['vos']);
$exip = mysqli_real_escape_string($con,$_POST['exip']);
$inip = mysqli_real_escape_string($con,$_POST['inip']);
$description = mysqli_real_escape_string($con,$_POST['description']);
$status = mysqli_real_escape_string($con,$_POST['hidden_status']);
$fileupload = mysqli_real_escape_string($con,$_FILES['fileupload']['name']);
echo $fileupload;
รบกวนด้วยครับ
Tag : PHP, HTML5, Ajax, jQuery, Bootstrap Framework
|
|
|
|
|
|
Date :
2021-09-09 03:58:23 |
By :
parinpph |
View :
549 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ทำได้แล้วครับ ขอบคุณมากครับ
|
|
|
|
|
Date :
2021-09-10 10:56:32 |
By :
parinpph |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|