Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取td中的文本框值,将其存储在Javascript数组中并传递回PHP_Javascript_Php_Jquery_Ajax_Html Table - Fatal编程技术网

获取td中的文本框值,将其存储在Javascript数组中并传递回PHP

获取td中的文本框值,将其存储在Javascript数组中并传递回PHP,javascript,php,jquery,ajax,html-table,Javascript,Php,Jquery,Ajax,Html Table,我用PHP显示了一个表。只有两行,第一行包含标题,第二行包含td内的文本框。在脚本的这一部分中,我试图让用户输入一些值,然后我想根据标题名称获取这些值,以便我可以使用它们进行查询 我已经根据您在下面代码中看到的标题数量创建了表标题和文本框。这是在一个函数中,其他部分只是表和查询的代码 现在我的主要问题是,我想根据标题获取每个文本框的值。例如,如果标题名为Configuration,并且该标题下文本框中的值是用户键入的1,5,8。单击按钮后,将执行一个新的PHP函数。这个函数就是我用来获取文本框值

我用PHP显示了一个表。只有两行,第一行包含标题,第二行包含td内的文本框。在脚本的这一部分中,我试图让用户输入一些值,然后我想根据标题名称获取这些值,以便我可以使用它们进行查询

我已经根据您在下面代码中看到的标题数量创建了表标题和文本框。这是在一个函数中,其他部分只是表和查询的代码

现在我的主要问题是,我想根据标题获取每个文本框的值。例如,如果标题名为
Configuration
,并且该标题下文本框中的值是用户键入的
1,5,8
。单击按钮后,将执行一个新的PHP函数。这个函数就是我用来获取文本框值的函数。我曾尝试在Javascript中迭代表单元格,但我一直无法获取文本框值

如果可能的话,我想知道如何在Javascript中将所有标题存储在一个数组中,并相应地存储文本框值。如果它是在PHP中,则类似于
$user\u input[$header][$text\u val]
。或者,只要我能够获得与头相对应的值,就可以使用单独的数组,以便在查询中使用它

注意:我使用ajax的post将值传递回我的PHP文件。该表是使用动态值创建的,因此它将在同一个PHP文件中获取值并将其传递回该文件(称为database.PHP)。我看到了一些与
json\u encode
有关的代码,但我从未使用过与
json
有关的任何代码,因此如果有使用json的解决方案,请原谅我,并帮助我用简单的术语解释它

while($row = mysql_fetch_assoc($result))
{
    $board_name = $row['board_name'];
    $header[] = $board_name;
}

foreach($header as $index => $head)
{
    $html .= "<th>$head</th>";
}

$html .= "
    </tr>
    <tr id=\"config_input\">
        <td colspan = \"3\">
            Please Key in Configuration:
        </td>";

foreach($header as $index => $head)
{
    $html .= "
    <td>
        <input type=\"text\" style=\"width: 70px\"/>
    </td>";
}

$html .= "</tr>";

使用下面的代码,您可以获得标题和值的JSON对象。请查看:

HTML:

可以使用AJAX将此json对象发送到php

$.ajax({
    type: "POST",
    url: "file.php",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    data: json
}).done(function(JqXHR){
    alert(JqXHR);
});

我想您应该使用诸如#header和#config等ID。。。。使用类(.header)而不是Id。。。。并使用getElementsByClassName访问它。对于
console.log(json)
部分,它是否也应该显示值?因为我看到的是
LOG:[对象对象],[对象对象对象],[对象对象]
等等。如果不应该,我如何才能看到里面的值呢?您可以使用JSON.stringify()如果导航器没有正确显示JSON对象,请替换为:console.log(JSON);使用console.log(JSON.stringify(JSON));好吧,我设法让它工作,并得到正确的值显示,那么PHP部分呢?我应该使用
json\u decode
来访问它,但是它会是
json\u decode(json)
吗?只需在php文件中使用$varName=$\u POST就可以了,因为您直接发送json对象,并且php文件接收到类似数组的内容。。。您还可以在类似ajax的数据中使用名称发送:{varName:json},在php中使用$varName=$\u POST['varName']
<table id="myTable">
<thead>
    <tr>
        <th>id</th>
        <th>name</th>
        <th>comment</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td><input value="1" type="text"></td>
        <td><input value="name1" type="text"></td>
        <td><input value="coment1" type="text"></td>
    </tr>
</tbody>
var header = [];
var values = [];
var json = [];

$('#myTable').find('th').each(function() {
    header.push($(this).html());
});


$('#myTable').find('tbody > tr > td').each(function() {
    values.push($(this).find('input').val());
  str.push($(this).html());
});

for (var i in header) {
  json[i] = {};
  json[i][header[i]] = values[i];
}

console.log(json);
$.ajax({
    type: "POST",
    url: "file.php",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    data: json
}).done(function(JqXHR){
    alert(JqXHR);
});