简体   繁体   English

我如何将结果从控制台日志保存到数据库?

[英]How i can save result from console log to the database?

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 PS Ajax request is not needed 请帮助我,我真的需要您的帮助不需要PS Ajax请求

 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 我的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));
            ?>

I don't know what you mean with Ajax request is not needed . 我不知道您对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. 但是我认为,您必须将AJAX的值传递给服务器,并让一些PHP处理存储。

恕我直言,最好的选择是:您必须将php代码移动到单独的文件中,并通过ajax进行调用,在请求有效负载中,您必须将json编码的arr和其他所需的数据放置在POST字段中,或者将一个JSON与所有数据一起放置,那么您必须在php json_decode()有效负载中,最后将其保存到数据库

First you need to know that Javascript (at least in this case) is a client side language, and php server side language. 首先,您需要知道Javascript(至少在这种情况下是客户端语言)和php服务器端语言。

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.) 也就是说,根据您的代码,当浏览器请求该页面时,服务器运行php,然后将包含javascript的页面发送给浏览器,该页面将由浏览器本身执行,并且php执行的结果(在这种情况下将打开)连接并插入空记录。)

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. 您可以运行js脚本,将日志打印到控制台,然后将日志发送到将要执行php代码的另一个页面。 You can do it in sync mode, like a normal redirect, o in async mode, using AJAX. 您可以使用AJAX在同步模式下(例如正常重定向)或在异步模式下进行操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将`console.log` 中的数据保存到 MySQL 数据库? - How to save data from `console.log` to MySQL database? 如何在不覆盖console.log的情况下将浏览器控制台日志保存到数据库中 - How can I save the browser console logs to my DB without overriding console.log 如何查看我保存到“MySQL”数据库中的结果? - How can I see the result of what I save into the "MySQL" database? 如何从 selenium 的 console.log 中提取数组? - How can I extract an array from the console.log in selenium? 如何从Electron的控制台中自动记录警告和错误? - How can I automatically log warnings and errors from the console in Electron? CSSLint:如何将日志从控制台日志保存到文件中 - CSSLint : How to save log from console log into file 如何存储计算器的结果并返回 alert 或 console.log? - How can I store the result of a calculator and return with alert or console.log? Javascript:如何将console.log结果返回为字符串形式? - Javascript: How can I return console.log result into string form? 如果在console.log 中调用了相应的函数,如何打印标签。 现在它不打印标签和结果? - How can I print label if the corresponding function is called in the console.log. Now its not printing the label and result? 如何让 console.log 输出 getter 结果而不是字符串“[Getter/Setter]”? - How can I get console.log to output the getter result instead of the string "[Getter/Setter]"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM