javascript - 如何将使用 JavaScript 动态创建的表内的数据保存到数据库

标签 javascript php html

 function create(x) {
               var field=document.createElement('fieldset');
                            var t=document.createElement('table');
                            t.setAttribute("id","myTable");
                            document.body.appendChild(t);
                            field.appendChild(t);
                            document.body.appendChild(field);

                            var row=document.createElement('th');
                            newHeader = document.createElement("th");
                            newHeader.innerText = x;
                            row.appendChild(newHeader);


                            var row1=document.createElement('tr');
                            var col1=document.createElement('td');
                            var col2=document.createElement('td');

                            var row2=document.createElement('tr');
                            var col3=document.createElement('td');
                            var col4=document.createElement('td');

                            var row3=document.createElement('tr');
                            var col5=document.createElement('td');
                            var col6=document.createElement('td');

                             col1.innerHTML="Name";
                            col2.innerHTML="<input type='text' name='stateactivityname' size='40' required>";
                            row1.appendChild(col1);
                            row1.appendChild(col2);
                            col3.innerHTML="Registration Applicable";
                            col4.innerHTML="<select name='regapp' required><option></option><option>Yes</option><option>No</option></select>";
                            row2.appendChild(col3);
                            row2.appendChild(col4);
                            col5.innerHTML="Registers Applicable";
                            col6.innerHTML="<select name='registers' required><option></option><option>Yes</option><option>No</option></select>";
                            row3.appendChild(col5);
                            row3.appendChild(col6); 
                            t.appendChild(row);
                            t.appendChild(row1); 
                            t.appendChild(row2);
                            t.appendChild(row3);
                            addrow('myTable');
      }

将数据存储到数据库的 PHP 代码是:

<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
   <?php
   $conn=new mysqli("localhost","root","","newcomplyindia");
   if($conn->connect_errno){
       echo("connection error");
   }
   $actname=$_POST["actname"];
   $industry=$_POST['industrytype'];
   $centralorstate=$_POST["cors"];
   $sql="insert into acts (actname,centralorstate) value ('".$actname."','".$centralorstate."')";
   $regapp=$_POST["regapp"];
   if($regapp=='Yes'){
       $regapp=true;
   }
   else{
       $regapp=false;
   }
   $registers=$_POST["registers"];
   if($registers=='Yes'){
       $registers=true;
   }
   else{
       $registers=false;
   }
   $sub=$_POST["sub"];
   if($sub=='Yes'){
       $sub=true;
   }
   else{
       $sub=false;
   }
   if($conn->query($sql)==true){
             echo 'act name added ';
   }
   $lastid=$conn->insert_id;
   $sql1="insert into actsstate (actid,registrationrequired,registersapplicable,sublocation)"
           . "values('$lastid','$regapp','$registers','$sub')";

   if($conn->query($sql1)==true){
        echo '<br>name and central/state added';
   } 
   $stateactivity=$_POST["stateactivityname"];
   $activityname=$_POST["activityname"];
   $activitymonth=$_POST["month"];
   $activitydate=$_POST["date"];
   $sql2="insert into activity (name,actid,activityname,activitymonth,activitydate)"
       . "values('$stateactivity','$lastid','$activityname','$activitymonth','$activitydate')";
   if($conn->query($sql2)){
       echo 'activity added';
   }
   else{
       echo 'no record';
   }
   $conn->close();
   ?>

我有一个像这样的JavaScript。该表是动态创建的。我想将该表中的数据存储到数据库中。我使用 mysqli 进行数据库连接 我是 JavaScript 新手。谁能帮我做到这一点

最佳答案

这是使用Vanilla JS(纯js)的方法

var xhttp = new XMLHttpRequest();
var url = "save.php";
xhttp.open("POST", url, true);
// uncomment this if you're sending JSON
// xhttp.setRequestHeader("Content-type", "application/json");

xhttp.onreadystatechange = function() { // Call a function when the state changes.
    if(xhttp.readyState == 4 && xhttp.status == 200) {
        // the 4 & 200 are the responses that you will get when the call is successful
        alert(xhttp.responseText);
    }
}
xhttp.send('the data you want to send');

这是一种使用 Flat PHP(纯 php)保存到数据库(在我的例子中是 mysql)的方法

$servername = "localhost";
$username   = "db_username";
$password   = "db_password";
$dbname     = "db_name";

// connect to the DB
$conn = new mysqli($servername, $username, $password, $dbname);
// check if you're connected
if ($conn->connect_error) {
    echo "Connection failed: " . $conn->connect_error;
}
else {
    // echo "connecting to DB succeeded <br> ";
}

// uncomment the following if you're recieving json
// header("Content-Type: application/json");
// $array = json_decode(file_get_contents("php://input"), true);

$sql = "INSERT INTO table_name (columns,names) VALUES (columns,values)";
if ($conn->query($sql) === TRUE) {
    echo "Data was saved successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

要了解有关 sql 命令的更多信息,我建议 w3schools tutorials

关于javascript - 如何将使用 JavaScript 动态创建的表内的数据保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35549403/

相关文章:

javascript - 如何通过回调观察 window.variable 的变化?

php - PHP如何在不使用sendmail的情况下使用mail()发送邮件?

html - curl: 从动态 url 下载

html - 如何屏蔽具有渐变背景的图像?

php - 具有多单位支持功能的库存

html - 为什么我的 div 在页面内部垂直居中而不是它的父 div?

javascript - d3词云-无法添加到div内部

javascript - 如何在 Angular 8 中为 ngSubmit() 获取选定的下拉值?

javascript - 从外部网站检索 json 文件的一部分 (sparkfun)

php - Mysql 日期字段搜索使用月份来查找即将到来的生日