0

I have php page with code: Everything is working fine.

<?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>

My questions are:

1) I am looking to get Area (Dean,Princial etc) and Subarea (Add, Edit etc.) value from HTML form only not to create array again as i created in php part?

2) Also to calculate size of only checked Area and Subarea, because size of Area and
subarea is different, to run both for loops to insert data into database table?

Please guide me how can i do?

Thanks

3 Answers 3

1

HI, If your problem is only with the loops, then try this.

$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."')");         
    }
}

If you want to build all this in the HTML only, then you have to deal with either javascript or jQuery.

Sign up to request clarification or add additional context in comments.

3 Comments

I want to ask one more thing, if i insert this php page like <iframe src=x.php></iframe> into iframe tag into another y.php page having html form. then how i insert this x.php form values into database using y.php page. any addition any of these file x.php or y.php.
Why iframe? Using the iframes is not recommended for form submitting. Because if you submit the data, the response will be shown in the same iframe, and that iframe will act as a mini browser. You can achieve this easily by using the Ajax. Anyway you can submit the data in the iframe to the intended page, but if have any data in parent page may refreshed, while submitting the data in the iframe.
Can you help me to get these stored values from database, if i want update any of these or all these value?
0

Will this help you out?

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

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

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

 }


}

6 Comments

Eddy: this isn't working even no entry in datbase. Is any other possible solution?
Do you get a error message? Can you echo $area and $subarea in the second foreach-loop? What are the values?
How does the $_POST-array look? Use: var_dump($_POST)
Array structure is like this: Array ( [Dean] => Array ( [0] => list [1] => Edit ) [Principal] => Array ( [0] => create [1] => edit [2] => add [3] => delete ) ) Anyway i got the answer. Can you help me to insert these value as a iframe page value (x.php), controlled by parent page html form (y.php) and insert into database along with parent page values.
I will help you. Later on I will have some time.
|
0

This example is not how I would build it from scratch. I try to stay with your code as much as possible.

You should seperate HTML and PHP when the HTML is not dynamic and you are using AJAX.

I haven't tested it so there might be some (syntax) errors.

The html:

<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>

Put this in the header, this gives you additional javascript function (JQuery):

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

The javascript (handles) the ajax-request:

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');}
  
}

The 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.';}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.