John Davidson

javascript - Button not working to get data from one database and add it into new databse

0 comments
Message:


I new to learn PHP,HTML,CSS and JAVASCRIPT, and battle with this code. I have tried it without and with form, but when click on button,(with form) it is running for 2 seconds and it does not update my new database. With out form nothing is happening.
I know there are problems with my code, learning and fixing them.


    <?php include_once 'header.php'?> 

<form id="basicForm" action="updateDatabase.php" method="post" enctype="multipart/form-data" name="basicForm">
<div class="wrapper">
<div class="content-wrapper">

<div class="container-fluid">
<div class="row">
<div class="col-25">
<div class="card">
<div class="card-header">
<h3 class="card-title">Update Database</h3>
</div>
<div class="card-body">
<p>
<button position="relative" class="btn btn-block btn-outline-danger btn-lg c1" id="homeDB">Home DB</button>&nbsp;
<span id="homeupdate"></span>
<span id="loaders1" style="display:none;">
<img alt="" src="dist/img/loader11.gif">
</span>
</p>
<p>
<button position="relative" class="btn btn-block btn-outline-success btn-lg c1" id="workdb">Work DB</button>&nbsp;
<span id="workupdate"></span>
<span id="loaders2" style="display:none;">
<img alt="" src="dist/img/loader11.gif">
</span>

</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>

<script>

$(document).ready(function(){

$('#homeDB').on('click', function() {


$.ajax({
type:'POST',
url:'db.php',
// dataType: 'json',
data:{db:"homeDB"},
beforeSend: function(){
$("#loaders1").show();
},
complete: function(){
$("#loaders1").hide();
},
success:function(data) {

$("#homeupdate").html("Databasis Opgedateer");

},
error: function (error) {
$("#homeupdate").html(error);
}
});

});


$('#workdb').on('click', function() {


$.ajax({
type:'POST',
url:'db.php',
// dataType: 'json',
data:{db:"workdb"},
beforeSend: function(){
$("#loaders2").show();
},
complete: function(){
$("#loaders2").hide();
},
success:function(data) {

$("#workupdate").html("Databasis Opgedateer");

},
error: function (error) {
$("#workupdate").html(error);
}
});

});
</script>


<script>
jQuery(document).ready(function(){

// Basic Form
jQuery("#basicForm").validate({
highlight: function(element) {
jQuery(element).closest('.form-group').removeClass('has-success').addClass('has-error');
},
success: function(element) {
jQuery(element).closest('.form-group').removeClass('has-error');
}
});

// Error Message In One Container
jQuery("#basicForm2").validate({
errorLabelContainer: jQuery("#basicForm2 div.errorForm")
});

// With Checkboxes and Radio Buttons

jQuery('#genderError').attr('for','gender');
jQuery('#intError').attr('for','int[]');

jQuery("#basicForm3").validate({
highlight: function(element) {
jQuery(element).closest('.form-group').removeClass('has-success').addClass('has-error');
},
success: function(element) {
jQuery(element).closest('.form-group').removeClass('has-error');
}
});

jQuery("#basicForm4").validate({
highlight: function(element) {
jQuery(element).closest('.form-group').removeClass('has-success').addClass('has-error');
},
success: function(element) {
jQuery(element).closest('.form-group').removeClass('has-error');
},
ignore: null
});

// Validation with select boxes
jQuery("#flowers, #fruits").select2({
minimumResultsForSearch: -1
});

});
</script>

</body>
</html>
<?php include_once 'footer.php'?>


My Answer:
It seems like you are trying to transfer data from one database to another using JavaScript. However, JavaScript alone cannot interact directly with databases. You will need to use a server-side language like PHP to handle the database operations.

Here is a basic example of how you can achieve this using PHP:

1. Create a PHP file (e.g., transfer.php) that will handle the database operations:

php
// Connect to the source database
$sourceConnection = mysqli_connect("source_host", "source_username", "source_password", "source_database");

// Connect to the destination database
$destinationConnection = mysqli_connect("destination_host", "destination_username", "destination_password", "destination_database");

// Retrieve data from the source database
$query = "SELECT * FROM source_table";
$result = mysqli_query($sourceConnection, $query);

// Insert data into the destination database
while ($row = mysqli_fetch_assoc($result)) {
$data1 = $row['column1'];
$data2 = $row['column2'];

$insertQuery = "INSERT INTO destination_table (column1, column2) VALUES ('$data1', '$data2')";
mysqli_query($destinationConnection, $insertQuery);
}

// Close the connections
mysqli_close($sourceConnection);
mysqli_close($destinationConnection);
?>


2. In your HTML file, create a form with a button that will trigger the transfer process:

html



Transfer Data









When you click the "Transfer Data" button, the form will submit a POST request to the transfer.php file, which will handle the database operations.

Make sure to replace the placeholder values (e.g., source_host, source_username, source_password, source_database, source_table, destination_host, destination_username, destination_password, destination_database, destination_table) with your actual database connection details and table names.

Also, ensure that your PHP server has the necessary permissions to access both databases.

Rate this post

4 of 5 based on 7975 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us