Php 保存表单';s文本字段数据并将其重置为空白
因此,对于web应用程序,我需要使用HTML/PHP/MySQL编写一个新的用户表单。 用户表单的要求包括:Php 保存表单';s文本字段数据并将其重置为空白,php,forms,reset,Php,Forms,Reset,因此,对于web应用程序,我需要使用HTML/PHP/MySQL编写一个新的用户表单。 用户表单的要求包括: 自传 具有用户名、密码和确认密码文本字段 具有重置所有字段的重置按钮 如果两个密码字段不匹配,则报告错误、重置表单并保留用户名 如果已使用用户名,请报告错误并重置所有内容 如果一切正常,对密码进行散列和加密,将数据添加到数据库,报告成功,并重置表单 我目前正在研究3号和4号。这是我到目前为止所拥有的(如果代码很难阅读,请道歉): 用户名:将表单重置为加载时的状态 要么坚持使用PHP,然
用户名:
将表单重置为加载时的状态
要么坚持使用PHP,然后需要将类型更改为submit
,并通过if(isset($\u POST[“reset”])
检查用户是否要重置表单
或者使用Javascript:
必须有两个文件
index.html
savedata.php
现在,您可以通过jQuery、AJAX将表单数据从index.html发送到savadata.php。
只需遵循以下步骤。
将此脚本添加到已创建的index.html中
<script>
jQuery(function(){
$('form').submit(function(e) {
e.preventDefault();
var username = $("#userName").val();
var password = $("#password").val();
var cPassword = $("#passwordConfirm").val();
// i am validating confirm password on client side but don't do it for security reasons
if(password == cPassword) {
// now send the Form Data to Server Side to save theme
$.post('savadata.php', {
username:username,
password:password
} , function(responce){
if(responce == '1') {
// form submitted successfully
// clear form fields
$("#userName").val('');
$("#password").val('');
$("#passwordConfirm").val('');
alert('Data Saved...');
} else {
alert('there is an error to save data');
}
})
} else {
alert('Confirm password not matched');
}
}
});
</script>
添加用于检查字段的脚本函数,以便我们预测错误。请尝试一些JQUERY
进行验证。。。它将提供简单的验证。如果你建议他使用客户端验证而不是服务器端验证,那么你提供了一些非常糟糕的建议。
<script>
jQuery(function(){
$('form').submit(function(e) {
e.preventDefault();
var username = $("#userName").val();
var password = $("#password").val();
var cPassword = $("#passwordConfirm").val();
// i am validating confirm password on client side but don't do it for security reasons
if(password == cPassword) {
// now send the Form Data to Server Side to save theme
$.post('savadata.php', {
username:username,
password:password
} , function(responce){
if(responce == '1') {
// form submitted successfully
// clear form fields
$("#userName").val('');
$("#password").val('');
$("#passwordConfirm").val('');
alert('Data Saved...');
} else {
alert('there is an error to save data');
}
})
} else {
alert('Confirm password not matched');
}
}
});
</script>
<?php
$username = $_POST['username'];
$password = md5($_POST['password']);
// Do what ever you want with savadata.php
// if Data Saved Successfuly echo '1'; javascript will take this flag as your data is saved.
?>