the function is valid for onclick. checked via alert

help with the php code that this data will receive and send to the database

function SaveMenu() { var tab = document.getElementById("AddNewTable"); var elems = tab.getElementsByTagName('*'); var Stroka = ''; for (var i = 0; i < elems.length; i++) { strID = elems[i].id; if (strID.substr(0, 5) == 'EdIzm') { Stroka = Stroka + strID.substr(5) + "|"; Stroka = Stroka + elems[i].value + "|"; Stroka = Stroka + document.getElementById("EdCost" + strID.substr(5)).value + "|"; } } } 
  • данные которые я выбрал или записал, сохраняются - and where did you record and where do you save it? - Alexey Shimansky
  • my mistake, i'm sorry. function on onclick, alert th checked. - Ifrat
  • Read about XMLHttpRequest and questions will disappear. - And
  • Your strID variable is global. Add var if it was not planned - vp_arth

2 answers 2

Sending data to the server

Data can be sent to the server in three ways:

  • under the link - get-request, data in the query string, opens a new (target) or reloads the current page.
  • form - get / post request, the data in the fields of the form Page is updated or a new one is opened, if target is specified.
    As target can be specified iframe , possibly hidden.
  • AJAX - initiated from javascript, the answer also comes in javascript.
    Updating the page does not occur.

Ajax

Ajax request can be done in several ways.
The most common is using the XMLHttpRequest class:

 var xmlhttp = new XMLHttpRequest; xmlhttp.open('POST', '/api/script.php'); xmlhttp.onload = function() { console.log(xmlhttp.responseText); }; xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded') var body = [ 'name=' + encodeURIComponent('Alice'), 'age=' + encodeURIComponent('22'), ].join('&'); xmlhttp.send(body); 

Popular libraries such as jQuery have their own convenient wrappers for ajax queries: $ .ajax .


Example with modern Fetch API .
It is very convenient, but it has some problems with browser support (IE, Safari ...).
Fortunately, implementing polyfill is not very difficult.

 function save() { var endpoint = 'http://httpbin.org/post'; var Stroka = 'g/ml|13.5|15'; fetch(endpoint, { method: 'post', headers: { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" }, body: 'data='+encodeURIComponent(Stroka) }).then(res => res.json()) .then(res => console.log(res)); } save(); 

PHP:

 var_dump($_POST['data']); 

    Suppose you have already received data in php, and you need to save this data to the database.
    Since the question does not indicate the database used, we will assume that used.

    Php communication with databases is carried out through drivers implemented as extensions.

    For mysql 2 different drivers can be used:

    Consider saving data on the example of PDO .

    First you need to establish a connection with the database:

     $db = new \PDO('mysql:host=localhost;dbname=test', $user, $pass); 

    Now it only remains to save the necessary data:
    Suppose you have a Storage table with a text column data

     $stmt = $db->prepare('INSERT INTO Storage (data) VALUES (?)'); $stmt->execute([$_POST['data']]);