我正在创建一个单页PHP应用程序并尝试通过ajax .post()提交表单,而不是通过导航的典型表单提交。麻烦的是我无法弄清楚如何获取所有信息的数组,所以我可以将它传递给.post()命令。
下面的代码生成表格,并从数据库中提取所有数据,如果我将其包装在表单标签中,它将按原样工作。如果单击侧栏上的Ajax菜单,您可以在此处查看已处理的版本以及一些调试代码:http://tinyurl.com/lq4fe8a
<?
include 'db.php';
$i=0;
foreach($db->query('SELECT * from navmenu') as $row) {
$rowarray[$i] = array();
echo ($i % 2)?'<tr class="odd">':'<tr class="even">';
print_r("<td width=200>" . $row[2] . "</td>");
print_r("<input type=\"hidden\" id=\"");
$row[2] = str_replace(' ', '_', $row[2]);
print_r("". $row[2] . "[0]\" value=\"" . $row[0] . "\">");
print_r("<td><input type=\"textbox\" id=\"" . $row[2] . "[1]\" size=\"75\" value=\"" . $row[3] . "\"></td>");
//parent selection
print_r("<td><select id=\"" . $row[2] . "[2]\">");
if($row[1] == "0"){
print_r("<option value=\"0\" selected>No Parent</option>");
}
foreach($db->query('SELECT * from navmenu') as $option) {
print_r("<option value=\"" . $option[0] . "\"");
if($row[1] == $option[0]){
print_r(" selected");
}
print_r(">" . $option[2] ."</option>");
}
// end foreach
print_r("</select></td>");
print_r("<td><input type=\"textbox\" id=\"" . $row[2] . "[3]\" size=\"5\" value=\"" . $row[4] . "\"></td>");
// setup status whether active or not
print_r("<td><input type=\"hidden\" id=\"" . $row[2] . "[4]\" value=\"INACTIVE\"> "); //set value so it never passes NULL
if ($row[3] == "ACTIVE"){
print_r("<input type=\"checkbox\" id=\"" . $row[2] . "[4]\" value=\"ACTIVE\" checked=\"true\"></td></tr>");
}
else{
print_r("<input type=\"checkbox\" id=\"" . $row[2] . "[4]\" value=\"ACTIVE\"></td></tr>");
}
print_r("\n");
$rowarray[$i]= array("$row[2][0]", "$row[2][1]", "$row[2][2]", "$row[2][3]", "$row[2][4]");
$i++; //increment by one
}
?>
在我在foreach循环中定义$ rowarray的底部,我试图将表单中的所有信息添加到单个数组中,但我最终得到了这个:
array
0 =>
array
0 => string 'Settings[0]' (length=11)
1 => string 'Settings[1]' (length=11)
2 => string 'Settings[2]' (length=11)
3 => string 'Settings[3]' (length=11)
4 => string 'Settings[4]' (length=11)
1 =>
array
0 => string 'Grid_Demo[0]' (length=12)
1 => string 'Grid_Demo[1]' (length=12)
2 => string 'Grid_Demo[2]' (length=12)
3 => string 'Grid_Demo[3]' (length=12)
4 => string 'Grid_Demo[4]' (length=12)
2 =>
array
0 => string 'TEST[0]' (length=7)
1 => string 'TEST[1]' (length=7)
2 => string 'TEST[2]' (length=7)
3 => string 'TEST[3]' (length=7)
4 => string 'TEST[4]' (length=7)
它将项目存储为字符串而不是子数组,因此我无法执行类似print_r($ rowarray [0] [0])的操作来获取内容。不确定我做错了什么或从哪里去。对于它的价值,这里是javascript提交代码。
<script>
$(document).ready(function () {
$("a[id^=formsubmit]").click(function ( event ) {
$.post("functions.php?do=adminmenu",{
// Send these values via POST
<?
foreach ($rowarray as $index => $option) {
foreach ($option as $rowitem => $rowval)
{
$option = str_replace(' ', '_', $option);
print "$index:$('[id^=\'$rowval\']').val(), ";
}
}
print "$index:$('#$rowval').val()";
?>
}
,function (data) {
$( ".contentarea2" ).html( data );
},"html");
});
});
</script>
答案 0 :(得分:0)
看起来我忘记将它们存储为$ key =&gt; $ value对。至少问题的第一部分是通过这样解决的:
$rowarray[$row[0]]= array("id" => "$row[0]", "parentid" => "$row[1]", "name" => "$row[2]", "url" => "$row[3]", "sortorder" => "$row[4]", "status" => "$row[5]", "tooltip" => "$row[6]");
然后将其存储起来:
array
1 =>
array
'id' => string '1' (length=1)
'parentid' => string '0' (length=1)
'name' => string 'Settings' (length=8)
'url' => string 'settings.php' (length=12)
'sortorder' => string '0' (length=1)
'status' => string 'INACTIVE' (length=8)
'tooltip' => string 'tooltip' (length=7)
2 =>
array
'id' => string '2' (length=1)
'parentid' => string '0' (length=1)
'name' => string 'Grid_Demo' (length=9)
'url' => string 'grid.php' (length=8)
'sortorder' => string '100' (length=3)
'status' => string 'INACTIVE' (length=8)
'tooltip' => string '' (length=0)