PDA

View Full Version : does anybody know how to make checkboxes stick (php) when error checking?


nanny
01-11-2007, 03:38 AM
Hi I can validate my forms except for multiple checkboxes and multiple lists/menus
I can get the other parts of my form to be sticky when error checking but does anybody know how for these?
I have no trouble by inserting the record with implode.....and editing with explode but that is as far as it goes.
I really need these to be validated using php.
Thanks for your time.

davidj
01-11-2007, 05:44 AM
im sorry

im not sure what you want

your wanting to validate check boxes with php

nanny
01-11-2007, 09:02 AM
Yes that is correct. I have in the form:
Enter Name:text
Enter Business:text
Enter Industry: 8 checkboxes under name category[]
Enter Category: 10 checkboxes under name sub_category[]
Enter Description: text
Enter Location: menu/list
Enter Email: text
Enter Website: text
Enter Address: text
Enter Phone: text

I can validate all except the checkboxes and menu/list.
I have used the implode and explode for checkboxes and that works fine in giving me the output in a comma delimited form.
I have trouble with if the user has selected many things including the checkbox that when the page refreshes on error checking all elements within the form are shown again that the user submitted except for the checkboxs and menu/list.

Hope that makes better sense. I know it is an array but how to get the form to be sticky on submit with error is something I can't do......
Thanks for your time...........

davidj
01-11-2007, 09:10 AM
so you want the checkboxes to display whats been checked after the validation has been run. So when the page refreshes the checkbox's are still checked

nanny
01-11-2007, 09:15 AM
Yes please............I have been trying a lot of ways but can't get the checkboxes or the menu/list selection to show again.
Thanks

davidj
01-11-2007, 09:39 AM
if you name all the checkboxes like this

checkbox[]

this will assign values to the checkbox[] array and will numericaly assign a key in the array..

$checkbox[0];
$checkbox[1];
$checkbox[2];
etc

so only one $_POST[] is required to catch the values from all the checkboxes because this would be an Array which contains all

after you have validated the form fields add this php function in the checkbox tag.

NOTE: make sure that you target the right checkbox with the right array variable
<?php if(!(strcmp($checkbox[0], 3))){echo "checked";}?>

lets explain..

its an IF conditional statement and its using strcmp (string compare). There are 2 values im comparing against and they are the checkbox value and the $variable. If both match then echo a string "CHECKED" which happens to tick a checkbox if is present within the tag!

Ta-da. Jobs a goodun

<input name="checkbox[]" type="checkbox" id="checkbox[]" value="1" <?php if(!(strcmp($checkbox[0], 1))){echo "checked";}?>/>
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="2" <?php if(!(strcmp($checkbox[1], 2))){echo "checked";}?>/>
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="3" <?php if(!(strcmp($checkbox[2], 3))){echo "checked";}?>/>

nanny
01-11-2007, 10:47 PM
Sorry David I tried that I still don't get the checkbox staying.
I will show you my validation so far and what I have done:
At the top of the page I have:
<?php
if (isset($_POST['industry'])) {
$_POST[industry] = implode("\n", $_POST['industry']);
}
else {
$_POST['industry'] = '';
}
?>
<?php
// Validate form input
$MM_flag="MM_insert";
if (isset($_POST[$MM_flag])) {

$title = trim($_POST['Title']);
if (empty($title)) {
$error['Title'] = 'Please enter a Title for searching';
}
$bname = trim($_POST['businessname']);
if (empty($bname)) {
$error['businessname'] = 'Please enter your Business Name';
}
$desc = trim($_POST['Description']);
if (empty($desc)) {
$error['Description'] = 'Please enter your Business Description';
}
$industry = $_POST['industry'];
if (empty($industry)) {
$error['industry'] = 'Please select a checkbox for your industry';
}
?>
On my edit page which show all the checkboxes that have been selected:
I have this placed just before the list of checkboxes:
<?php $industry = explode(',',$row_rslistedit['industry']); ?>
And with each checkbox:
<input name="industry[]" type="checkbox" value="Beauty"<?php
if (is_array($industry) && in_array("Beauty", $industry))
{echo 'checked="checked"';} ?> />
Beauty
the list keeps going as say:
<input name="industry[]" type="checkbox" value="Day Spa"<?php
if (is_array($industry) && in_array("Day Spa", $industry))
{echo 'checked="checked"';} ?> />
Day Spa
Do you think it is in the validation code for $_POST['industry'] where it won't work. I tried placing the same in the insert page as I have the edit page to see if that works but it doesn't.
I have the mysql table as industry VARCHAR(255) so it is in Dreamweaver as a text field.
I really need help with this one.
Thanks for your help, I really appreciate your time.

Sorry forgot to add I have this just in front of the insert record:
if (!isset($error)) {

and at the end of the insert:
$_POST = array();
}

Thanks again.

nanny
01-11-2007, 11:00 PM
Thanks for the edit couldn't find anywhere to place the code in the Go Advanced nothing came up so I did the Post Quick Reply
Thanks

davidj
01-11-2007, 11:09 PM
note the reason i gave!

nanny
01-11-2007, 11:14 PM
That's cool.

davidj
01-11-2007, 11:19 PM
its 00:15 here in the uk

will resolve this in the morning

davidj
01-12-2007, 06:12 AM
<input name="industry[]" type="checkbox" value="Beauty"<?php
if (is_array($industry) && in_array("Beauty", $industry))
{echo 'checked="checked"';} ?> />Beauty



should be

<input name="industry[]" type="checkbox" id="industry[]" value="1" <?php if(!(strcmp($industry[0], 1))){echo "checked";}?>/>Beauty

make sure that $industry[0] is the correct one and contains the right value for that checkbox

nanny
01-13-2007, 12:54 AM
Sorry David I get a parse error when I do the next line and I still don't get the checkboxes to stay once the page refreshes on error.
Where you have value="1" etc. wouldn't work I place the value as it is e.g. Beauty etc.
By the way I have no way of placing code in this system - how the hell do you do it??? I am using the quick reply which is no different to Go Advanced except for smilies.......wow .........no place for making my code look neat..

Sorry I am a noobie to forums.........
Thanks for your help.....very appreciated.

davidj
01-13-2007, 09:23 AM
i need to see how you wrote your code so i can check it

nanny
01-23-2007, 02:07 AM
I gave away the checkboxes and now trying a dynamic multiple list. Easier to code than all those checkboxes.
I can get it to higlight the selection on the insert page and implode to give a comma delimited list on the results page to edit.
But when on the edit page it won't show any of those that are meant to be highlighted. It is still a dynamic multiple list.
When using the location dropdown list and it isn't multiple it shows the selection but that still isn't showing higlighted.
Is there any trick to do this.
The checkboxes showed up on the edit page but not on the insert page.
Otherwise I thought maybe using the checkboxes and if I can find a javascript function for this maybe that would be the best effort.
What do you think?

davidj
01-23-2007, 05:47 AM
the secret to success does not mean cut and paste javascript or to give up on your original idea because its "too hard"

you need to post the entire code for all pages with the checkboxes and i will write this for you

nanny
01-24-2007, 11:15 PM
Your are very sweet David, thank you for your help.
I will show you part of the insert record after the code for the Dreamweaver where this applies, I have no trouble with using php validation if it is empty but that is all.
Might be best to start from scratch and try to understand how to do this for all my forms...

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" onsubmit="return validate(this);">
<table align="center">
<tr valign="baseline">
<td colspan="4" align="right" nowrap><div align="left"><span class="title"><strong>Select an Industry </strong></span></div></td>
<tr valign="baseline">
<td width="115" align="right" valign="middle">Industry::</td>
<td colspan="3"><p>
<select name="class_category" size="5" id="class_category">
<?php
do {
?>
<option value="<?php echo $row_indust['class_category']?>"><?php echo $row_indust['class_category']?></option>
<?php
} while ($row_indust = mysql_fetch_assoc($indust));
$rows = mysql_num_rows($indust);
if($rows > 0) {
mysql_data_seek($indust, 0);
$row_indust = mysql_fetch_assoc($indust);
}
?>
<?php if (isset($_POST['class_category'])) {
echo htmlentities($_POST['class_category']);} ?></select>
</p>
</td>
<tr valign="baseline">
<td colspan="4" align="right" nowrap><div align="left"><span class="title"><strong>Select a Category </strong></span></div></td>
<tr valign="baseline">
<td align="right" valign="middle">Category::</td>
<td colspan="3"><select name="categorytype" size="5" id="categorytype">
<?php
do {
?>
<option value="<?php echo $row_cattype['categorytype']?>"><?php echo $row_cattype['categorytype']?></option>
<?php
} while ($row_cattype = mysql_fetch_assoc($cattype));
$rows = mysql_num_rows($cattype);
if($rows > 0) {
mysql_data_seek($cattype, 0);
$row_cattype = mysql_fetch_assoc($cattype);
}
?>
<?php if (isset($_POST['categorytype'])) {
echo htmlentities($_POST['categorytype']);} ?></select></td>
</tr>
<tr valign="baseline">
<td colspan="4" align="right" nowrap><div align="left"><span class="title"><strong>Please give your Business Name, Description, Contact Details </strong></span></div></td>
</tr>
<tr valign="baseline">
<td align="right" valign="middle">Business Name:</td>
<td colspan="3"><input name="classbusinessname" type="text" value="<?php if (isset($_POST['classbusinessname'])) {
echo htmlentities($_POST['classbusinessname']);} ?>" size="32" maxlength="80" /><tr valign="baseline">
<td colspan="4" align="right" nowrap><div align="left"><span class="title"><strong>Select a Title for searching purposes </strong></span></div></td>
<tr valign="baseline">
<td align="right" valign="middle">Title:</td>
<td colspan="3"><p>
<input name="classTitle" type="text" size="32" maxlength="50" value="<?php if (isset($_POST['classTitle'])) {
echo htmlentities($_POST['classTitle']);} ?>" />
</p>
</td>
<br /></td>
</tr>
<tr valign="baseline">
<td align="right" valign="middle">Description:</td>
<td colspan="3"><textarea name="classDescription" id="classDescription" cols="30" rows="6" OnBlur="textCounter( this,classDescription__CharCount,200)" OnKeyDown="textCounter( this,classDescription__CharCount,200)" OnKeyUp="textCounter( this,classDescription__CharCount,200)"><?php if (isset($_POST['classDescription'])) {
echo htmlentities($_POST['classDescription']);} ?></textarea>
<script>
function textCounter(field, countfield, maxlimit)
{


if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.value = maxlimit - field.value.length;
}
</script>
</td>
</tr>
<tr valign="baseline">
<td align="right" valign="middle">&nbsp;</td>
<td colspan="3">Characters Remaining:<input tabindex='-1' class='threemax' width='2' id='classDescription_CharCount' name='classDescription__CharCount' type='text' readonly='true' value='200'>
</p> <p align="left"></p> <div align="left"></td>
</tr>
<tr valign="baseline">
<td align="right" valign="middle">Contact Name:</td>
<td colspan="3"><input name="contactname" type="text" value="<?php if (isset($_POST['contactname'])) {
echo htmlentities($_POST['contactname']);} ?>" size="32" maxlength="120" /></td>
</tr>
<tr valign="baseline">
<td align="right" valign="middle">Address:</td>
<td colspan="3"><textarea name="address" cols="30" rows="4"><?php if (isset($_POST['address'])) {
echo htmlentities($_POST['address']);} ?></textarea>
</td>
</tr>
<tr valign="baseline">
<td align="right" valign="middle">Australian State or Location:</td>
<td colspan="3"> <select name="stateCode" size="5" id="stateCode">
<?php
do {
?>
<option value="<?php echo $row_state['stateCode']?>"><?php echo $row_state['stateCode']?></option>
<?php
} while ($row_state = mysql_fetch_assoc($state));
$rows = mysql_num_rows($state);
if($rows > 0) {
mysql_data_seek($state, 0);
$row_state = mysql_fetch_assoc($state);
}
?>
<?php if (isset($_POST['stateCode'])) {
echo htmlentities($_POST['stateCode']);} ?></select></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap><div align="left"><strong>Thank You!</strong></div></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr valign="baseline">
<td nowrap align="right">&nbsp;</td>
<td colspan="3"><input name="listing" type="submit" id="listing" value="Insert Listing"></td>
</tr>
</table>
<input type="hidden" name="regid" value="<?php echo $row_rsGetRegid['regid']; ?>">
<input type="hidden" name="adscreation_date" value="NOW()">
<input type="hidden" name="MM_insert" value="form1">
</form>

The class_category and categorytype fields are a dynamic drop down list that was the checkboxes (of which I would like to have again).
This is the mysql for them:

Hope that makes it easier. I have read on the internet the way you were showing me previously but because my primary key is not an integer may be that could be an issue???

Thanks again

nanny
01-24-2007, 11:19 PM
This is the mysql for it....not enough room above.

CREATE TABLE class_category
(
class_category VARCHAR(255) default '' NULL,
PRIMARY KEY (class_category)
);
INSERT INTO class_category (class_category ) VALUES ('Beauty');
INSERT INTO class_category (class_category ) VALUES ('Day Spa');
INSERT INTO class_category (class_category ) VALUES ('Fashion');
INSERT INTO class_category (class_category ) VALUES ('Hairdressing');
INSERT INTO class_category (class_category ) VALUES ('Nails');
INSERT INTO class_category (class_category ) VALUES ('Make-Up');
INSERT INTO class_category (class_category ) VALUES ('Massage');
INSERT INTO class_category (class_category ) VALUES ('Tanning');
CREATE TABLE categorytype
(
categorytype VARCHAR(255) NULL,
PRIMARY KEY (categorytype)
);
INSERT INTO categorytype (categorytype) VALUES ('Associations/Clubs');
INSERT INTO categorytype (categorytype) VALUES ('Business Resources');
INSERT INTO categorytype (categorytype ) VALUES ('Design - Shopfit - Repair');
INSERT INTO categorytype (categorytype ) VALUES ('Education');
INSERT INTO categorytype (categorytype ) VALUES ('Employment');
INSERT INTO categorytype (categorytype ) VALUES ('For Sale');
INSERT INTO categorytype (categorytype ) VALUES ('Magazines');
INSERT INTO categorytype (categorytype ) VALUES ('Mobile Services');
INSERT INTO categorytype (categorytype) VALUES ('Photography');
INSERT INTO categorytype (categorytype ) VALUES ('Salons');
INSERT INTO categorytype (categorytype) VALUES ('Suppliers - Products - Equipment');

The primary key is not an integer, so maybe that makes a difference.
Thanks again.

davidj
01-24-2007, 11:22 PM
reformat the code so it includes the check boxes

nanny
01-25-2007, 01:05 AM
Sorry for the delay, the heat here in Australia got to me had to go to the pool for a while.
This is the checkboxes I haven't put the [] yet, is that placed in every one?
<tr valign="baseline">
<td colspan="4" align="right" nowrap><div align="left"><span class="title"><strong>Select an Industry </strong></span></div></td>
<tr valign="baseline">
<td width="115" align="right" valign="middle">Industry::</td>
<td colspan="3"><p> <label>
<input name="class_category" type="checkbox" id="class_category" value="Beauty" <?php if (!(strcmp($row_indust['class_category'],"Beauty"))) {echo "checked";} ?> />
Beauty
<br />
<input name="class_category" type="checkbox" id="class_category" value="Day Spa" <?php if (!(strcmp($row_indust['class_category'],"Day Spa"))) {echo "checked";} ?> />
Day Spa</label>
<label>
<br />
<input name="class_category" type="checkbox" id="class_category" value="Fashion" <?php if (!(strcmp($row_indust['class_category'],"Fashion"))) {echo "checked";} ?> />
Fashion</label>
<label>
<br />
<input name="class_category" type="checkbox" id="class_category" value="Hairdressing" <?php if (!(strcmp($row_indust['class_category'],"Hairdressing"))) {echo "checked";} ?> />
Hairdressing</label>
<br />
<label>
<input name="class_category" type="checkbox" id="class_category" value="Nails" <?php if (!(strcmp($row_indust['class_category'],"Nails"))) {echo "checked";} ?> />
Nails</label>
<label>
<br />
<input name="class_category" type="checkbox" id="class_category" value="Make-Up" <?php if (!(strcmp($row_indust['class_category'],"Make-Up"))) {echo "checked";} ?> />
Make-Up</label>
<label>
<br />
<input name="class_category" type="checkbox" id="class_category" value="Massage" <?php if (!(strcmp($row_indust['class_category'],"Massage"))) {echo "checked";} ?> />
Massage</label>
<label>
<br />
<input name="class_category" type="checkbox" id="class_category" value="Tanning" <?php if (!(strcmp($row_indust['class_category'],"Tanning"))) {echo "checked";} ?> />
Tanning</label>
<br />
</p>
</td>
<tr valign="baseline">
<td colspan="4" align="right" nowrap><div align="left"><span class="title"><strong>Select a Category </strong></span></div></td>
<tr valign="baseline">
<td align="right" valign="middle">Category::</td>
<td colspan="3"> <label>
<input <?php if (!(strcmp($row_cattype['categorytype'],"Associations/Clubs"))) {echo "checked";} ?> name="categorytype" type="checkbox" id="categorytype" value="Associations/Clubs" />
Associations/Clubs
<br />
<input <?php if (!(strcmp($row_cattype['categorytype'],"Business Resources"))) {echo "checked";} ?> name="categorytype" type="checkbox" id="categorytype" value="Business Resources" />
Business Resources
<br />
<input <?php if (!(strcmp($row_cattype['categorytype'],"Design - Shopfit - Repair"))) {echo "checked";} ?> name="categorytype" type="checkbox" id="categorytype" value="Design - Shopfit - Repair" />
Design - Shopfit - Repair
<br />
<input <?php if (!(strcmp($row_cattype['categorytype'],"Education"))) {echo "checked";} ?> name="categorytype" type="checkbox" id="categorytype" value="Education" />
Education
<br />
<input name="categorytype" type="checkbox" id="categorytype" value="Employment" />
Employment
<br />
<input <?php if (!(strcmp($row_cattype['categorytype'],"For Sale"))) {echo "checked";} ?> name="categorytype" type="checkbox" id="categorytype" value="For Sale" />
For Sale
<br />
<input <?php if (!(strcmp($row_cattype['categorytype'],"Magazines"))) {echo "checked";} ?> name="categorytype" type="checkbox" id="categorytype" value="Magazines" />
Magazines
<br />
<input <?php if (!(strcmp($row_cattype['categorytype'],"Mobile Services"))) {echo "checked";} ?> name="categorytype" type="checkbox" id="categorytype" value="Mobile Services" />
Mobile Services
<br />
<input <?php if (!(strcmp($row_cattype['categorytype'],"Photography"))) {echo "checked";} ?> name="categorytype" type="checkbox" id="categorytype" value="Photography" />
Photography
<br />
<input <?php if (!(strcmp($row_cattype['categorytype'],"Salons"))) {echo "checked";} ?> name="categorytype" type="checkbox" id="categorytype" value="Salons" />
Salons
<br />
<input <?php if (!(strcmp($row_cattype['categorytype'],"Suppliers - Products - Equipment"))) {echo "checked";} ?> name="categorytype" type="checkbox" id="categorytype" value="Suppliers - Products - Equipment" />
Suppliers - Products - Equipment</label></td>

Thanks again David.

davidj
01-27-2007, 06:20 PM
you have the correct syntax

this should work..

i normaly stick my php at the end of the tag so i dont confuse myself

nanny
01-27-2007, 09:20 PM
Hi David, thanks again.
Previously you had[0] ect. in the field, do I not need this at all?

<input name="industry[]" type="checkbox" id="industry[]" value="1" <?php if(!(strcmp($industry[0], 1))){echo "checked";}?>/>Beauty

So with what I have, do I use php to check if the array is empty and if so to select a checkbox?
Also will I still use the implode on this page and use implode and explode on the edit page?

Lots of questions, as theses are the areas I have been working on....

Thanks again for your help..

davidj
01-27-2007, 09:31 PM
you do not need to specify a numeric key in the name of the tag ...

name="class_category[1]" //<<


this key will be set automatically so this is valid...

name="class_category[]" //<<


if you set the value to the same as in the variable then the solution i provided will catch and match ...

<input name="class_category" type="checkbox" id="class_category" value="Beauty" <?php if (!(strcmp($row_indust['class_category'],"Beauty"))) {echo "checked";} ?> />

please PM me if you want further info

nanny
01-27-2007, 09:39 PM
Thanks David I will give this a whirl again.
It will be just a few minutes as I am checking other areas of the forum and css first.