PDA

View Full Version : for insert record + server-side form validation to work....


nanny
10-11-2006, 03:27 AM
Hi should you use the enctype="application/x-www-form-urlencoded" for the validation to work? I have used that for form mail but not sure what to use for this? Dreamweaver doesn't use anything. I know if you are doing an upload you need to use multi-part/form data.
I had my server-side working the other day and now it isn't so I am trying to work out scenarios.

Thanks for your help.
Also does anybody know how to call the implode and explode function for checkboxes within an insert/update record?
NOt sure whether they happen up top before the form or within.
Cheers

davidj
10-11-2006, 08:57 AM
not sure if you need the enctype for form validation to work. I have never used it and i validate forms all the time (PHP)

to use explode...String into Array

$string = "no1, no2, no3, no4";

$ArrayFromString = explode(",", $string);

echo $ArrayFromString [0]; // << output no1
echo $ArrayFromString [2]; // << output no3

to use implode...Array into String (using array above)

$StringFromArray = implode(",", ArrayFromString );

echo $StringFromArray; // output no1, no2, no3, no4

nanny
10-12-2006, 04:40 AM
Thanks davidj,
Not sure how to use what you have given me, totally new to arrays etc.
But in one form it is simply name, business, username, password for registration.
The next form is the listing page where they insert contact details, description and can select mulitple categories and multiple locations.

So would I put use this only for the second form where I am using checkboxes?
Or a muliple list?
I am figuring this would be too hard for location considering it is a lot of locations.
Thanks hope I am making sense.
Keen to learn........

davidj
10-12-2006, 08:39 AM
im sorry but you have lost me

im not sure what you want to do

nanny
10-12-2006, 09:12 AM
Me too............

I have been having major trouble trying to use php as validation for my dreamweaver forms. I can get javascript to work well but have been advised to use php instead.
My first form is a Registration page with the username and password.
I am trying to get the page to return with errors showing but still show form fields including already submitted information. Otherwise it will be really annoying for people to keep on inserting the same information over and over again.
Well I had it working for one day and then something happened and it stopped.

I can give the code if you like.......

The second form is for the Registered user to place a listing.
That is where I was going to add checkboxes for the categories and allow them to insert muliple choices. That is where I thought I would have to implode and explode.
I have been looking at php.net but sometimes it gets confusing, especially when using dreamweaver and the code is different.
Thanks.....keen to learn...

davidj
10-12-2006, 11:50 AM
this is very spooky

i have just dealt with such a problem in my own code

have you set your field values with the $_Post vars ??
$document_type = $_post['document_type'];
<input name="document_type" type="text" class="form-field-3" id="document_type" value="<?php echo $document_type;?>">

davidj
10-12-2006, 11:57 AM
That is where I was going to add checkboxes for the categories and allow them to insert muliple choices. That is where I thought I would have to implode and explode.

if you are using checkboxes as long as the boxes have a coresponding database field then you can have as many as you like with using arrays. Just give them a unique name

nanny
10-13-2006, 12:22 AM
Hi thanks for your help. I read a lot of tutorials on the net but when it comes to dreamweaver it has its own way of doing things. I inserted what I have below my connection details, what I am using for the Register Form. This is the one that worked one day and then that was it, never again.
PHP CODE:

<?php
mysql_select_db($database_config, $config);
$query_register = "SELECT * FROM regid";
$register = mysql_query($query_register, $config) or die(mysql_error());
$row_register = mysql_fetch_assoc($register);
$totalRows_register = mysql_num_rows($register);

if (array_key_exists('submit', $_POST)) {
$name = trim($_POST['reg_fullname']);
$bus = trim($_POST['reg_busname']);
$pswd = trim($_POST['$pswd']);
$loginUsername = trim($_POST['reg_username']);
if (empty($name)) {
$error['reg_fullname'] = 'Please enter your full name';
}
if (empty($bus)) {
$error['reg_busname'] = 'Please enter your business name';
}
if (empty($pswd)) {
$error['reg_password'] = 'Please enter your password up to 6 characters';
}
if (empty($loginUsername)) {
$error['reg_username'] = 'Please enter your email address if you need one go to hotmail.com';
}
$pattern = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
if (!preg_match($pattern, trim($loginUsername))) {
$error['reg_username'] = 'Please enter a valid email address';
}
$LoginRS__query = "SELECT reg_username FROM regid WHERE reg_username='" . $loginUsername . "'";
mysql_select_db($database_config, $config);
$LoginRS=mysql_query($LoginRS__query, $config) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if($loginFoundUser){
$error['reg_username'] = "$loginUsername is already in use. Please choose a different username.";
}

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . date("Y-d-m",strtotime($theValue)) . "'" : "NULL";
break;
case "time":
$theValue = ($theValue != "") ? "'" . date("H:i:s",strtotime($theValue)) . "'" : "NULL";
break;
case "datetime":
$theValue = ($theValue != "") ? "'" . date("Y-d-m H:i:s",strtotime($theValue)) . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}

$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")) {
$insertSQL = sprintf("INSERT INTO regid (reg_fullname, reg_busname, reg_username, reg_password, reg_phone, reg_status, creation_date) VALUES (%s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['reg_fullname'], "text"),
GetSQLValueString($_POST['reg_busname'], "text"),
GetSQLValueString($_POST['reg_username'], "text"),
GetSQLValueString($_POST['reg_password'], "text"),
GetSQLValueString($_POST['reg_phone'], "text"),
GetSQLValueString($_POST['reg_status'], "text"),
GetSQLValueString($_POST['creation_date'], "date"));

mysql_select_db($database_config, $config);
$Result1 = mysql_query($insertSQL, $config) or die(mysql_error());

$insertGoTo = "signin.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
$_POST = array();
}
?>

HTML CODE The form:


<table width="542" border="0" cellpadding="17" cellspacing="0" bordercolor="#000000">
<tr>
<td style="color:000000;line-height:1.6em"><div align="justify">


<span class="style2"><?php
if ($error) {
echo '<ul>';
foreach ($error as $alert) {
echo "<li class='errmsg'>$alert\n";
}
echo '[/list]';
// 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);
}
}
?>


<span class="style5">*</span></span> asterisk means this section must be filled out </p>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table align="center">
<tr valign="baseline">
<td nowrap align="right"><span class="style5">* </span><span class="texter">Your Full Name:</span></td>
<td><input name="reg_fullname" type="text" id="reg_fullname" value="<?php if (isset($_POST['reg_fullname'])) {
echo htmlentities($_POST['reg_fullname']);} ?>" size="32" maxlength="50"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"><span class="style5">* <span class="texter">Business Name:</span></span></td>
<td><input name="reg_busname" type="text" class="style11" id="reg_busname" value="<?php if (isset($_POST['reg_busname'])) {
echo htmlentities($_POST['reg_busname']);} ?>" size="32" maxlength="80"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"><span class="style5">* <span class="texter">Email Address:</span></span></td>
<td><input name="reg_username" type="text" class="style11" id="reg_username" value="<?php if (isset($_POST['reg_username'])) {
echo htmlentities($_POST['reg_username']);} ?>" size="32" maxlength="100">


This will be your username to sign in and retreive lost password. If you don't have an email address you can get one for free from hotmail.com </td>
</tr>
<tr valign="baseline">
<td nowrap align="right"><span class="style5">*<span class="texter"> Password:</span></span></td>
<td><input name="reg_password" type="text" class="style11" value="<?php if (isset($_POST['reg_password'])) {
echo htmlentities($_POST['reg_password']);} ?>" size="6" maxlength="6"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"><span class="texter">Phone</span>:</td>
<td><input name="reg_phone" type="text" id="reg_phone" class="style11" value="<?php if (isset($_POST['reg_phone'])) {
echo htmlentities($_POST['reg_phone']);} ?>"size="10" maxlength="10"></td>
</tr>
<tr valign="baseline">
<td colspan="2" align="right" nowrap><textarea name="message" cols="60" rows="20" readonly="readonly" wrap="virtual">Taken this out as it is not necessary to be seen here</textarea></td>
</tr>
<tr valign="baseline">
<td colspan="2" align="right" nowrap><div align="center"><span class="style1">By Registering you agree to the above conditions.</span><span class="style19 style1"></span><span class="style19"></span></div></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"></td>
<td><input name="submit" type="submit" id="submit" style="padding:0;background-color:CC3300;border: 1px solid #000000;color:FFFFFF;width:55;font-weight:bold" value="Continue"></td>
</tr>
</table>
<input type="hidden" name="reg_status" value="<?php echo $row_register['reg_status']; ?>">
<input type="hidden" name="creation_date" value="NOW()">
<input type="hidden" name="MM_insert" value="form1">
</form>


</p>
</div></td>
</tr>
</table>


Go to Next Page:
After this they sign in and that takes them to the page where they can place their listing (advertisement) for the directory. The listing insert record is the one that will have the checkboxes for categories. I wasn't sure whether I have started on the right track.

Firstly I have my mysql table as this:

CREATE TABLE class_category
(
class_category VARCHAR(50) 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');

Maybe I should have given it a numbered id for the primary key. It works without if I am using it as a normal selection, but not sure for checkbox and multi choices.

Any way, with the checkbox I have been using this:

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


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


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


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


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


<?php
//start loop

$var = "class_category";

if (isset($_POST[$var])) {

//add checkbox to database

}

//end loop
?>


Do I have to give them an id="class_category"?
Would I be better off not having that last piece after the checkbox array and have this on top of the page instead? From what I gather this would be helpful for the update record page, am I on the right track?

<?php
if(!isset($_POST['class_category']))
{
//array is empty
$_POST['class_category'] = array();
echo "None selected.";
}
else
{
//must do this for checkboxes (passing multiple values)
foreach ($_POST['class_category'] as $var)
{
echo " - $var";
}
}

$var = $_POST['class_category'];
$varString = implode(",",$var);
?>

So many questions for you, hope it makes sense this time.
Thank you.

davidj
10-13-2006, 08:45 AM
1.) never rely on Dreamweaver to write your code. It writes junk
2.) your question is huge and will take more than a 10 minute read and reply, something i wont be able to get away with here at work.
This needs re-writing a bit so need the weekend to check it out

nanny
10-13-2006, 09:01 AM
Thank you for having a look, you are very kind.
Thanks again.

nanny
10-16-2006, 10:07 AM
Hey did you get a chance to have a look at this one?
Cheers