Page 1 of 2

How to get checkbox value and compare with table row value

Posted: Tue Apr 10, 2012 1:47 am
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.

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

Posted: Tue Apr 10, 2012 3:07 am
by social_experiment
The code used to retrieve the value of the checkbox seems ok; what output do you get?

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

Posted: Tue Apr 10, 2012 6:46 am
by kwh01
it will insert into my table, but sadly, none of the value is retrieved. therefore my table show all blank

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

Posted: Tue Apr 10, 2012 8:01 am
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).

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

Posted: Tue Apr 10, 2012 8:39 am
by kwh01
looks something wrong in here..hmm..:\ but cant figure it out why :S

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

Posted: Tue Apr 10, 2012 8:44 am
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?

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

Posted: Tue Apr 10, 2012 8:50 am
by kwh01
sir, ya. is it wrong or lack of something? :|

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

Posted: Tue Apr 10, 2012 8:59 am
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?

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

Posted: Tue Apr 10, 2012 9:09 am
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 >,<

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

Posted: Tue Apr 10, 2012 9:32 am
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

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

Posted: Tue Apr 10, 2012 9:41 am
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?

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

Posted: Tue Apr 10, 2012 4:48 pm
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.

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

Posted: Tue Apr 10, 2012 6:26 pm
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.

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

Posted: Wed Apr 11, 2012 7:41 am
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

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

Posted: Wed Apr 11, 2012 7:46 am
by social_experiment
kwh01 wrote:it inserted 2 rows at once. hmm. cannot figured it out why
How many subjects did you select?