Solved

checkbox in php

Posted on 2014-12-17
8
34 Views
Last Modified: 2016-05-26
checkbox in php
0
Comment
Question by:surender singh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 168 total points
ID: 40504846
Checkbox is the same in php as it is in html

<input type="checkbox" name="" value="">
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 166 total points
ID: 40504889
Checkboxes in php are submitted only if checked. So if you have a form with one checkbox and when the user submit the form the checkbox is unchecked, it wont appear in the array $_POST (or $_GET).
If the problem is to pass several checkboxes which have the same function within your form, then you have to call them uing array notation. Suppose you have a table and you want the user be able to select which records he wants to edit, you have to call each checkbox, for instance, 'recordToEd[]' and give it a value which holds the record id:

while ($row = mysqli->_fetch_assoc($result))
{
  echo  "<tr><td><input type='checkbox' name='recordToEdit[]' value='" . $row['id'] . "' /> </td>...other columns here ... </tr>";
}

Open in new window


When you submit the form you can get the checkboxes this way:
if (isset($_POST['recordToEdit']) && is_array($_POST['recordToEdit']))
{
  foreach ($_POST['recordToEdit'] as $record)
  {
    $recordsToEdit = $record;
  }
}

Open in new window


In the recordsToEdit array you'll get all the id of the record the user want to edit.

Hope this is what you need to know (and to have not forget something...)

Cheers
Marco
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 40505994
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 43

Expert Comment

by:Rob
ID: 41607271
I've requested that this question be closed as follows:

Accepted answer: 250 points for GaryC123's comment #a40504846
Assisted answer: 250 points for marqusG's comment #a40504889

for the following reason:

No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Split:
-- Gary (http:#a40504846)
-- Marco Gasi (http:#a40504889)


If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

tagit
Experts-Exchange Cleanup Volunteer
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41607272
If anybody still cares about the quality of the answers in the PAQ, E-E hould probably have a PHP subject-matter expert look at these.  #40504846 doesn't say anything useful.  #40504889 gives a code example with a logic error that will overwrite a variable multiple times processing only the last element of the array.  The cited article shows the correct answer, with tested-and-working code examples.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41608168
I agree with Ray: my code should have been
if (isset($_POST['recordToEdit']) && is_array($_POST['recordToEdit']))
{
  foreach ($_POST['recordToEdit'] as $record)
  {
    $recordsToEdit[] = $record;
  }
}

Open in new window

1
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41608740
My recommendation is to awrd point to the comment #a40505994 for the reasons listed in comment #a41607272
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question