After one whole day of trying various things I learned from other threads, I still can't get my code to work. I'm basically just trying to send data from a JavaScript function
to a localhost database
(server created using AMPPS).
Currently, I have a JavaScript file with the following function:
function WritetoDB() {
alert('Reached. No problem getting here.');
$.get("savedata.php");
}
I've also tried $.post
, $.ajax
, (with their appropriate parameters and such) but none of them seem to work either. Originally, the function was supposed to receive and pass on some variables that need to be stored on the database, but I'm trying to get it to work first before passing on any variables.
This is the 'savedata.php':
<?php
$servername = "localhost";
$username = "root";
$password = "mysql";
$databaseName = "database";
$tableName = "table";
$conn = new mysqli($servername, $username, $password);
$A = mysqli_real_escape_string($conn, 'this is A');
$B = mysqli_real_escape_string($conn, 'this is B');
$C = mysqli_real_escape_string($conn, 'this is C');
$sql = "INSERT INTO `database`.`table` (columnA,columnB,columnC)
VALUES ('$A', '$B', '$C')";
$conn->close();
I tried executing the PHP
code in the index.php
file and it works (data was sent to the database). However, it doesn't work when I'm trying to call it from a function in the JavaScript
file. Most solutions from guides/forums I referred to seems to work for others, but not on mine.
Any help is greatly appreciated and thank you in advance.