PHP HTML表单多维数组MySQL

时间:2014-11-20 06:41:34

标签: php html mysql arrays

我有带代码的php页面:一切正常。

<?php
session_start();
include 'db.php';//database connection

if ( isset($_POST['permission']))
{
$x=$_POST['permission'];
$a=print_r($x);//print array structure

$permit_group=array('Dean','Principal');//redefine of permission array as already    
//defined in form permission[Dean][] etc.

$permit_no=count($permit_group);//count array size of all defined areas
$b=print_r($permit_no);//its out put is 2; i am looking for: $permit_no=count($x)???

for($i=0; $i<$permit_no; $i++)//for loop for all possible permission (Dean, Principal   
etc.)
{

$jj=count($permit_group['$i']);//count size of subarea given to to individual area 
//person and output is 0; i am looking for: $jj=count($x['area'])???
$c=print_r($jj);

for($j=0; $j<jj; $j++)
{
$x1=$permit_group[$i];//i am looking for this:$x1=$x[$i]; How can i do this ?
$x2=$x[$permit_group[$i]][$j];//i am looking for this:$x2=$x[$i][$j];How can i do this?
mysqli_query($connection,"INSERT INTO upermission (area, subarea) VALUES   
('$x1','$x2')");
}
}
}
?>
<div>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div>
<ul>Dean
<li><input type="checkbox" name="permission[Dean][]" value="list"/><label>List 
Principal   
</label></li>
<li><input type="checkbox" name="permission[Dean][]" value="Edit"/><label>Edit     
Principal   
</label></li>
</ul>

<ul>Principal: 
<li><input type="checkbox" name="permission[Principal][]" value="create"/>  
<label>Create Faculty</label></li>
<li><input type="checkbox" name="permission[Principal][]" value="edit"/><label>Edit  
Faculty</label></li>
<li><input type="checkbox" name="permission[Principal][]" value="add"/>  
<label>Add Faculty</label></li>
<li><input type="checkbox" name="permission[Principal][]" value="delete"/>  
<label>Delete Faculty</label></li>
</ul>
</div>
<input type="submit" name="submit" />
</form>
</div>

我的问题是:

1)我希望获得Area(Dean,Princial等)和Subarea(添加,编辑等)的价值    HTML表单只是不像我在php部分创建的那样再次创建数组?

2)还要计算仅检查区域和子区域的大小,因为区域的大小和
   subarea是不同的,运行for循环以将数据插入数据库表?

请指导我该怎么办?

由于

3 个答案:

答案 0 :(得分:1)

HI,如果您的问题仅与循环有关,请尝试此操作。

$post_val = $_POST['permission'];   
foreach($post_val as $key=>$value)
{
    $x1 = $key;     
    for($i=0;$i<count($value);$i++)
    {
        $x2 = $value[$i];           
        mysqli_query($connection,"INSERT INTO upermission (area, subarea) VALUES
       ('".$x1."','".$x2."')");         
    }
}

如果你只想在HTML中构建所有这些,那么你必须处理javascript或jQuery。

答案 1 :(得分:0)

这会帮助你吗?

foreach ($_POST['permission'] as $area) {

 foreach ($_POST['permission'][$area] as $subarea) {

  mysqli_query($connection, "INSERT INTO upermission (area, subarea) VALUES ('$area','$subarea')");

 }


}

答案 2 :(得分:0)

这个例子不是我如何从头开始构建它。我尽量保持你的代码。

当HTML不是动态的并且您正在使用AJAX时,您应该分离HTML和PHP。

我还没有对其进行测试,因此可能存在一些(语法)错误。

html:

&#13;
&#13;
<form>

  <ul>Dean
    <li>
      <input id="dean_list" type="checkbox" value="List Principal"/>
    </li>
    <li>
      <label>Edit Principal</label>
      <input id="dean_edit" type="checkbox" value="Edit Principal"/>
    </li>
  </ul>

  <ul>Principal: 
    <li>
      <input id="principal_create" type="checkbox" value="Create Faculty"/>  
    </li>
    <li>
      <input id="principal_edit" type="checkbox" value="Edit Faculty"/>
     </li>
    <li>
      <input id="principal_add" type="checkbox" value="Add Faculty"/>  
    </li>
    <li>
      <input id="principal_delete" type="checkbox" value="Delete Faculty"/>  
    </li>
  </ul>

  <input onclick="submitForm()" type="button" value="submit">


</form>
&#13;
&#13;
&#13;

将它放在标题中,这为您提供了额外的javascript函数(JQuery):

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

javascript(处理)ajax-request:

&#13;
&#13;
function submitForm() {
  
  if (/* validate */) { // you could validate
    
    var querystring = '';
  
    if (document.getElementById("dean_list").checked) {
      querystring = querystring + 'permission[dean]=list&';
    }
  
    if (document.getElementById("dean_edit").checked) {
      querystring = querystring + 'permission[dean]=edit&';
    }

    if (document.getElementById("principal_create").checked) {
      querystring = querystring + 'permission[principal]=create';
    }
  
    if (document.getElementById("principal_edit").checked) {
      querystring = querystring + 'permission[principal]=edit';
    }

    if (document.getElementById("principal_add").checked) {
      querystring = querystring + 'permission[principal]=add';
    }

    if (document.getElementById("principal_delete").checked) {
      querystring = querystring + 'permission[principal]=delete';
    }
    
    /* AJAX code to submit form. */
    $.ajax({
    type: "POST",
    url: "", // the url of your php file
    data: querystring,
    cache: false,
    success: function(html) { // html is the response of the php
    alert(html);
    
  }
  else {alert('Message you want to display when input is not valid');}
  
}
&#13;
&#13;
&#13;

php:

include 'db.php'; //database connection

if (isset($_POST['permission'])) {

 foreach ($_POST['permission'] as $area) {

  foreach ($_POST['permission'][$area] as $subarea) {

   mysqli_query($connection, "INSERT INTO upermission (area, subarea) VALUES ('$area','$subarea')");

  }

 }

 echo 'Records added';

}
else {echo 'No records added.';}