使用PHP / Javascript进行表单验证

问题描述:




我有一个上传文件和将数据插入mysql的表格

db。我想验证表格。我已经尝试了几个Javascript

表格验证功能,但似乎数据直接进入了

处理页面,而不是javascript看到数据是否缺失和

弹出警报。我认为这可能是因为大部分表格都是用数据库(列表等)填充的数据,但是当我在

a简单文本框中遗漏数据时,它仍然没有看到它。我已经尝试了几个不同的b / b $ b的东西,这些东西都没用,所以我简化并尝试在一个最简单的级别处理一个表格

元素时间 - 仍然没有运气。有什么东西

特别需要做的是让PHP在PHP之前工作吗?是否有一个

使用PHP进行客户端验证的好方法?


这里有大部分代码:


函数ValidateForm(上传)

{

if(IsEmpty(upload.title))

{

alert(''你没有输入标题!'')

form.title.focus();

返回false;

}


返回true;


}

< / script>

<?php

注意:查询在这里 - 它们填写了一些列表框


< form enctype =" multipart / form-data&quot ;行动= QUOT; upload.php的&QUOT; name =" upload"

id =" upload"方法= QUOT;交&QUOT; onsubmit =" javascript :返回ValidateForm(上传)">

< input name =" uploadfile"类型= QUOT;隐藏&QUOT;值= QUOT; uploadfile&QUOT; />

<! - //创建文件和描述输入。 - >


< table width =" 100%"边界=&QUOT; 0&QUOT; CELLSPACING = QUOT; 5英寸cellpadding =" 0">

< snip - 取出不同的类别列表>

< tr>

< td < fieldset>

< legend>< a name =" Standard" id =" Standard">< / a>标准文档< / legend>

< br />完成此部分< strong>仅< / strongif / document is link

a工具。< br />< br />

< snip - 取出生成的类别/子类别列表>

< / td>

< / tr>

< tr>

< td>< fieldset>

< legend>文档信息< / legend>

< strong>文档标题< / strong>:

< input name =" title" ID = QUOT;标题&QUOT;类型= QUOT;文本&QUOT; size =" 40"

maxlength =" 50" />

< br />

(必填 - 不超过50个字符)< br />

< br />

< strong>说明:< / strong>(必填-150个字符)< br />

< textarea name ="描述&QUOT; COLS = QUOT; 30英寸; rows =" 5">< / textarea>

< br />


< / fieldset>< / td>

< td>< fieldset>

< legend>项目数据< / legend>

< / p>

< strong>主要项目:< / strong>

< select name =" project" id =" project">

< option value =" value">选择一个项目...< / option>

<?php

做{

?>

< 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>

< / p>

< snip-取出另一个列表框>

< / fieldset>< / td>

< / tr>

< tr>

&lt ; td>< fieldset>

< legend>要上传的文档< / legend>从您的计算机上传文档。

< p>< strong>文件:其中/强&GT;

< input type =" file"名称= QUOT;文件上传&QUOT; id =" fileupload" />

< / p>< / fieldset>< / td>

< td>< / td>

< / tr>

< / table>

< p< / p>

< div align =" center">

< input type =" submit"命名= [提交&QUOT;值= [提交&QUOT; />

< / div>

< / form>

< snip>

? >


谢谢

Chris

Hi,

I have a form for uploading documents and inserting the data into a mysql
db. I would like to validate the form. I have tried a couple of Javascript
form validation functions, but it appears that the data goes straight to the
processing page, rather than the javascript seeing if data is missing and
popping up an alert. I thought it may be because much of the form is
populated with data from the db (lists, etc.), but when I leave out data in
a simple textbox, it still doesn''t see it. I''ve tried a couple of different
things, that didn''t work, so I simplified and tried to address one form
element at the simplest level at a time - still no luck. Is there something
special I need to do to make the Javascript work before the PHP? Is there a
good way to do client side validation with PHP?

Here''s most of the code:

function ValidateForm(upload)
{
if(IsEmpty(upload.title))
{
alert(''You have not entered a title!'')
form.title.focus();
return false;
}

return true;

}
</script>
<?php
NOTE: queries are here - they fill some of the list boxes

<form enctype="multipart/form-data" action="upload.php" name="upload"
id="upload" method="post" onsubmit="javascript:return ValidateForm(upload)">
<input name="uploadfile" type="hidden" value="uploadfile" />
<!--//Create the file and description inputs. -->

<table width="100%" border="0" cellspacing="5" cellpadding="0">
<snip - took out a different category listing>
<tr>
<td <fieldset>
<legend><a name="Standard" id="Standard"></a>Standard Document</legend>
<br />Complete this section <strong>only</strongif document/link is NOT
a Tool.<br /><br />
<snip - took out the generated category/subcategory lists>
</td>
</tr>
<tr>
<td><fieldset>
<legend>Document Information</legend>
<strong>Document Title</strong>:
<input name="title" id="title" type="text" size="40"
maxlength="50" />
<br />
(Required - no more than 50 characters)<br />
<br />
<strong>Description:</strong>(Required -150 characters) <br />
<textarea name="description" cols="30" rows="5"></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>
</p>
<snip- took out another listbox>
</fieldset></td>
</tr>
<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="submit" value="Submit" />
</div>
</form>
<snip>
?>

Thanks
Chris

row_projects [''projID '']?>"><?php echo
row_projects[''projID'']?>"><?php echo


row_projects [''projCode'']?>< / option>

<?php

} while(
row_projects[''projCode'']?></option>
<?php
} while (


row_projects = mysql_fetch_assoc(
row_projects = mysql_fetch_assoc(