使用js或jquery将POST发送到mysql

I want to send an array of strings on my JavaScript to a MySQL table. What is the best way and how can I do it?

You can use AJAX POST like so:

function sendArray()
{
var myArray = ["one","two","three"];
$.ajax({
url     : 'YOUR_URL',
method    : 'POST',
data :{
 arrayData:myArray
},
success   : function(response)
{
alert("data sent response is "+response);
},
error : function(e)
{
alert("data not sent")
}
});
}
</script>
<button onclick="sendArray();">Send Array</button>

here myArray is the string array you want.

on the PHP backend you can get this data as:

<?php
$arrayData = $_REQUEST['arrayData'];
foreach($arrayData as $data)
{
    echo $data;
}

First of all understand the difference. Javacript is client side scripting language. It will not interact on my MySql on its own you will need to makean ajax call and then use server side scripting language like PHP to fulfil your purpose

you can use jquery to create an ajax call like

$.ajax({
type: "POST",
url: "somefile.php",
data: dataUwanttoinsert,
success: success,
dataType: dataType
});

then add your SQL query to the php file and when you make this ajax call it will do the query you have written

$.ajax({
    url: 'url',
    data: {'a': a, 'm': m},
    type: 'POST',
    cache: false ,
    success: function (data, textStatus, jqXHR) {
    alert(data);
    }
});

How I'd do it is by sending the string as a HTTP request payload using AJAX to a server that is connected to your MySQL database. Once the data is received by the server, it can process it and insert it to the database.

Here are some docs to help you get started:

  1. Intro to ajax
  2. Inserting data to MySQL using PHP