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

  • SEARCH
  • Home
  • 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 7650195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:11:23+00:00 2026-05-31T11:11:23+00:00

I’m new to PHP and having a problem reading checkboxes submitted by a form.

  • 0

I’m new to PHP and having a problem reading checkboxes submitted by a form. Before explaining it i would like to mention that i’m trying to edit a much larger application and the data cannot be sent in any other way. It’s just a matter of finding a good method of reading what is being sent.

When a normal text input is sent, the form will post the following:

custom[0][type]="text"
custom[0][name]="VariableName"
custom[0][value]="VariableName"

Basically there is a main “custom” multidimensional array that has several elements (0,1,2,3 etc) and each element has name and value.

However, when one of the elements is a checkbox, the following stuff gets posted:

custom[1][type]="list"
custom[1][name]="SelectedOptions"
custom[1][value]="Value1"
custom[1][value]="Value3"
custom[1][value]="Value5"

Getting to the PHP side of things, this is the code i’m using to read the data sent by the form. The code below works ok in scenario 1 (with text based inputs) but only reads one value when we have list type custom data.

foreach($_POST['custom'] as $item){
    if($item['value'] != "") echo $item['name'].'='.$item['value']
}

The problem is that $item[‘value’] only reads one of the values, not all 3. How can i get all 3 values in a variable ? It probably is a very easy thing…

To put it all combined, this is what it is sent with POST (3 checkboxes are checked for Variable2)

custom[0][name] Variable1
custom[0][type] text
custom[0][value]    ValueForVariable1
custom[1][name] Variable2
custom[1][type] checkbox
custom[1][value]    Value1
custom[1][value]    Value3
custom[1][value]    Value5

And this is what print_r($_POST) shows for the posted data above

[custom] => Array
(
[0] => Array
    (
    [value] => ValueForVariable1
    [name] => Variable1
    [type] => text
    )
[1] => Array
    (
    [value] => Value1
    [name] => Variable2
    [type] => checkbox
    )

Just to be sure we’re all on the same page, the actual data is generated by a more complex system and we can’t really change that. I’m interested in seeing how we can read all 3 values for Variable2 that are sent in the POST.

Thanks !

  • 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-31T11:11:24+00:00Added an answer on May 31, 2026 at 11:11 am

    EDIT:
    With the extra information that you’ve since provided, I see that I originally misunderstood the problem.

    Since you have no control over over the form that sends the data to your PHP script, and thus can’t change it to ensure that the later checkboxes with the same name do not overwrite the earlier ones, you’ll have to get access to and process the raw post data itself.

    $postdata = file_get_contents("php://input");
    echo $postdata;
    

    … will output the postdata just like a GET querystring: blah=1&blah=2&blah=3 (blah indicates 3 form fields with the same name blah, the first two of which would be overwritten in $_POST leaving $_POST['blah'] = 3).
    with a little exploding on & and looping and parsing looking for the variables in question, or even any conflicting variables, will get you where you’re trying to go.


    Original Answer:

    HTML forms only submit checkboxes (or radio buttons) that have been checked. If they haven’t been checked, the browser won’t send the data back to the server.

    The main way to solve this is to know what you’re looking for on the back end and test for it (i.e. if (isset($_POST['checkboxname'])).

    If you truly need a general purpose backend that will dynamically incorporate all checkbox elements, checked or not, the way I’ve solved this in the past is to use javascript to record all form elements on the page and submit that info with the rest of the form (and, in my case, also submit whether that field was changed or not).


    Here’s a function that you can run at the top of the script to treat POST the way you expect it to be treated from your ASP background:

    function post_process() {
        $rawpostdata = file_get_contents('php://input');
        if (!$rawpostdata) return;
        $fields = explode('&', $rawpostdata);
        $post = array();
        foreach ($fields as $field) {
            list($key, $val) = explode('=', $field);
            if (isset($post[$key])) $post[$key] .= ','.$val;
            else $post[$key] = $val;
        }
        $_POST = $post;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.