PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Nov 23, 2014 2:09 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Tue Apr 10, 2012 1:47 am 
Offline
Forum Newbie

Joined: Mon Apr 09, 2012 10:08 pm
Posts: 20
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.

Top
 Profile  
 
PostPosted: Tue Apr 10, 2012 3:07 am 
Offline
DevNet Master
User avatar

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


Top
 Profile  
 
PostPosted: Tue Apr 10, 2012 6:46 am 
Offline
Forum Newbie

Joined: Mon Apr 09, 2012 10:08 pm
Posts: 20
it will insert into my table, but sadly, none of the value is retrieved. therefore my table show all blank


Top
 Profile  
 
PostPosted: Tue Apr 10, 2012 8:01 am 
Offline
DevNet Master
User avatar

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


Top
 Profile  
 
PostPosted: Tue Apr 10, 2012 8:39 am 
Offline
Forum Newbie

Joined: Mon Apr 09, 2012 10:08 pm
Posts: 20
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.

Top
 Profile  
 
PostPosted: Tue Apr 10, 2012 8:44 am 
Offline
DevNet Master
User avatar

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


Top
 Profile  
 
PostPosted: Tue Apr 10, 2012 8:50 am 
Offline
Forum Newbie

Joined: Mon Apr 09, 2012 10:08 pm
Posts: 20
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.

Top
 Profile  
 
PostPosted: Tue Apr 10, 2012 8:59 am 
Offline
DevNet Master
User avatar

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


Top
 Profile  
 
PostPosted: Tue Apr 10, 2012 9:09 am 
Offline
Forum Newbie

Joined: Mon Apr 09, 2012 10:08 pm
Posts: 20
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 >,<


Top
 Profile  
 
PostPosted: Tue Apr 10, 2012 9:32 am 
Offline
DevNet Master
User avatar

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


Top
 Profile  
 
PostPosted: Tue Apr 10, 2012 9:41 am 
Offline
Forum Newbie

Joined: Mon Apr 09, 2012 10:08 pm
Posts: 20
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.

Top
 Profile  
 
PostPosted: Tue Apr 10, 2012 4:48 pm 
Offline
DevNet Master
User avatar

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


Top
 Profile  
 
PostPosted: Tue Apr 10, 2012 6:26 pm 
Offline
Forum Contributor
User avatar

Joined: Sat Nov 20, 2010 10:00 pm
Posts: 209
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.


Top
 Profile  
 
PostPosted: Wed Apr 11, 2012 7:41 am 
Offline
Forum Newbie

Joined: Mon Apr 09, 2012 10:08 pm
Posts: 20
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.

Top
 Profile  
 
PostPosted: Wed Apr 11, 2012 7:46 am 
Offline
DevNet Master
User avatar

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group