[英]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.