Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8331241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:21:19+00:00 2026-06-09T02:21:19+00:00

I am trying to submit multiple arrays with a checkbox form but I am

  • 0

I am trying to submit multiple arrays with a checkbox form but I am only able to submit one array at the moment, here is what I have so far

In this example I am submitting an array of numbers with the delete[] array, this array gets processed properly, I also want to submit the array condition[] this does not get processed properly, what is the best way to solve this issue?

php code

$catalog = $database->getInventory();

if($catalog){   
   $numRows = sizeof($catalog);//count
   echo "<b>Book Count:</b> ".$numRows."<br>";

   echo "<form method='post' action='inventory.php'>";
   echo "<table id='example' class='tablesorter' border='0' cellpadding='0' cellspacing='1'>";
   echo "
      <thead>
         <tr>
           <th>ISBN</th>        
           <th>Title&nbsp;&nbsp;&nbsp;</th>
           <th>Rank&nbsp;&nbsp;</th>
           <th>Condition&nbsp;&nbsp;</th>   
           <th><input type='checkbox' name='delete' value='all' /></th>
         </tr>
      </thead>\n";


   foreach($catalog as $elem){
      echo "
         <tr>
            <td>".$elem["isbn"]."</td>
            <td>".$elem["title"]."</td>
            <td>".$elem["rank"]."</td>
            <td>".$elem["condition"]."</td>
            <td> 
               <input type='checkbox' name='add[]' 
                  value='".$elem['isbn']."_".$elem['condition']."_"."' />
            </td>
         </tr>";    
   }

   echo "</table>";
   echo "</form>";
}

example html markup

<form method='post' action='inventory.php'>
   <table>
      <tr>
         <td>
            <input type='hidden' name='addInventoryBook' value='1'>
            <input type='submit' value='Add' />
         </td>
      </tr>

      <tr>
         <td>
            <input type='checkbox' name='add[]' value='100001_used' />
         </td>
      </tr>

      <tr>
         <td>
            <input type='checkbox' name='add[]' value='100001_new' />
         </td>
      </tr>

      <tr>
         <td>
            <input type='checkbox' name='add[]' value='100003_new' />
         </td>
      </tr>

   </table>
</form>

php function

function Inventory(){   
   if(isset($_POST['addInventoryBook'])){
      if(isset($_POST['add']) && is_array($_POST['add'])){
     $arr = array();
         foreach($_POST['add'] as $checkbox){
            $temp = explode("_", $checkbox);

            $arr[] = array(
               "isbn"       => $temp[0],
               "condition"      => $temp[1],
               "sub_condition"  => $temp[2]
            );
         }              
     $this->addInventoryBook($arr); 
      }

      else{
         echo "No values have been set";
      }
 }


function addInventoryBook($arr){
   foreach($arr as $elem){
      //if used get sub-category
      if($elem['condition']=='used'){
         echo $elem['isbn']."-".ucfirst($elem['condition'])
            .ucfirst($elem['sub_condition'])."<br>";
      }

      else if($elem['condition']=='new'){
         echo $elem['isbn']."-".ucfirst($elem['condition'])."<br>";
      }

   }
}

All I want is to basically be able to pass two arrays to my php script

current output

100001
100002
100003

desired output

100001   good
100002   new
100003   new
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:21:21+00:00Added an answer on June 9, 2026 at 2:21 am

    The problem that you are having, I suspect, is that only the checkboxes that are checked will be passed back to the server, whereas all the hidden fields will always be passed so the lengths of the arrays will differ and the keys wont correspond.

    The solution to this is actually relatively simple – you just need to specify the keys for the condition array so you can match the values up again. Something like this:

    HTML:

      <tr>
         <td>
            <input type='hidden' name='condition[100001]' value='good' />
            <input type='checkbox' name='delete[]' value='100001' />
         </td>
      </tr>
    
      <tr>
         <td>
            <input type='hidden' name='condition[100002]' value='new' />
            <input type='checkbox' name='delete[]' value='100002' />
         </td>
      </tr>
    

    PHP:

    foreach ($_POST['delete'] as $delete) {
      $condition = $_POST['condition'][$delete];
      // Do stuff
    }
    

    This ties the values in the $_POST['condition'] array back up with the $_POST['delete'] array so everything will match up again.

    EDIT

    The way the keys are being created above is not great and in retrospect it is the wrong way to do it.

    To demonstrate the right way to do it, let’s imagine we have the following array, nice and simple:

    $books = array(
      10001 => 'good',
      10002 => 'new',
      10003 => 'new',
      10004 => 'good'
    );
    

    What we need to do is tie up the two inputs that are associated with each book, which means we need a set of key/value pairs that can be matched up. That sounds like an array to me. But unlike the example above, the keys should be irrelevant to the data – they don’t need to mean anything, because all we want is the data.

    What we need to do is specify every single key explicitly (no stack-style array pushes) and make the keys agnostic of the data they relate to.

    We can do this:

    $i = 0;
    foreach ($books as $isbn => $condition) {
      echo "
        <tr>
          <td>
            <input type='hidden' name='condition[$i]' value='$condition' />
            <input type='checkbox' name='delete[$i]' value='$isbn' />
          </td>
        </tr>
      ";
      $i++;
    }
    

    …and then, when the form is submitted, we can do this:

    // We still base our code on $_POST['delete'] - because this is the array that
    // depends on the user input. This time, though, we'll look at the keys as well
    foreach ($_POST['delete'] as $key => $isbn) {
      $condition = $_POST['condition'][$key];
      // Do stuff
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to submit a specific form programatically, but I allways get the initial
I'm trying to submit a form entry with an uploaded file, but I can't
I'm trying to submit a form on an .asp page but Mechanize does not
Imagine an HTML form with multiple submit buttons, Im trying to write javascript function
I am trying to get the values of multiple form inputs, but the problem
Trying to work with multiple submit buttons within a single form in struts2 application
I'm stuck: I'm trying to submit a form using AJAX, but I can't find
I have a page with several forms. I am trying to submit one of
I have a form on an HTML page with multiple submit buttons that perform
I am trying to submit this multi select as an array but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.