• 0

[PHP] Post form checkboxes and text


Go to solution Solved by ZakO,

Question

xScence

I have this html form:

 
<form action="" method="POST">
0<input type="text" name="name-txt[]" /><input type="checkbox" name="chbox[]"/><br/>
1<input type="text" name="name-txt[]" /><input type="checkbox" name="chbox[]"/><br/>
2<input type="text" name="name-txt[]" /><input type="checkbox" name="chbox[]"/><br/>
3<input type="text" name="name-txt[]" /><input type="checkbox" name="chbox[]"/><br/>
4<input type="text" name="name-txt[]" /><input type="checkbox" name="chbox[]"/><br/>
5<input type="text" name="name-txt[]" /><input type="checkbox" name="chbox[]"/><br/>
6<input type="text" name="name-txt[]" /><input type="checkbox" name="chbox[]"/><br/>
<input type="submit" name="submit" value="submit"/>
</form>

PHP CODE:

<?php
if(isset($_POST['submit'])){
$txt = $_POST['name-txt'];
$chk = $_POST['chbox'];


print_r($chk);

}

?>

i want to list all check boxes checked and unchecked like this
if i checkbox 1
will returns me 
array(
      [0] => on
)

but when i checkbox 5 will return me the same array key[0] => on so how can i do it like this (like text fields):
if i checkbox 2,3,6

array(
      [0] => 
      [1] => 
      [2] => on
      [3] => on
      [4] =>
      [5] =>
      [6] => on
)

thanks alot.

Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0
ZakO

Checkboxes that are unchecked never get sent to the server, a common (albeit ugly) way to workaround this is to add an additional hidden control with the same name containing a default value, if the checkbox is ticked, that value will be overwritten, i.e:
 

<form action="" method="POST">
0<input type="text" name="name-txt[]" /><input type="hidden" name="chbox[0]" value="" /><input type="checkbox" name="chbox[0]" /><br/>
1<input type="text" name="name-txt[]" /><input type="hidden" name="chbox[1]" value="" /><input type="checkbox" name="chbox[1]"/><br/>
2<input type="text" name="name-txt[]" /><input type="hidden" name="chbox[2]" value="" /><input type="checkbox" name="chbox[2]"/><br/>
3<input type="text" name="name-txt[]" /><input type="hidden" name="chbox[3]" value="" /><input type="checkbox" name="chbox[3]"/><br/>
4<input type="text" name="name-txt[]" /><input type="hidden" name="chbox[4]" value="" /><input type="checkbox" name="chbox[4]"/><br/>
5<input type="text" name="name-txt[]" /><input type="hidden" name="chbox[5]" value="" /><input type="checkbox" name="chbox[5]"/><br/>
6<input type="text" name="name-txt[]" /><input type="hidden" name="chbox[6]" value="" /><input type="checkbox" name="chbox[6]"/><br/>
<input type="submit" name="submit" value="submit"/>
</form>

Then print_r($_POST['chbox']) will give, for example:

Array
(
  [0] =>
  [1] =>
  [2] =>
  [3] =>
  [4] => on
  [5] => on
  [6] =>
)

When doing this you need to add explicit indices as I've done in the example above, you can't just use chbox[]. 

 

There are various other ways to approach this depending on your use-case though, for example, give each checkbox a unique known value, and just check if (in_array(id, $_POST['chbox'])) to determine if it was checked.

Link to post
Share on other sites
  • 0
TheGhostWalker

You could try this:

foreach($_POST['chbox'] as $boxes){
    echo $boxes . ' ';
}

it'll loop through all checkboxes and show their value (0 or 1 I think)

Link to post
Share on other sites
  • 0
xScence

You could try this:

foreach($_POST['chbox'] as $boxes){
    echo $boxes . ' ';
}

it'll loop through all checkboxes and show their value (0 or 1 I think)

It's not that i want...

checkboxes.jpg

 

Link to post
Share on other sites
  • 0
xScence

You could try this:

foreach($_POST['chbox'] as $boxes){
    echo $boxes . ' ';
}

it'll loop through all checkboxes and show their value (0 or 1 I think)

It's not that i want...

checkboxes.jpg

 

Link to post
Share on other sites
  • 0
xScence

Sorry for the double post my internet sux, and thanks ZakO

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By News Staff
      Save 73% off the Interactive Learn to Code Bundle - now just $29.99
      by Steven Parker

      Today's highlighted deal comes via our Online Courses section of the Neowin Deals store, where you can save 73% off the Interactive Learn to Code Bundle. Start writing codes and programs from scratch with 9 interactive courses on SQL, JavaScript, PHP, jQuery, BootStrap, and more.



      This deal consists of the following courses:

      An Interactive SQL Tutorial for Beginners: Introduction to SQL
      Getting Started with SQL Doesn't Have to Be Hard An Interactive JavaScript Course for Beginners
      Learn JavaScript Interactively with a One-of-a-Kind JS Online Course Interactive jQuery Tutorial: Learn jQuery Step-by-Step
      Take an Interactive jQuery Training Course for Beginners to Quickly Master jQuery Events, Effects & More Learn PHP Online: PHP Basics Explained in an Interactive & Fun Manner
      An Interactive Tutorial for Beginners Who Want to Master PHP Basics in a Non-Boring Way The Complete Solidity Smart Contract Guide
      Master Solidity Programming Through Hands-On Experience Master Python Fundamentals the Fun Way: An Interactive Python Tutorial
      A Python Training Course for Absolute Beginners Who Wants to Master the Language Without Getting Bored A Responsive Web Design Course: Find Out How to Create a Responsive Website
      Make a Website Mobile-Friendly with a Step by Step, Responsive Web Design Tutorial Bootstrap Tutorial: Learn to Create Dynamic Websites in a Pinch
      Find Out How to Use Bootstrap the Fun Way by Using an Interactive Course An Interactive Java Tutorial: Learn by Practice!
      Learn Java From Scratch by Combining Theory with Hands-On Coding Experience Good to know
      Length of time users can access this course: lifetime Certification of completion included Redemption deadline: redeem your code within 30 days of purchase For a full description, specs, and instructor info, click here.

      Here's the deal:
      This Interactive Learn to Code Bundle normally costs* $112, but you can pick it up for just $29.99 for a limited time - that represents a saving of $82.01 (73%) off.

      >> Get this deal, or learn more about it <<
      See all discounted Online Courses. This is a time-limited offer.
      Get $1 credit for every $25 spent · Give $10, Get $10 · 10% off for first-time buyers.

      Not for you?
      If this offer doesn't interest you, why not check out the following offers:

      The Nintendo Gaming Bundle Giveaway 20% off Ivacy VPN subscription with coupon code IVACY20 NordVPN subscription at up to 68% off for a 2 year plan Private Internet Access VPN subscription at up to 71% off Unlocator VPN or SmartDNS unblock Geoblock with 7-day free trial Disable Sponsored posts · Other recent deals · Preferred partner software

      Disclosure: This is a StackCommerce deal or giveaway in partnership with Neowin; an account at StackCommerce is required to participate in any deals or giveaways. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through our branded deals site, and it all goes toward the running costs.

    • By News Staff
      Save 97% off this Software Developer Certification Bundle - now just $39.99
      by Steven Parker



      Today's highlighted deal comes via our Online Courses section of the Neowin Deals store, where you can save 97% off the Software Developer Certification Bundle. Java, C#, Python, Redux and more! This 41-hour course bundle will help you get started in the world of coding, programming, and app development.

      What's the deal?
      This bundle consists of the following courses:

      C# in 6 Hours: C# for Complete Beginners - Learn by Coding
      Master the Most Popular Game Development Language Xamarin: Build Native Cross Platform Apps with C# Codes
      Use Your C# Skills, Learn to Build Native Mobile Apps with Xamarin for Android, iOS & Windows React JS: Learn React JS from Scratch with Hands-On Projects
      Start Learning from Scratch, Dive in React JS & Become a React Developer Redux with React JS: Learn Redux with Modern React JS
      Master Redux, React Router & Portals React Native: Learn React Native with Hands-On Practices
      Dive Deeper Into React Native & Use It to Build iOS and Android Apps Java Programming: Learn Core Java & Improve Java Skills
      Advance Your Java Skill Set with Hands-On Examples Python: Learn Python with Real Python Hands-On Examples
      Equip Yourself with a Range of Python Programming Skills Mobile App Marketing: Learn App Monetization From Scratch
      Monetize Your App & Create a Profit-Generating App Business HTML for Everyone: Real World Coding in HTML
      Learn Web Development & HTML Essentials from Scratch Good to know
      Length of time users can access this course: lifetime Certification of completion included Redemption deadline: redeem your code within 30 days of purchase For terms, certification and instructor info, click here.

      What's the benefit?
      This Software Developer Certification Bundle normally costs $1,391 but it can be yours for only $39.99, that's a saving of $1,351.01 (97%) off!

      >> Get this deal, or learn more about it <<
      See all Online Courses on offer. This is a time limited deal.
      Get $1 credit for every $25 spent · Give $10, Get $10 · 10% off for first-time buyers.

      Not for you?
      If this offer doesn't interest you, why not check out the following offers:

      The Samsung Galaxy Z Flip 256GB Giveaway 20% off Ivacy VPN subscription with coupon code IVACY20 NordVPN subscription at up to 70% off Private Internet Access VPN subscription at up to 71% off Unlocator VPN or SmartDNS unblock Geoblock with 7-day free trial Disable Sponsored posts · Other recent deals · Preferred partner software

      Disclosure: This is a StackCommerce deal or giveaway in partnership with Neowin; an account at StackCommerce is required to participate in any deals or giveaways. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through our branded deals site, and it all goes toward the running costs.

    • By News Staff
      Save 97% off the 2020 Learn to Code Full Stack Developer Certification Bundle
      by Steven Parker

      Today's highlighted deal comes via our Online Courses section of the Neowin Deals store, where you can save 97% off the 2020 Learn to Code Full Stack Developer Certification Bundle. Become a high-earning web developer with 50+ hours of instruction from top-tier instructors, including Rob Percival and John Elder.



      What's the deal?
      This deal consists of the following courses:

      Microsoft SQL Server & T-SQL Course For Beginners
      Understand the Benefits of SQL & Earn Big Bucks as a Skilled Developer Learn C# by Building Applications
      Understand the Concepts of C#6 &C#7 and Use Them to Build Real World .NET Console Apps Beginner Object-Oriented Programming in C# & .NET Core
      Climb Up the Developer Hierarchy by Mastering Object-Oriented Programming & Its Fundamental Concepts Using MySQL Databases with Python
      Learn MySQL Database with Python the Fast & Easy Way with John Elder HTML Programming for Everyone
      Learn to Build Awesome Websites Using HTML & the Bootstrap CSS Framework Intro to PHP for Web Development
      Learn PHP for Web Development & Become a Backend Coder in No Time The Complete C# Programming Course
      Master C# & .NET Framework with Code Challenges, Exercises, & Real-Life Examples with Rob Percival Advanced Python Training
      Go from Complete Beginner to a Python Language Expert Learn Angular by Creating a Web Application
      Master this Popular Web Dev Framework by Building an Interactive Virtual Cookbook Good to know
      Length of time users can access this course: lifetime Certification of completion included Redemption deadline: redeem your code within 30 days of purchase For a full description, specs, and instructor info, click here.

      What's the benefit?
      This 2020 Learn to Code Full Stack Developer Certification Bundle normally costs* $1,650, but you can pick it up for just $38.99 for a limited time - that represents a saving of $1,611.01 (97%) off.

      >> Get this deal, or learn more about it <<
      See all discounted Online Courses. This is a time-limited offer.

      Save even more!
      Stick with Neowin Deals and earn credit or even deeper discounts.

      Check out our recent deals here or on the Neowin Deals site. For every $25 spent, you get $1 credit added to your Neowin Deals account. Refer the deal for $10 credit via social media or email and if it results in a purchase of at least $10, you'll get $10 credit added to your account. 10% off for first-time buyers: be sure not to dismiss the popup offer to subscribe to email updates on the deals page in order to profit! Not for you?
      If this offer doesn't interest you, why not check out our giveaways on the Neowin Deals website? There's also a bunch of freebies you can check out here

      Miscellany and the fine print!
      In some cases, such as with Online Courses, a store credit refund within 15 days of purchase is possible if you are unhappy with it; this does not apply to all deals, so please do check the terms on the page before making a purchase.

      Check our other recent deals, before they expire, or our preferred partner software.

      How can I disable these posts? Click here.

      Disclosure: This is a StackCommerce deal or giveaway in partnership with Neowin; an account at StackCommerce is required to participate in any deals or giveaways. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through our branded deals site, and it all goes toward the running costs. *Values or percentages mentioned above are subject to StackCommerce's own determination of retail pricing.

    • By News Staff
      Save 98% off this Ultimate Web Coding for Everyone Bundle
      by Steven Parker

      Today's highlighted deal comes via our Online Courses section of the Neowin Deals store, where you can save 98% off* this Ultimate Web Coding for Everyone Bundle. Dive deeper in the world of web coding with 8 courses on Python, Ruby, Javascript, PHP, HTML, Bootstrap 4, MySQL and Node.js



      What's the deal?
      This deal consists of the following courses:

      Python Programming for Everyone
      Learn Basic to Advanced Python 3 Programming in Only an Hour, Python 3 Download Included! Ruby Programming for Everyone
      Get Up to Speed with This Beginner-Friendly Language in Just One Hour JavaScript Programming for Everyone
      Learn to Code Javascript Like a Professional Programmer Intro to PHP for Web Development
      Learn PHP for Web Development & Become a Backend Coder in No Time HTML & Bootstrap CSS Programming for Everyone
      Learn to Build Awesome Websites Using HTML & the Bootstrap CSS Framework CSS Bootstrap 4 for Everyone
      Learn the Latest Version of Bootstrap 4 from Start to Finish & Build 3 Website Projects Intro to MySQL with Node.js: Learn to Use MySQL with Node
      Learn How to Use MySQL Databases with Node.js & Explore What These Databases Can Do Node.js Absolute Beginners Guide: Learn Node from Scratch
      Learn Node.js to Build Websites & Host Them Live on Heroku Webhosting Good to know
      Length of time users can access this course: lifetime Certification of completion included Redemption deadline: redeem your code within 30 days of purchase For a full description, specs, and instructor info, click here.

      What's the benefit?
      This Ultimate Web Coding for Everyone Bundle normally costs* $1,600, but you can pick it up for just $29 for a limited time - that represents a saving of $1,571 (98%) off.

      >> Get this deal, or learn more about it <<
      See all discounted Online Courses. This is a time-limited offer.

      Save even more!
      Stick with Neowin Deals and earn credit or even deeper discounts.

      Check out our recent deals here or on the Neowin Deals site. For every $25 spent, you get $1 credit added to your Neowin Deals account. Refer the deal for $10 credit via social media or email and if it results in a purchase of at least $10, you'll get $10 credit added to your account. 10% off for first-time buyers: be sure not to dismiss the popup offer to subscribe to email updates on the deals page in order to profit! Not for you?
      If this offer doesn't interest you, why not check out our giveaways on the Neowin Deals website? There's also a bunch of freebies you can check out here.

      Miscellany and the fine print!
      In some cases, such as with Online Courses, a store credit refund within 15 days of purchase is possible if you are unhappy with it; this does not apply to all deals, so please do check the terms on the page before making a purchase.

      Check our other recent deals, before they expire, or our preferred partner software for Private Internet Access and NordVPN deals.

      How can I disable these posts? Click here.

      Disclosure: This is a StackCommerce deal or giveaway in partnership with Neowin; an account at StackCommerce is required to participate in any deals or giveaways. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through our branded deals site, and it all goes toward the running costs. *Values or percentages mentioned above are subject to StackCommerce's own determination of retail pricing.

    • By News Staff
      HTML Essentials - free cheat sheet download
      by Steven Parker

      Claim your complimentary free cheat sheet today, before the offer expires.



      What's it about?


      Master HTML elements in no time with this cheat sheet.

      HTML is one of the building blocks of the web. While it's not a programming language, it's one of the easiest languages to learn as a coding newbie.

      But the long list of HTML elements you need to be familiar with can prove to be a stumbling block. Don't worry, though, because this cheat sheet can help you get past that problem.

      The cheat sheet contains concise descriptions of various tags, attributes, objects, and other important HTML elements. You can dip into it anytime for quick recall!

      Download today!

      How to get this free resource:
      Complete and verifiable information is required in order to receive this offer. If you have previously made use of these free offers, you will not need to re-register. While supplies last! Please ensure you read the terms and conditions to claim this offer.

      >> HTML Essentials - Free cheat sheet download <<
      Offered by MakeUseOf, view their other free resources. Limited time offer.

      Not for you?
      That's OK, there are other free eBooks on offer you can check out here, but be aware that these are all time-limited offers. If you are uncomfortable sharing your details with a third-party sponsor, we understand. Or via our preferred partner:

      How can I disable these posts? Click here.

      Disclosure: A valid email address is required to fulfill your request. Complete and verifiable information is required in order to receive this offer. By submitting a request, your information is subject to TradePub.com's Privacy Policy.