You are here: Re: Form validation with PHP/Javascript « PHP Programming Language « IT news, forums, messages
Re: Form validation with PHP/Javascript

Posted by Chris on 08/16/06 22:23

"Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
news:6LWdnftQxpp1xn_ZnZ2dnUVZ_umdnZ2d@comcast.com...
>
> Chris,
> I don't rely on js for form validation. The user can turn it off too
> easily. I validate everything server side. Even when I validate with JS,
> I revalidate on the server.
>
<snip>
> I use both, depending on the circumstances.
>
> But the bottom line is - always validate server side, even if you fist
> validate client side.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstucklex@attglobal.net
> ==================

Hi,

I've been working with an example (from
http://www.php-mysql-tutorial.com/form-validation-with-php.php) that
validates first js, then PHP. It used an onClick event in the submit button
rather than an onSubmit in the form tag. The copy/paste code from the site
works beautifully, first the js, then the php. The original form doesn't
define an action, but I inserted one just to see what would happen and it
still worked. I took the code, pasted it into my form page and edited the
variables to match only 2 of my textboxes (simply to check if they had a
value) for a test run. In the test I removed the action parameter just to
see if the validation worked. I left both textboxes empty - the form ran
but the only validation that occurred was PHP, the js alerts never appeared.
Then I put the form action pointing to a processing page back in, it never
saw the js, posted the php errors, then processed through. I tried useing
onSubmit, but it doesn't work at all. It looks like I'm stuck with only
having server-side processing, which refreshes the page each time the error
message comes up - so I need to add the $_POST values or variables back into
the form.

Since beginning to write this message, I have edited the page several times,
and what you see below works with php validation, but the js never occurs. I
just don't understand why it works so well in the example (even when I make
changes or change the action parameter, etc.) I've snipped several areas in
what you see below, but compared to the original code at the website, the js
appears to be correct other than editing variables (unless I accidentally
overlooked something like a parens or whatever). I'm just burnt out
now....and have 1 day to finish and deploy the site.

<script language="JavaScript">
function checkForm()
{

var title, desc;
with(window.document.upload)
{
title = title;
desc = description;

}

if(trim(title.value) == '')
{
alert('Please enter a title');
title.focus();
return false;
}
else if(trim(desc.value) == '')
{
alert('Please enter a description');
desc.focus();
return false;
}
else
{
title.value = trim(title.value);
desc.value = trim(desc.value);
return true;
}
}

/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
return str.replace(/^\s+|\s+$/g,'');
}

/*
Check if a string is in valid email format.
Returns true if valid, false otherwise.
*/
</script>


<?php
$root = $_SERVER['DOCUMENT_ROOT'];

$errmsg = ''; // error message
$title = ''; // doc title
$desc = ''; // doc description

if(isset($_POST['send']))
{
$title = $_POST['title'];
$desc = $_POST['description'];

if(trim($title) == '')
{
$errmsg = 'Please enter a title';
}
else if(trim($desc) == '')
{
$errmsg = 'Please enter a description';
}
if($errmsg == '')
{

//set variables:


if (isset($_POST['toolType'])) {
$tooltype_id = $_POST['toolType'];
} else {
$tooltype_id = "";
}

if (isset($_POST['toolCat'])) {
$toolcat_id = $_POST['toolCat'];
} else {
$toolcat_id = "";
}

if (isset($_POST['tool'])) {
$tool = $_POST['tool'];
} else {
$tool = "";
}

$primeproj_id = $_POST['project'];
$cat_id = $_POST['category'];
$subcat_id = $_POST['subcategory'];
$multiple ="";

//get group and project code and set variables
<snip>
//get tooltype and tool category codes

<snip>

//check that form has been submitted
if (isset($_POST['uploadfile'])) { //begin check for submission

//check if file uploaded

if (isset($_FILES['fileupload']['name'])) { // begin check for uploaded
file

$uploadedname = $_FILES['fileupload']['name'];
$tempname = $_FILES['fileupload']['tmp_name'];

//check if tool upload
if (isset($_POST['tool'])) { //begin tool upload

require('toolupload.php');

} else { //end tool upload and begin standard doc upload

require('docupload.php');

} //end all uploads

} else {//Start error statement
echo "Your forgot to upload a document!";
echo $_FILES['fileupload']['error']."<br />";
print_r($_FILES)."<br />";

} //End error statement

} //End check for submission

}
} // end php check for errors

if(!isset($_POST['send']) || $errmsg != '')
{

?>
<div align="center" class="errmsg"><?=$errmsg;?></div>
<form enctype="multipart/form-data" name="upload" id="upload" method="post">
<input name="uploadfile" type="hidden" value="uploadfile" />

<?php
//Get projects from db

<snip>

//Get tooltypes from db
<snip>

//Get toolcats from db
<snip>

?>
<!--//Create the file and description inputs. -->

<table width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
<td><fieldset>
<legend>Tool Document</legend>
<strong>IMPORTANT</strong>: Complete this section <strong>only</strong> if
document/link is a Tool, otherwise continue to <a href="#Standard">Standard
Document Category</a> section)<br /><br /> <input type="checkbox"
name="tool" value="Tools" />
<strong>Tool</strong> (select this if document is a tool)<br /><br
/>
<strong>Tool Type:</strong> (Select one)
<br />
<?php do{ ?>
<label>
<input type="radio" name="toolType" id="toolType" value="<?php echo
$row_tooltypes['toolTypeID']?>" />
<?php echo $row_tooltypes['toolType']?></label>
<?php }
while ($row_tooltypes = mysql_fetch_assoc($tooltypes)); ?><br />
<strong>Tool Category: </strong>(Select one) <br />

<?php do{ ?>
<label>
<input type="radio" name="toolCat" id="toolCat" value="<?php echo
$row_toolcats['toolCatID']?>" />
<?php echo $row_toolcats['toolCat']?></label>
<?php }
while ($row_toolcats = mysql_fetch_assoc($toolcats)); ?>
</fieldset></td>

<snip>

<td><fieldset>
<legend>Document Information</legend>
<strong>Document Title</strong>:
<input name="title" id="title" type="text" size="40"
maxlength="50" value="<?php echo $title;?>"/>
<br />
(Required - no more than 50 characters)<br />
<br />
<strong>Description:</strong>(Required -150 characters) <br />
<textarea name="description" cols="30" rows="5" value="<?php echo
$desc;?>"></textarea>
<br />

</fieldset></td>
<td><fieldset>
<legend>Project Data</legend>
</p>
<strong>Primary Project:</strong>
<select name="project" id="project">
<option value="value">Select a project...</option>
<?php
do {
?>
<option value="<?php echo $row_projects['projID']?>"><?php echo
$row_projects['projCode']?></option>
<?php
} while ($row_projects = mysql_fetch_assoc($projects));
$rows = mysql_num_rows($projects);
if($rows > 0) {
mysql_data_seek($projects, 0);
$row_projects = mysql_fetch_assoc($projects);
}
?>
</select>
<snip>
<tr>
<td><fieldset>
<legend>Document to Upload</legend>Upload a document from your computer.
<p><strong>File:</strong>
<input type="file" name="fileupload" id="fileupload"/>
</p></fieldset></td>
<td></td>
</tr>
</table>
<p> </p>
<div align="center">
<input type="submit" name="send" id="send" value="Submit"
onClick="return checkForm();"/>
</div>
</form>
<?php } // end form ?>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация