javascript - 使用 Javascript 输入数据未存储在数据库中?

标签 javascript php html mysql ajax

我的 PHP 页面中有一个表,位于 PHP 的 HTML block 内。下面是该表的代码。

<form enctype="application/x-www-form-urlencoded">
<table width="200" border="1">
  <tr>
    <td>Product</td>
    <td>Promotional Price</td>
    <td>Regular Price</td>
    <td>Stacking</td>
  </tr>
  <tr>
    <td><input type="text" id="product"></td>
    <td><input type="text" id="pp1"></td>
    <td><input type="text" id="rp1"></td>
    <td><input type="text" id="stacking"></td>
  </tr>
</table>
<div id ="div1">
<input type="button"  value="Submit"  onClick="PostData();"/><br/>
</div>
</form> 

将其发送到另一个 PHP 的 JavaScript 代码如下。

<script type="text/javascript">
function PostData() {




    // 1. Create XHR instance - Start
    var xhr;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
    else {
        throw new Error("Ajax is not supported by this browser");
    }
    // 1. Create XHR instance - End

    // 2. Define what to do when XHR feed you the response from the server - Start
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status == 200 && xhr.status < 300) {
                document.getElementById('div1').innerHTML = xhr.responseText;
            }
        }
    }
    // 2. Define what to do when XHR feed you the response from the server - Start
    var product = document.getElementById("product").value;
    var pp1 = document.getElementById("pp1").value;
    var rp1 = document.getElementById("rp1").value;
    var stacking = document.getElementById("stacking").value;

    // var image = document.getElementById("image").value;
    // 3. Specify your action, location and Send to the server - Start
xhr.open('POST', 'report.php');
    //xhr.open('POST', 'config.php');
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("product=" + product + "pp1=" + pp1 + "rp1=" + rp1 + "stacking=" + stacking);
}

</script>

将值存储到数据库的 PHP 如下。

    <?php  
    //Updated after the answer from AnikIslam
    $servername = "localhost";
$username = "root";
$password = "";
$dbname = "testing";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO tbl_report (product,pp1, rp1,stacking) VALUES ('$product', '$pp1', '$rp1','$stacking')";

if ($conn->query($sql) === TRUE) {
    echo "Successful";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
    ?>

如屏幕截图所示,我有 2 个 block 来输入数据。现在数据仅从第一个 block 输入数据库。我希望数据从第二个 block 存储到数据库中的第二行。如何实现。

enter image description here

最佳答案

尝试这样

 xhr.send("product=" + product + "&pp1=" + pp1 + "&rp1=" + rp1 + "&stacking=" + stacking);

对于 PHP 部分

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testing";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO tbl_report (product,pp1, rp1,stacking) VALUES ('$product', '$pp1', '$rp1','$stacking')";

if ($conn->query($sql) === TRUE) {
    echo "Successful";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

编辑

每个表单都应该有唯一的 ID

在提交按钮中传递 block 名称

像这样

第一个区 block

onClick="PostData('Block1');"

第二 block

onClick="PostData('Block2');"

JS

function PostData(block){



  if(block==="Block1"){
     // fetech data from block1
  }
  else if(block==="Block2"){
     // fetech data from block2
  }

}

关于javascript - 使用 Javascript 输入数据未存储在数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31869515/

相关文章:

javascript - 如何从 Google 表格中的单元格获取值,以用作 Google 脚本中的数字变量

javascript - CSS/JS : CPU lightweight loading spinner

javascript - 数据库写入时未触发云函数

php - 用于 mysql 和 php 的 sql linter

html - 'after' 的样式在浏览器中有所不同

javascript - React - 在功能组件中测试外部功能

php - Mysql查询: switch if record come from table news or news_collect

html - 将 Bootstrap 4 导航栏与 wordpress 4.x 合并的问题

javascript - 现有 .js 文件中的 jQuery 代码

php - 检索一个 id 数组?