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 926401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:40:50+00:00 2026-05-15T19:40:50+00:00

I am going nuts here, I have an array of checkboxes from a form

  • 0

I am going nuts here, I have an array of checkboxes from a form that I am trying to $_POST with PHP. EVERYTHING on my form posts fine except the check boxes. The checkboxes DO post, but in the wrong order. For instance when I want checkbox[0] and checkbox[2] I actually get checkbox[0] and checkbox[1].

I have tried many different ways to get the value of the checkbox, including isset but I still have the same problem. I just need the checkbox value of on to be stored in my database if the checkbox is indeed checked.

My code is below. $in_production is the checkbox. I can provide the code that generates the checkbox too if it is needed.

Thanks in advance.

if ($_GET['action'] == 'Edit_Product'){

    include("../dbinfo.php");

    $q_id = $_GET['q_id'];

    for ($i = 0; $i < count($_POST['p_id']); $i++){

        $result = mysql_query('SELECT * FROM products WHERE q_id = '.$q_id);
        $num = mysql_num_rows($result);

        $p_id = ($_POST['p_id'][$i]);
        $in_production = ($_POST['in_production'][$i]);
        $p_name = ($_POST['p_name'][$i]);
        $p_price = ($_POST['p_price'][$i]);

        $p_name_conflict = FALSE;

        for ($ii = 0; $ii < $num; $ii++){
            $row = mysql_fetch_array($result);
            $p_name_conflict_check = $row['p_name'];
            $p_id_conflict_check = $row['p_id'];

            if($p_name_conflict_check == $p_name &&
              $p_id_conflict_check != $p_id){
                $p_name_conflict = TRUE;
            }

        }

        if ($p_name_conflict == FALSE){
            $query = "UPDATE products SET p_name='$p_name',
              p_price='$p_price', in_production='$in_production',
              last_modified=CURDATE() WHERE p_id = '$p_id'";
            mysql_query($query);
        }

        else{
            $update_failures =+1;
        }

    }

    mysql_close($link);

    if($update_failures == 0){
        header("Location: Products_Updated.html");
    }

    elseif ($update_failures != 0){
        header("Location: Products_Exist.php?update_failures=".$update_failures);
    }

}

P.S. I don’t know why but the code block icons are not present on SO right now… so my code is not all pretty. Also, I know my code is horribly inefficient, but I am just trying to get this working right now, then fine tune later. I am open to efficiency suggestions as well, but that is not my primary objective with this question.

EDIT: Here is the form from the HTML…

        <form id="form" name="form" method="post" action="/Management/Products/Product_Management.php?action=Edit_Product&q_id=<?php echo "$q_id" ?>">

                <?php

                    include("../dbinfo.php");   

                    $result = mysql_query('SELECT * FROM products WHERE q_id =' . $q_id);
                    $num = mysql_num_rows($result);
                    mysql_close($link);

                    for ($i = 0; $i < $num; $i++){
                        $row = mysql_fetch_array($result);

                        $p_id = $row['p_id'];
                        $p_name = $row['p_name'];               
                        $p_price = $row['p_price'];
                        $in_production = $row['in_production'];
                        $date_added = $row['date_added'];
                        $last_modified = $row['last_modified'];

                        if($in_production == 'on'){
                            $checked = 'checked';
                        }

                        else{
                            $checked = '';
                        }

                        echo "<div>Product ID# " . $p_id . "<label style=\"font-style:italic\"> (Originally added on " . $date_added . ", last modified on " . $last_modified . ")</label></div><br/>";
                        echo "<input id=\"p_id" . $p_id . "\" class=\"text\" type=\"hidden\" name=\"p_id[]\" value=\"" . $p_id . "\"/>";

                        echo "<label>Product Name *</label><br/>";
                        echo "<div><label style=\"font-style:italic\">(Product still in production <input type=\"checkbox\" name=\"in_production[]\"" . $checked . " style=\"width:15px\"/>)</label></div>";    
                        echo "<input id=\"p_name" . $p_id . "\" class=\"text\" type=\"text\" name=\"p_name[]\" maxlength=\"20\" onfocus=\"on_focus(this)\" onblur=\"on_blur(this)\" value=\"" . $p_name . "\"/><br/><br/>";

                        echo "<label>Product Price *</label><br/>";
                        echo "<div><label style=\"font-style:italic\">(Without taxes)</label></div>";
                        echo "<input id=\"p_price" . $p_id . "\" class=\"text\" type=\"text\" name=\"p_price[]\" maxlength=\"6\" onkeypress=\"return currency(this, event)\" onchange=\"currency_format(this)\" onfocus=\"on_focus(this)\" onblur=\"on_blur(this)\" value=\"" . $p_price . "\"/><br/><br/><br/><br/>";

                    }

                ?>

            <input class="button" type="button" value="Submit" onclick="product_edit_form_check()"/><br/><br/>

        </form>
  • 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-05-15T19:40:50+00:00Added an answer on May 15, 2026 at 7:40 pm

    It would be helpful if you could post some of the HTML-part so we could see how you create your form. It seems you’re generating your checkboxes without indexes in your array, so all checkboxes have the name/id “checkbox[]”, which is ok if you don’t care about the index, but if posted, the array will be numbered starting from “0” and then counting up which is the reason why you’ll get “0” and “1” posted, even if “0” and “2” were checked.

    Try to give your checkboxes’ name/id numbers when generating the HTML, like “checkbox[0]”, “checkbox[1]”, “checkbox[2]”, and so on. So when checkbox 0 and 2 are checked, you should get those values (including the correct index) posted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm going nuts here, trying to get my system configured. I have a laptop
While going through university and from following the development of SO, I've heard a
What's going on here? printf.sh: #! /bin/sh NAME=George W. Bush printf Hello, %s\n $NAME
I'm going to start a new project - rewriting an existing system (PHP +
Going back to my previous question on OCSP, does anybody know of reliable OCSP
Been going over my predecessor's code and see usage of the request scope frequently.
After going through the Appendix A, C# Coding Style Conventions of the great book
I am going to be using C/C++, and would like to know the best
I'm going to build an API for a web app and I'm interested in
Is my best be going to be a shell script which replaces symlinks with

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.