简体   繁体   English

通过单击按钮php将javascript数组插入数据库

[英]Insert javascript Array into database by clicking a button php

I have the following code: - the javascript helps me select a text file and it chooses only the id from the text file. 我有以下代码:-JavaScript帮助我选择了一个文本文件,并且它仅从文本文件中选择ID。 example of text file is below: 文本文件的示例如下:

ID,Name,Surname
re-002,ram,kelu
rf-897,rem,juke

When i added the button 'loader', the javascript readText no longer displays the id that it took from the text file. 当我添加按钮“加载程序”时,javascript readText不再显示它从文本文件中获取的ID。

What i want to do is to allow user to select a text file, read only the ids, and then place the ids in my database. 我想要做的是允许用户选择一个文本文件,仅读取ID,然后将ID放在我的数据库中。

My html page: 我的html页面:

<!DOCTYPE html>
<html>
<head>
<title>reading file</title>
<script type="text/javascript">

var reader = new FileReader();
function readText(that){

    if(that.files && that.files[0]){
        var reader = new FileReader();
        reader.onload = function (e) {  
            var output=e.target.result;
            //process text to show only lines with "-":     
            output = output.split("\n").filter((line, i) => i != 0).map(line => line.split(",")[0]).join("<br/>\n");
            document.getElementById('main').innerHTML= output;
        };//end onload()
        reader.readAsText(that.files[0]);   
    }

    } 

$("#loader").on("click", function(){
    var upload = $.ajax({
                        type: "POST",
                        url: "loader.php",
                        data: {array:output},
                        cache: false,
                        beforeSend: function() {
                        }
    });

</script>
</head>
<body>
<h1> Utilisateur Nomm&#233; </h1>
<h3> Import : <button id="loader" onclick='btn()'> Import</button>
<h3> Choose file : <input type="file" onchange='readText(this)' />

</h3>
</body>
</html>

My php page 'loader.php': 我的php页面“ loader.php”:

<?php
  define ( 'DB_HOST', 'localhost' );
  define ( 'DB_USER', 'root' );
  define ( 'DB_PASSWORD', '' );
  define ( 'DB_NAME', 'dbapp' );

 $array = json_decode($_POST['output']);

 $mysqli = new mysqli('DB_HOST','DB_USER','DB_PASSWORD','DB_NAME');

$arr_id =   $mysqli->real_escape_string($array[0]);

if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}

//MySqli Insert Query
$insert_row = $mysqli->query("INSERT INTO `user` (id) VALUES($arr_id)");

if($insert_row){
print 'Success! ID of last inserted record is : ' .$mysqli->insert_id .'<br 
/>'; 
}else{
die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
// close connection 
$mysqli->close();
?>

Your code is brittle. 您的代码很脆弱。 But moving output declaration outside readText might help actually send some data 但是将output声明移到readText之外可能实际上有助于发送一些数据

 var output; // move the output declaration here var reader = new FileReader(); function readText(that) { if (that.files && that.files[0]) { var reader = new FileReader(); reader.onload = function (e) { output = e.target.result; //process text to show only lines with "-": output = output.split("\\n").filter((line, i) => i != 0).map(line => line.split(",")[0]).join("<br/>\\n"); document.getElementById('main').innerHTML = output; }; //end onload() reader.readAsText(that.files[0]); } } function btn() { var upload = new XMLHttpRequest(); upload.open("POST", "loader.php"); upload.send(JSON.stringify({ array: output })) upload.onreadystatechange = function () { if (upload.readyState === XMLHttpRequest.DONE) { if (upload.status === 200) { alert(upload.responseText); } else { alert('There was a problem with the request.'); } } } } 
 <main id="main"></main> <h1> Utilisateur Nomm&#233; </h1> <h3> Import : <button id="loader" onclick="btn()"> Import</button> <h3> Choose file : <input type="file" onchange='readText(this)' /> 

Bonus: done without jQuery. 奖励:不用jQuery就可以完成。

https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX/Getting_Started https://developer.mozilla.org/zh-CN/docs/Web/Guide/AJAX/Getting_Started

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

相关问题 Javascript数组(内部带有对象)到PHP以插入数据库 - Javascript Array(With objects inside) to PHP to insert into a database 如何通过单击按钮清除javascript中数组的内容? - How to clear the contents of an Array in javascript by clicking a button? Javascript数组到PHP到数据库 - Javascript array to PHP to database Java脚本。 通过单击按钮将数据插入数据库 - Java Script. Insert data to database by clicking button PHP / MYSQL Javascript - 使用按钮将从数据库中提取的行值插入到文本字段中 - PHP/MYSQL Javascript - Insert row value pulled from database into textfield with a button 按钮不点击不动态点击 - javascript - button not clicking not clicking dynamically - javascript 如何使用php或javascript单击添加按钮后动态添加选择框字段并将所选值存储在数据库中的所选框中 - How to add select box field dynamically after clicking add button by using php or javascript and store selected value in selected box in database 单击按钮将 JavaScript 代码链接到 Django 数据库 - linking JavaScript code to Django database upon clicking on a button 使用javascript和php将值插入数据库 - Insert values into a database using javascript and php 使用ajax,javascript和php将数据插入数据库 - insert data into database using ajax, javascript and php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM