如何将数组从json stringify传递到php函数 [英] How to pass an array from json stringify into php function
问题描述
我对此事感到困惑.
我有一个从HTML表中构建的数组作为json stringify
数据.
I have an array that I have built from an HTML table as a json stringify
data.
我想将该数组传递给php
函数,但没有太大的成功.
I want to pass that array into the php
function but with not too much success.
我尝试了很多方法,但是没有一个方法无法获取数据数组.
I tried many methods but none can't get the array of data.
这是数据的数组:
// JavaScript Docume
$(document).on('click','#display_data',function(e){
var convertTableToJson = function()
{
var rows = [];
$('.table-bordered tr:has(td)').each(function(i, n){
var $row = $(n);
rows.push([
$row.find('td:eq(0)').text(),
$row.find('td:eq(1)').text(),
$row.find('td:eq(2)').text(),
$row.find('td:eq(3)').text(),
$row.find('td:eq(4)').text(),
$row.find('td:eq(5)').text(),
$row.find('td:eq(6)').text(),
$row.find('td:eq(7)').text(),
]);
});
return JSON.stringify(rows);
};
$(function(){
alert(convertTableToJson ());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-bordered">
<thead>
<tr>
<th>Kode Material</th>
<th>Storage Location</th>
<th>Movement Type</th>
<th>Id Indentifier</th>
<th>Item</th>
<th>Date Input</th>
<th>Netto</th>
<th>Unit</th>
<th><input type="checkbox" id="mycheckbox1" name="mycheckbox1"></th>
</tr>
</thead>
<tbody>
<tr>
<td>101200</td>
<td>WCB</td>
<td>101</td>
<td>5006540050</td>
<td>1</td>
<td>10.08.2017</td>
<td>23.970</td>
<td>KG</td>
<td><input type="checkbox" class="mycheckbox" name="mycheckbox"></td>
</tr>
<tr>
<td>101200</td>
<td>WCB</td>
<td>101</td>
<td>5006539985</td>
<td>1</td>
<td>10.08.2017</td>
<td>42.970</td>
<td>KG</td>
<td><input type="checkbox" class="mycheckbox" name="mycheckbox"></td>
</tr>
</tbody>
</table>
<button id='display_data'>Click me </button>
然后我尝试制作这样的代码,以将该数组传递到PHP
and I try to make a code like this for passing that array into the PHP
$(document).on('click','#display_data',function(e){
var convertTableToJson = function()
{
var rows = [];
$('.table-bordered tr:has(td)').each(function(i, n){
var $row = $(n);
rows.push([
$row.find('td:eq(0)').text(),
$row.find('td:eq(1)').text(),
$row.find('td:eq(2)').text(),
$row.find('td:eq(3)').text(),
$row.find('td:eq(4)').text(),
$row.find('td:eq(5)').text(),
$row.find('td:eq(6)').text(),
$row.find('td:eq(7)').text(),
]);
});
$(".table-bordered").val(JSON.stringify(table_data));
};
var data = $(".table-bordered").val();
$.ajax({
data:data,
type:"POST",
url:"../php/tagihan/save_data.php",
dataType:"html",
success: function(data){
alert ("Data:" + data);
}
});
});
,但是当我尝试echo
并尝试print_r
时,它可以显示数组.这是我编写的php
代码.
but it can give the array displaying when I try to echo
that and try to print_r
. and this is the php
code that I made.
<?php
include('../../Connections/koneksi.php');
// reading json file
$array = json_decode( $_POST['table-bordered'] );
print_r ($array);
?>
推荐答案
您希望$_POST
键存在于$_POST
中,但是它不存在.您希望它存在是因为HTML内有一个table-bordered
,但这与您传递的内容没有任何关系.因此,第一步是确保该密钥将存在于您的$_POST
:
You expect a table-bordered
key to be existent in $_POST
, yet, it does not exist. You expect it to exist because you have a table-bordered
inside the HTML, but that does not have a connection to what you passed. So, the first step is to make sure that this key will exist in your $_POST
:
$.ajax({
data:{"table-bordered": data},
type:"POST",
url:"../php/tagihan/save_data.php",
dataType:"html",
success: function(data){
alert ("Data:" + data);
}
});
这样,我们以正确的方式传递了data
,但是我们需要确保data
包含所需的值.您不能在table
上为此目的使用jQuery .val()
,但是我也看不到将其放入HTML的意义.您可以将其存储在变量中:
This way we pass data
in the right way, but we need to make sure that data
contains the values we need. You cannot use jQuery .val()
for this purpose on a table
, but I do not see the point of putting this into the HTML either. You can store it inside a variable:
$(document).on('click','#display_data',function(e){
var rows = []; //Migrated here, as we will need it later
var convertTableToJson = function()
{
$('.table-bordered tr:has(td)').each(function(i, n){
var $row = $(n);
rows.push([
$row.find('td:eq(0)').text(),
$row.find('td:eq(1)').text(),
$row.find('td:eq(2)').text(),
$row.find('td:eq(3)').text(),
$row.find('td:eq(4)').text(),
$row.find('td:eq(5)').text(),
$row.find('td:eq(6)').text(),
$row.find('td:eq(7)').text(),
]);
});
//This line will no longer be needed
//$(".table-bordered").val(JSON.stringify(table_data));
};
//Incorrect line
//var data = $(".table-bordered").val();
//Correct line, it is unnecessary though, as you can directly pass rows
//as data instead, but kept this to make sure the code is close to the
//initial version
var data = rows;
$.ajax({
data:{"table-bordered": data},
type:"POST",
url:"../php/tagihan/save_data.php",
dataType:"html",
success: function(data){
alert ("Data:" + data);
}
});
});
这篇关于如何将数组从json stringify传递到php函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!