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

How to get checkbox value and compare with table row value

Postby kwh01 » Tue Apr 10, 2012 1:47 am

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.
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

Postby social_experiment » Tue Apr 10, 2012 3:07 am

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
User avatar
social_experiment
DevNet Master
 
Posts: 2775
Joined: Sun Feb 15, 2009 12:08 pm
Location: .za

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

Postby kwh01 » Tue Apr 10, 2012 6:46 am

it will insert into my table, but sadly, none of the value is retrieved. therefore my table show all blank
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

Postby social_experiment » Tue Apr 10, 2012 8:01 am

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
User avatar
social_experiment
DevNet Master
 
Posts: 2775
Joined: Sun Feb 15, 2009 12:08 pm
Location: .za

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

Postby kwh01 » Tue Apr 10, 2012 8:39 am

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.
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

Postby social_experiment » Tue Apr 10, 2012 8:44 am

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

Syntax: [ Download ] [ Hide ]
<?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
User avatar
social_experiment
DevNet Master
 
Posts: 2775
Joined: Sun Feb 15, 2009 12:08 pm
Location: .za

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

Postby kwh01 » Tue Apr 10, 2012 8:50 am

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.
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

Postby social_experiment » Tue Apr 10, 2012 8:59 am

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
User avatar
social_experiment
DevNet Master
 
Posts: 2775
Joined: Sun Feb 15, 2009 12:08 pm
Location: .za

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

Postby kwh01 » Tue Apr 10, 2012 9:09 am

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 >,<
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

Postby social_experiment » Tue Apr 10, 2012 9:32 am

Syntax: [ Download ] [ Hide ]
<?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
User avatar
social_experiment
DevNet Master
 
Posts: 2775
Joined: Sun Feb 15, 2009 12:08 pm
Location: .za

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

Postby kwh01 » Tue Apr 10, 2012 9:41 am

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.
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

Postby social_experiment » Tue Apr 10, 2012 4:48 pm

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
Syntax: [ Download ] [ Hide ]
<?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
social_experiment
DevNet Master
 
Posts: 2775
Joined: Sun Feb 15, 2009 12:08 pm
Location: .za

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

Postby Pazuzu156 » Tue Apr 10, 2012 6:26 pm

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:

Syntax: [ Download ] [ Hide ]
<?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.
User avatar
Pazuzu156
Forum Contributor
 
Posts: 209
Joined: Sat Nov 20, 2010 10:00 pm

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

Postby kwh01 » Wed Apr 11, 2012 7:41 am

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.
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

Postby social_experiment » Wed Apr 11, 2012 7:46 am

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
User avatar
social_experiment
DevNet Master
 
Posts: 2775
Joined: Sun Feb 15, 2009 12:08 pm
Location: .za

Next

Return to PHP - Code

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 10 guests