Insert into mySQL database from local html / javascript website

What am i trying to do

The goal of my program is to insert data from a local HTML / website JSinto an online database (non-local) mySQL.

What I tried so far

The original method that I was trying to use to achieve this was to have my local site use javascript to send data through an online file PHP, and then this file PHPinserts this information into the table mySQL, But I kept getting cross-origin errors.

Upon reaching the wall, I programmatically opened https://stackoverflow.com/a/3129449/2128 to determine whether it was even possible to post messages from the local website to an online file PHPduring which I was aware of this, this was not possible without changing the policies. related to Chrome on every machine trying to visit this local website.

Purpose of this thread

To determine whether, instead of publishing through a file, PHPinclude inserting data into the table mySQLfrom the local HTML / website JSif there is another approach that I have not considered.

The reasoning that you do not change browser policies:

, . , . Apache ..

, - local HTML/JS, mySQL, . , , , , PHP , , - , mySQL , - HTML/JS?

JS:

function uploadPetData(petName, petAge, petType) {

    var urlString ="Pet_Name="+petName+"&Pet_Age="+petAge+"&Pet_Type="+petType;
    $.ajax({
        type: 'POST',
        url: 'http://example.com/test.php',
        crossDomain: true,
        data : urlString,   
        contentType:contentType,   
        success: function(responseData, textStatus, jqXHR) {
            var value = responseData.someKey;
        }
    });
}

PHP:

<?php
$con=mysqli_connect("...","...","...","...");

$petName = $_POST['Pet_Name'];
$petAge = $_POST['Pet_Age'];
$petType = $_POST['Pet_Type'];

$petName = mysqli_escape_string($con, $petName);
$petAge = mysqli_escape_string($con, $petAge);
$petType = mysqli_escape_string($con, $petType);

$query = mysqli_query($con, "INSERT INTO Pets (Name, Season, Episode)
                             VALUES ('$petName', '$petAge', '$petType')");
mysqli_close($con);
?>
+4
2

js , db. js .

js, api . - (nginx apache), js. ajax promises - , , RxJs Observables.

, .

0

, HTML/JS - mySQL . , , , , PHP , , -, mySQL , - HTML/JS?

; , .

, ,

, , script .

, , , , , ; <form> ; POST .

, - HTML/JS . mySQL db . , - HTML/JS, - mySQL db. . JS PHP mySQL. , , .

, "", , cron

, , mySQL

0

Source: https://habr.com/ru/post/1671418/


All Articles