?
Solved

checkbox in php

Posted on 2014-12-17
8
Medium Priority
?
35 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 672 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 664 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 664 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 111

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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

764 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