2

I have a completed selection and then there is a table that I fill out as well, then by clicking on the button the selected result goes to the console, but how to save it to the database? Please help me, I really need your help P.S. Ajax request is not needed

 function submit() {
    $table.find('tr').each(function(){
        var rowValues = {};
        $(this).find('td').each(function(i) {
            var value = $(this).find("input").val();
            rowValues[columnNames[i]] = value;
        });
        arr.push(rowValues);
    });

    console.log(arr);

    var selector = document.getElementById('category_select');
    var id = selector[selector.selectedIndex].id;
    console.log(id);

    var selector = document.getElementById('patient_select');
    var value = selector[selector.selectedIndex].value;
    console.log(value);  //How i can save result in db ?
  $.ajax({
            url: 'insert.php',
            type: 'POST',
            data: {
                data: value,
            },
            dataType: 'json',
            beforeSend: function(xhr) {
                $('#bt').text('OK');
            },
            success: function(data) {
                $('#bt').text('Send');
                alert(data);
            }
        });
}

my insert.php

  <?php 

        $value = $_POST['value'];

        $link = mysqli_connect(
            'localhost', 
            'root',       
            '',   
            'answer_result');     
        if (!$link) {
            printf("ERR: %s\n", mysqli_connect_error());
        }

        mysqli_query($link,"INSERT INTO answer_result_table (`answer_content`, `patient_id`)
        VALUES ( '$value', '$value')") 
        or die(mysqli_error($link));
            ?>
XXXXXX
  • 43
  • 6

3 Answers3

0

I don't know what you mean with Ajax request is not needed.

But I think, that you have to pass that value with AJAX to the Server and let some PHP handle the storage.

Progaros
  • 45
  • 1
  • 12
0

IMHO the best option is that: You have to move php code to separate file and call it via ajax, in request payload you have to place json encoded arr and other needed data and place it in POST fields or make one JSON with all data, then you must in php json_decode() payload and finally save it to db

  • See also [How to get body of a POST in php?](https://stackoverflow.com/questions/8945879/how-to-get-body-of-a-post-in-php) – Adder Sep 11 '19 at 09:23
  • I updated the question. please see if ajax and insert are written correctly. – XXXXXX Sep 11 '19 at 09:56
0

First you need to know that Javascript (at least in this case) is a client side language, and php server side language.

that's mean, according your code, when the browser request this page, the server runs php and then send to the browser the page with the javascript, that will be executed by the browser itself, and the result of php execution (in this case opens a connection and insert a empty record.)

You can run the js script, print the log to the console, and then send the log to another page where the php code will be executed. You can do it in sync mode, like a normal redirect, o in async mode, using AJAX.