Yaku Kakashi
asked on
File name listing and insert selected filename into database
Hello guys I have table listing files inside a folder with their names and extension. Now what i want to happen now is to get selected name and insert in into database...The problem is i use form inside table and post data using aJax.instead of inserting the first name 2nd name the last name will be inserted into the database not the selected file name...it only get the last name... here is my code...
<html>
<?php
include ('include/head.php');
?>
<body>
<div class="div1" id="div1" align="center"></div>
<button type="button" name="backup" id="backup" class="btn btn-primary backup";
<b><i class="fa fa-print" aria-hidden="true"></i> Backup Database</b></button></br></br>
<!---Table--->
<div class="container">
<h2>This is just a test..heheheh</h2>
<table class="table table-bordered">
<thead>
<tr>
<th>Database Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$files = scandir('include/backup');
sort($files); // this does the sorting
foreach($files as $file){
if($file != '.' && $file != '..' && !is_dir($file)){
echo '<tr> <td>
<form name="form1" id="form1" method="post">
<input type="hidden" name="dbname" id="dbname" class="dbname" value="'.$file.'"/>
'.$file.'
</td>
<td>
<input type="hidden" name="id" id="id" class="id" value="1"/>
<input type="submit" value="Restore Backup" />
</form>
</td>
</tr>
';
}
}
?>
</tbody>
</table>
</div>
</body>
</html>
<script>
$(document).ready(function() {
$('form').submit(function(){
var formData = $(form1).serialize();
event.preventDefault();
swal({
title: "Are you sure?",
text: "Create imaginary backup file?",
icon: "warning",
buttons: true,
dangerMode: true,
closeModal: false,
})
.then(function(yes)
{
if (yes)
{
$.ajax({
url: 'include/restore_data.php',
type: 'post',
data: formData,
success: function(data) {
$("#div1").load("include/myphp-restore.php");
}
});
}
else
{
return false;
}
});
});
});
</script>
<script>
$(document).ready(function()
{
<!---Backup Database Function with SweetAlert--->
$(document).on('click', '.backup', function()
{
var that = this;
swal({
title: "Are you sure?",
text: "Create imaginary backup file?",
icon: "warning",
buttons: true,
dangerMode: true,
closeModal: false,
})
.then(function(yes)
{
if (yes)
{
$.ajax
({
success: function (data)
{
swal("Imaginary file has been backuped!", {
icon: "success",
buttons: false,
timer: 3000,
});
$("#div1").load("include/myphp-backup.php");
$('#table').DataTable().ajax.reload();
}
});
}
else
{
return false;
}
});
});
});
</script>
How can I post the selected name using aJax,,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER