Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Checkboxes for web application

Explorer ,
Oct 30, 2006 Oct 30, 2006
Hi.
I'm Using:: Dreamweaver8 - PHP5.1.6 - MySQL5 - Apache2.2.3
I'm also New to these softwares.
Hope you'll guide me to it.


-------------------------------------------------------------------------------------------
Rough Image:

Several Checkboxes (E.g. Electrical, Mechanical, Chemical...)

Function - Add or Edit

Naming -
E.g. Electrical -- Name: JobCategory | Checked Value: Electrical
Mechanical -- Name: JobCategory | Checked Value: Mechanical

Situation/Need --
When the user checks both 'Electrical' and 'Mechanical', the database should contain something like "Electrical, Mechanical" or "Electrical & Mechanical" or...

Column Name in Database: JobCategory
-------------------------------------------------------------------------------------------


My code now only allows me to have one value in the 'JobCategory' column although it uses checkboxes.
The code was something like:

$insertSQL = sprintf("INSERT INTO candidate (..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., JobCategory, ..., ..., ..., ..., ..., ..., ..., ..., ...) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
.
.
.
GetSQLValueString($_POST[JobCategory], "text"), //JobCategory is the Name of all the checkboxes
.
.
.


Thanks babe and dudes~
My appreciation
__SY__
TOPICS
Server side applications
488
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Nov 01, 2006 Nov 01, 2006
David Powers :
Thanks alot.
It works well now.
I tested all checkboxes and all could be edited into the database.



In MySQL database (using phpMyAdmin) :
Set the columndata type in phpMyAdmin to SET. In the
Length/Values field, enter the as a comma-delimited
list: 'a','b','c', etc.


Set the name of each check box to
name="ArrayName[]".
The pair of square brackets turns it into an array.


At the very top of the page, add this code :

<?php
if (isset($_POST['JobCategory'])
...
Translate
LEGEND ,
Oct 31, 2006 Oct 31, 2006
SY_angel wrote:
> When the user checks both 'Electrical' and 'Mechanical', the database should
> contain something like "Electrical, Mechanical" or "Electrical & Mechanical"
> or...
>
> Column Name in Database: JobCategory

Set the JobCategory column data type in phpMyAdmin to SET. In the
Length/Values field, enter the permitted categories as a comma-delimited
list: 'Electrical','Mechanical','Administrative', etc.

In your record insert page, set the name of each check box to
name="JobCategory[]". The pair of square brackets turns it into an array.

At the very top of the page, add this code:

<?php
if (isset($_POST['JobCategory'])) {
$_POST['JobCategory'] = implode(',', $_POST['JobCategory']);
}
else {
$_POST['JobCategory'] = '';
}
?>

--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 31, 2006 Oct 31, 2006
Wouldn't it just be easier to name each checkbox a different name e.g. name="electrical", name="mechanical" then use a for -else clause in the validation routine to select one or both values?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 31, 2006 Oct 31, 2006
WebXperience :
Maybe i just want it to store to only a column in a database.

David Powers :
In phpMyAdmin I've edit the column to 'SET' with the values like 'Electrical','Mechanical',...
The name of the checkboxes is changed to name="JobCategory[]"



I also added the code:

if (isset($_POST['JobCategory'])) {
$_POST['JobCategory'] = implode(',', $_POST['JobCategory']);
}
else {
$_POST['JobCategory'] = '';
}



It works! Thanks .
I tried it on the 'Edit' Function I have and it actually edit and the Category appear as -- Admin,Accounts
That's what i wanted! 🙂
But another problem I had was when I reach the 'Edit' page, the checkboxes were all not ticked.
E.g. Person A belongs to the Category of 'Admin,Accounts'
The checkbox of Admin and Accounts in the Edit page was not checked.



My code for the checkboxes (one example):
<input name="JobCategory[]" type="checkbox" value="Electrical" <?php if (!(strcmp($row_rsEditCandidate['JobCategory'],"Electrical"))) {echo "CHECKED";} ?>/>
<span class="style19">Electrical</span>

Thanks babesand dudes~
My appreciation
__SY__
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 01, 2006 Nov 01, 2006
SY_angel wrote:
> But another problem I had was when I reach the 'Edit' page, the checkboxes
> were all not ticked.
> E.g. Person A belongs to the Category of 'Admin,Accounts'
> The checkbox of Admin and Accounts in the Edit page was not checked.

You need to do a little hand-coding to deal with this situation.

Immediately before the first checkbox, add this line of code to extract
the values in the field into an array called $categories:

<?php $categories = explode(',',$row_rsEditCandidate['JobCategory']); ?>

Then change the checkbox code like this:

<input name="JobCategory[]" type="checkbox" value="Electrical" <?php
if (is_array($categories) && in_array("Electrical", $categories))
{echo 'checked="checked"';} ?> />

The conditional statement first checks that $categories is an array.
This prevents an error if $row_rsEditCandidate['JobCategory'] was empty.
The code then looks for "Electrical" in the $categories array. Even if
there's only one item in $row_rsEditCandidate['JobCategory'],
$categories will still be an array. Just replace "Electrical" with the
appropriate value for each checkbox.

I have also corrected echo "CHECKED". It looks as though you're using a
version of Dreamweaver that doesn't use the correct syntax for XHTML.

--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 01, 2006 Nov 01, 2006
David Powers :
Thanks alot.
It works well now.
I tested all checkboxes and all could be edited into the database.



In MySQL database (using phpMyAdmin) :
Set the columndata type in phpMyAdmin to SET. In the
Length/Values field, enter the as a comma-delimited
list: 'a','b','c', etc.


Set the name of each check box to
name="ArrayName[]".
The pair of square brackets turns it into an array.


At the very top of the page, add this code :

<?php
if (isset($_POST['JobCategory'])) {
$_POST['JobCategory'] = implode(',', $_POST['JobCategory']);
}
else {
$_POST['JobCategory'] = '';
}
?>



Before the first checkbox , add this line of code to extract
the values in the field into an array called $categories :

<?php $categories = explode(',',$row_rsEditCandidate['JobCategory']); ?>



Then change the checkbox code like this:

<input name="JobCategory[]" type="checkbox" value="Electrical" <?php
if (is_array($categories) && in_array("Electrical", $categories))
{echo 'checked="checked"';} ?> />



The conditional statement first checks that $categories is an array.
This prevents an error if $row_rsEditCandidate['JobCategory'] was empty.
The code then looks for "Electrical" in the $categories array. Even if
there's only one item in $row_rsEditCandidate['JobCategory'],
$categories will still be an array. Just replace "Electrical" with the
appropriate value for each checkbox.


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 02, 2006 Nov 02, 2006
SY_angel wrote:
> It works well now.
> I tested all checkboxes and all could be edited into the database.

Glad it was sorted out so quickly.

--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 01, 2006 Nov 01, 2006
Anyway those code are just generated by my Dreamweaver. I have no idea on that because I don't have any knowledge to it and couldn't realize it.

Thanks dude!
You Rockx~~

quote:

David Powers:
I have also corrected echo "CHECKED". It looks as though you're using a
version of Dreamweaver that doesn't use the correct syntax for XHTML.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 11, 2007 Jan 11, 2007
LATEST
Hi I know this question has been answered, but could somebody please tell me how you validate the checkboxes on the insert page and when the page is refreshed on submit with errors that it will show the users checked boxes rather than having to repeat all over again.
Very much appreciated on this one.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines