How to get checkbox value and compare with table row value
Moderator: General Moderators
How to get checkbox value and compare with table row value
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.
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.
- 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
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
Re: How to get checkbox value and compare with table row val
it will insert into my table, but sadly, none of the value is retrieved. therefore my table show all blank
- 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
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).kwh01 wrote:it will insert into my table, but sadly, none of the value is retrieved. therefore my table show all blank
“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
Re: How to get checkbox value and compare with table row val
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.
- 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
No that code also looks ok; i was thinking that maybe the values where missing from the value attribute, i.e value="".
Is there where you want to insert the value of the checkbox into the database?
Code: Select all
<?php
$sql="INSERT INTO $tbl_name(id,studentID,subjectname,subjectnumber)VALUES('','$_POST[studentID]','$_POST[subjectname]','$_POST[subjectnumber]')";
?>“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
Re: How to get checkbox value and compare with table row val
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.
- 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
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
Re: How to get checkbox value and compare with table row val
ya, it failed to entered, i have no idea why. took me almost 1 days to look at it and cannot figure it out whysocial_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?
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 >,<
- 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
Code: Select all
<?php
$sql="INSERT INTO $tbl_name(id,studentID,subjectname,subjectnumber)VALUES('','$_POST[studentID]','$subjectname','$subjectnumber')";
?>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
Re: How to get checkbox value and compare with table row val
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?
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.
- 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
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
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.
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);
}
?>“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
Re: How to get checkbox value and compare with table row val
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: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
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();
}
?>- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Re: How to get checkbox value and compare with table row val
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.
- 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
How many subjects did you select?kwh01 wrote:it inserted 2 rows at once. hmm. cannot figured it out why
“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