Skip to main content
January 12, 2007
Question

checkboxes for application answered but I have a question

  • January 12, 2007
  • 8 replies
  • 597 views
Hi thanks and sorry I use the below thread for this purpose but everything I did from that works really well except on the insert record page when validating the checkboxes and making them sticky on return.

Hope someone has an answer as everywhere can tell you how to show the list but not how to validate.
This topic has been closed for replies.

8 replies

January 18, 2007
Back again pasting problem sorry, I attempted many times to attach my code yesterday so that is the result. I had to do my insert record and edit again.
I have fixed that up, thanks heaps.
But I do have both class_category and categorytype checkboxes as well as a dropdown list that won't highlight the selection on reload with errors.

I do have at the top of the page:
<?php
if (isset($_POST['class_category'])) {
$_POST['class_category'] = implode(',', $_POST['class_category']);
}
else {
$_POST['class_category'] = '';
}
?>
as well as for categorytype.
The $error is used with all of the other form fields together.
So I will try changing it as to how you have explained for $error and see if that makes a difference.
I don't need to have multiple selection for my location dropdown list but it would be great if the selection stayed rather than disappearing each time on reload.

Thanks for your patience...appreciated.........

Inspiring
January 17, 2007
jjjhbj111 wrote:
> I have this on the insert page:
> <?php
> if (isset($_POST['categorytype'])) {
> $_POST['categorytype'] = implode(',', $_POST['categorytype']);
> }
> else {
> $_POST['categorytype'] = '';
> }

Your validation script never checks $_POST['categorytype']. If you want
to make it a required field, you need to add an error message in the
else part:

else {
$_POST['categorytype'] = '';
$error['categorytype'] = 'You must select at least one category';
}

> $loginUsername = trim($_POST['reg_username']);
> if (empty($_POST['classbusinessname'])) {
> $error['classbusinessname'] = 'Please enter your business name';
> }

You may have a reason for doing this, but you're mixing
$_POST['reg_username'] and $_POST['classbusinessname'] here.

> -> the part of the form with checkboxes next
> <input type="checkbox" name="class_category[]" value="Beauty" id="Beauty"

The name of your checkboxes (class_category) is different from what
you're checking for (categorytype) at the top of the page.

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
January 17, 2007
Sorry David I am still not getting anywhere and sometimes when I change things it will just go straight to showing a list of my whole directory.

I have this on the insert page:
<?php
if (isset($_POST['categorytype'])) {
$_POST['categorytype'] = implode(',', $_POST['categorytype']);
}
else {
$_POST['categorytype'] = '';
}
?>
<?php
session_start();
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;

// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they log in.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && true) {
$isValid = true;
}
}
return $isValid;
}

$MM_restrictGoTo = "../error.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
$MM_referrer .= "?" . $QUERY_STRING;
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}

// Validate form input
$MM_flag="MM_insert";
if (isset($_POST[$MM_flag])) {

$title = trim($_POST['classTitle']);
if (empty($title)) {
$error['classTitle'] = 'Please enter your title for searching';
}

$loginUsername = trim($_POST['reg_username']);
if (empty($_POST['classbusinessname'])) {
$error['classbusinessname'] = 'Please enter your business name';
}


function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
-> the rest of this and then
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if (!isset($error)) {
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
-> the rest of this and then
mysql_select_db($database_config, $config);
$Result1 = mysql_query($insertSQL, $config) or die(mysql_error());

$insertGoTo = "thanks.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
$_POST = array();
}
}
-> the top of page info
Place your free listing below:<br />
<span>
<?php
if ($error) {
echo '<ul>';
foreach ($error as $alert) {
echo "<li class='errmsg'>$alert</li>\n";
}
echo '</ul>';
// remove escape characters from POST array
if (get_magic_quotes_gpc()) {
function stripslashes_deep($value) {
$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
return $value;
}
$_POST = array_map('stripslashes_deep', $_POST);
}
}
?>
-> the part of the form with checkboxes next
<input type="checkbox" name="class_category[]" value="Beauty" id="Beauty"
<?php
// You need this in the first check box only
$industry = isset($_POST['class_category']) ? true : false;

// This goes in every check box, but change the first argument of
// in_array() to match the value of the check box
if ($industry && isset($error) && in_array('Beauty',
$_POST['class_category'])) echo 'checked="checked"'; ?> />
Beauty

<input type="checkbox" name="class_category[]" value="Day Spa" id="Day Spa"
<?php if ($industry && isset($error) && in_array('Day Spa',
$_POST['class_category'])) echo 'checked="checked"'; ?> />
Day Spa
<input type="checkbox" name="class_category[]" value="Fashion" id="Fashion"
<?php if ($industry && isset($error) && in_array('Fashion',
$_POST['class_category'])) echo 'checked="checked"'; ?> />
Fashion
<input type="checkbox" name="class_category[]" value="Hairdressing" id="Hairdressing"
<?php if ($industry && isset($error) && in_array('Hairdressing',
$_POST['class_category'])) echo 'checked="checked"'; ?> />
Hairdressing
</p>
-> and this continues with other text areas and another checkbox list categorytype.

Hope this enlightens where I am going wrong.
Inspiring
January 16, 2007
jjjhbj111 wrote:
> Sorry David to be a big pain in the................
> nothing makes the checkbox stay.
> Is it because I am using php 4 and dreamweaver mx 2004?

No. The code that I gave you works fine with PHP 4.

> To do a multiple list do you do the same? I know you use implode and explode
> the same.

Yes. I don't know if you have got a copy, but "PHP Solutions" shows you
how to handle all form elements and make them sticky when errors are
detected on submission.

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
January 16, 2007
Sorry David to be a big pain in the................
nothing makes the checkbox stay.
Is it because I am using php 4 and dreamweaver mx 2004?
It works for all errors on the page and returns with data that has been submitted but never for the checkboxes.
The edit pages show the checkboxes and I get the comma delimited set on the user results pages for them to edit or delete.
Should I try something else?
To do a multiple list do you do the same? I know you use implode and explode the same.

Thanks for your time and efforts, I need to be able to have more than text fields that enables error checking.

Inspiring
January 13, 2007
jjjhbj111 wrote:
> Sorry David it still isn't staying checked no matter what I try.
> Would it be because I have:
> GetSQLValueString($_POST['interests'], "text"),
>
> rather than:
> GetSQLValueString(isset($_POST['interests']) ? "true" : "",
> "defined","'Y'","'N'"),

No, it's because of the incorrect use of isset() in your previous post.

This is correct:

GetSQLValueString($_POST['interests'], "text")

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
January 13, 2007
Sorry David it still isn't staying checked no matter what I try.
Would it be because I have:
GetSQLValueString($_POST['interests'], "text"),

rather than:
GetSQLValueString(isset($_POST['interests']) ? "true" : "", "defined","'Y'","'N'"),

I have in my database as a text that is why I used text.
I manually added the checkboxs and assigned the the name accoding the set in my database. Works great on insert and edit with a delimited comma for display but the checkboxes will not stay checked on error.

Sorry for the headache........
Inspiring
January 12, 2007
jjjhbj111 wrote:
> Hi thanks and sorry I use the below thread for this purpose but everything I
> did from that works really well except on the insert record page when
> validating the checkboxes and making them sticky on return.

This is a prime example of how not to ask a question in an online forum.
You refer to a "thread below" without saying what the subject line is,
and you don't say which server-side language you're using. Please be
more precise when seeking help. However, you're fortunate that I
remember you're using PHP.

Check boxes do not appear in the $_POST array unless at least one has
been checked. Therefore, to make them sticky, you need to check that the
check box $_POST variable has been set and, if so, whether the value of
the check box is in the array.

Let's assume that you have a variable called $error that indicates a
validation problem.

<input type="checkbox" name="interests[]" value="Football" id="Football"
<?php
// You need this in the first check box only
$boxesChecked = isset($_POST['interests']) ? true : false;

// This goes in every check box, but change the first argument of
// in_array() to match the value of the check box
if ($boxesChecked && isset($error) && in_array('Football',
$_POST['interests'])) echo 'checked="checked"'; ?> />

<input type="checkbox" name="interests[]" value="Tennis" id="Tennis"
<?php if ($boxesChecked && isset($error) && in_array('Tennis',
$_POST['interests'])) echo 'checked="checked"'; ?> />

You can find details of how to validate all types of form input (and a
lot, lot more) in my book "PHP Solutions"
( http://foundationphp.com/phpsolutions/).

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
January 13, 2007
Hi David
Thanks for replying and I really do appologise.
Sometimes in haste we do silly things...........
I have researched and have done exactly what you have suggested, but didn't work.
The last time I had trouble with this I had my closing brackets in the wrong place.
I will try again.
Also thank you for explaining that:
quote:

Check boxes do not appear in the $_POST array unless at least one has
been checked. Therefore, to make them sticky, you need to check that the
check box $_POST variable has been set and, if so, whether the value of
the check box is in the array.


I will try and play with this again and let you know.
Thanks again, it's wonderful to get such expertise from here.


With this being an array can I still check for $error like this: