How to get checkbox value and compare with table row value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

How to get checkbox value and compare with table row value

Post by kwh01 »

i'm kinda new to php, not much php i know except mostly i get the knowledge by googling.
for my problem now,currently, im calling table student subject
but i want the user to select the checkbox based on the subject number
i cannot figure any solution to get the checkbox value and match with the subject number
any idea ?
it took me few hours to do, but still i cant figure it out.
Last edited by kwh01 on Sat Apr 14, 2012 3:56 am, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to get checkbox value and compare with table row val

Post by social_experiment »

The code used to retrieve the value of the checkbox seems ok; what output do you get?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: How to get checkbox value and compare with table row val

Post by kwh01 »

it will insert into my table, but sadly, none of the value is retrieved. therefore my table show all blank
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to get checkbox value and compare with table row val

Post by social_experiment »

kwh01 wrote:it will insert into my table, but sadly, none of the value is retrieved. therefore my table show all blank
Then it's possible that there is no values written to the html. Paste the HTML source code for one of the check boxes. Before you say "I already pasted the code" i'm refering to the source you view when you right click on a page and select "View source" (or "View page source" depending on your browser).
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: How to get checkbox value and compare with table row val

Post by kwh01 »

looks something wrong in here..hmm..:\ but cant figure it out why :S
Last edited by kwh01 on Sat Apr 14, 2012 3:56 am, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to get checkbox value and compare with table row val

Post by social_experiment »

No that code also looks ok; i was thinking that maybe the values where missing from the value attribute, i.e value="".

Code: Select all

<?php
$sql="INSERT INTO $tbl_name(id,studentID,subjectname,subjectnumber)VALUES('','$_POST[studentID]','$_POST[subjectname]','$_POST[subjectnumber]')";
?>
Is there where you want to insert the value of the checkbox into the database?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: How to get checkbox value and compare with table row val

Post by kwh01 »

sir, ya. is it wrong or lack of something? :|
Last edited by kwh01 on Sat Apr 14, 2012 3:56 am, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to get checkbox value and compare with table row val

Post by social_experiment »

The checkbox name is $_POST['stusubject']; you correctly loop through the array but fail to enter the values into the database. All the data from this form; is it for one student?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: How to get checkbox value and compare with table row val

Post by kwh01 »

social_experiment wrote:The checkbox name is $_POST['stusubject']; you correctly loop through the array but fail to enter the values into the database. All the data from this form; is it for one student?
ya, it failed to entered, i have no idea why. took me almost 1 days to look at it and cannot figure it out why :(
The flow of the system should be admin create studentname, studentID and student password. then i session register the studentID in previous php file
in this php file, i session out the studentID
and display total subject name and number which read based on my database
then, admin may select at least 1 or more subject for ONE student.
hope you know what im trying to say
sorry for poor english >,<
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to get checkbox value and compare with table row val

Post by social_experiment »

Code: Select all

<?php
$sql="INSERT INTO $tbl_name(id,studentID,subjectname,subjectnumber)VALUES('','$_POST[studentID]','$subjectname','$subjectnumber')";
?>
The query will have to change slightly and it has to go inside the foreach loop; if you're not inserting a value into "id" or it's auto-increment you can leave it out of the query

Edit
Where does the subject number come from; the checkbox only has a value of the subject name in this case
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: How to get checkbox value and compare with table row val

Post by kwh01 »

hmm, do you mind show me some example of foreach, im not really understand much on foreach, i just simply google and try some foreach code out.
and about subject number, i think i did not include the value into checkbox.
can checkbox have 2 value for it?
Last edited by kwh01 on Sat Apr 14, 2012 3:57 am, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to get checkbox value and compare with table row val

Post by social_experiment »

Firstly the question about the checkboxes: A check box only has 1 value attribute; you can populate that with data of your choosing

For this snippet i will assume that you don't have a subject number, only the subject name

Code: Select all

<?php
foreach($_POST['stusubject'] as $subjectname) {
      $sql="INSERT INTO $tbl_name(studentID,subjectname)VALUES('$_POST[studentID]','$subjectname')";
      $qry = mysql_query($sql);
}
?>
Each item that is in the $_POST['stusubject'] array (meaning each item checked) will be placed into the database each time the loop occurs. So if you chose 2 subjects, there will be 2 records in the table; each containing a student id value and a subject.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: How to get checkbox value and compare with table row val

Post by Pazuzu156 »

social_experiment wrote:Firstly the question about the checkboxes: A check box only has 1 value attribute; you can populate that with data of your choosing
What he said is true. Whenever I have forms, I use classes to handle all that for me. Maybe this code will help a bit:

Code: Select all

<?php
class Form {
  private $submit;
  private $checkbox;

  public function __construct() {
    $this->submit = (isset($_POST['submit'])) ? 1 : 0;
    $this->checkbox = ($this->submit) ? isset($_POST['checkbox']) : '';
  }

  public function is_box_checked() {
    if($this->checkbox==1) {
      return "Checkbox has been clicked!";
    } else {
      return "Checkbox has not been clicked!";
    }
  }
}
?>

<?php
require_once('class.php');
$check = new Form;
if(isset($_POST['submit'])) {
    echo $check->is_box_checked();
}
?>
Of course this is very basic, and I do not recommend doing it this exact way, but I really hope it gives you some idea of what to work with on this. As far as any value from the checkbox, you will only get a boolean (1 or 0) meaning checked or unchecked just as social_experiment said.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: How to get checkbox value and compare with table row val

Post by kwh01 »

sir, i had tried your way. it works, but problem is. it inserted 2 rows at once. hmm. cannot figured it out why
Last edited by kwh01 on Sat Apr 14, 2012 3:57 am, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to get checkbox value and compare with table row val

Post by social_experiment »

kwh01 wrote:it inserted 2 rows at once. hmm. cannot figured it out why
How many subjects did you select?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply