如何在php中验证Web表单 [英] How to validate a web form in php
问题描述
我已经开始为在线图书馆的注册表格创建一个独立的网页.
我想验证PHP和JavaScript中两个单独文件的用户输入,以便易于阅读,我可以将它们全部链接到下面的HTML文件中.
由于我是PHP和JavaScript的新手,所以请您提供一些帮助.
Hi,
I have started creating a stand alone web-page for a registration form for an online library.
I want to validate user inputs in PHP and JavaScript both separate files so its easy to read and I could just link them all to the HTML file i have below.
As i am new to PHP and JavaScript i would appreciate some help starting this please.
<html>
<body>
<div id="form">
<div id="header">
<h1>Registration Form</h1>
</div>
<form>
<fieldset id="Logon">
<legend>Create Logon</legend>
<!-- add a class to all the input boxes instead. Safer and more backwards-compatible -->
<div class="form-line">
<label for="username">Username *: </label>
<input type="text" id="username" /></div>
<div class="form-line">
<label for="pwd">Password *: </label>
<input type="password" id="pwd" /></div>
</fieldset>
<!-- adding a "confirm" password box also -->
<fieldset id="Details">
<legend>User Details</legend>
<div class="form-line">
<label for="surname">Surname *: </label>
<input type="text" id="surname" /></div>
<div class="form-line">
<label for="other_names">Other Names *: </label>
<input type="text" id="names" /></div>
<div class="form-line">
<label for="dob">Date of Birth *: </label>
<input type="text" id="dob" /></div>
<div class="form-line">
<label for="email">Email *: </label>
<input type="email" id="email" /></div>
<!-- Valid input types: http://www.w3schools.com/html5/html5_form_input_types.asp -->
<div class="form-line">
<label for="tel">Telephone: </label>
<input type="text" id="tel" /></div>
<div class="form-line">
<label for="add">Address 1 *: </label>
<input type="text" id="add1" /></div>
<div class="form-line">
<label for="add">Address 2: </label>
<input type="text" id="add2" /></div>
<div class="form-line">
<label for="add">Town/City *: </label>
<input type="text" id="twn" /></div>
<div class="form-line">
<label for="add">County: </label>
<input type="text" id="count" /></div>
<div class="form-line">
<label for="ptc">Post Code *: </label>
<input type="text" id="ptc" /></div>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
<p>Note: Please make sure your details are correct before submitting form and that all fields marked with * are completed!.</p>
</fieldset>
</form>
</div>
<div id="footer"> Your Online Library</div>
</body>
</html>
感谢您的提前帮助.
Thanks for you help in advance.
推荐答案
1.在 my.html 的"head" 或"body" 内包含 myscript.js 文件的路径.文件.
示例:
1. Include a path to your myscript.js file inside the "head" or "body" in my.html file.
Example:
<script src="..../myscript.js" type="text/javascript"></script>
2.对于每个"input" 实例,添加事件"onclick" .
注意:您的脚本将通过"id" 查找元素,拥有唯一的ID很重要!
例子:
2.For every "input" instance add event "onclick".
NOTE : Your script is going to find an element by "id" it''s important to have a unique id!
Example :
<input type="password" id="pwd" onclick="javascript:return checkById('pwd')" />
3.I您的 myscript.js 为每个元素编写函数.
示例:
3.I your myscript.js write your function for each element.
Example:
function checkById(codeElement)
{
var checkInputCode = document.getElementById(codeElement);
//get value inside the codeElement and compare it whatever you need
if(codeElement == 'pwd')
{
if(checkInputCode.value == "")
{
alert("YOU ENTER NOTHING");
return false;
}
else if(checkInputCode.value == 'correct password')
{
//do something...
return true;
}
//else if..... etc.
}
if(codeElement == 'surname')
{
...... so on
}
//if nothing found return false
return false;
}
您还可以为每个 codeElement 创建切换案例" (例如:pwd,count,ptc ...),然后针对以下内容执行 if语句正确输入.
希望我能帮上忙.
You can also create "Switch Case" for each codeElement (like : pwd,count,ptc...) and then execute if statments for correct input.
Hope i helped.
这篇关于如何在php中验证Web表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!