JavaScript验证无法插入数据库
JavaScript Validation cannot insert into the database
好吧,我不得不重新表述我的问题,我的javascript代码无法进入下一步,即isset[‘submit’],它只是停留在那里。我真的希望这里有人能帮助我纠正我的javascript代码,这样它就能成功地调用isset并运行我的php代码将其添加到数据库中。
我的php代码
if(isset($_POST['submit'])){
$firstname = mysql_escape_string($_POST['fname']);
$lastname = mysql_escape_string($_POST['lname']);
$age = mysql_escape_string($_POST['age']);
$gender = mysql_escape_string($_POST['gender']);
$email = mysql_escape_string($_POST['email']);
$password = mysql_escape_string($_POST['password']);
$nickname = mysql_escape_string($_POST['nickname']);
$regflag = 0;
$sql = "SELECT * FROM accounts";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query))
{
if($row['email'] == $email)
{
echo '<b> The email is already taken!</b>';
$regflag = 0;
}
if($row['nickname'] == $nickname)
{
echo ' <b>The nickname is already taken!</b>';
$regflag = 0;
}
}
if($regflag == 1)
{
$regsql = "INSERT INTO accounts VALUES('','$firstname','$lastname','$age','$gender','$email','$password','$nickname','MEMBER',now())";
$query = mysql_query($regsql);
echo '<body onLoad = "regsucess()"> </body>';
}
}
我的javascript代码
<script type = "text/javascript">
function validator(){
if(!document.register.fname.value)
{
alert('You need to supply your first name.');
document.register.fname.focus();
return false;
}
if(!document.register.lname.value)
{
alert('You need to supply your last name.');
document.register.lname.focus();
return false;
}
if(!document.register.age.value)
{
alert('You need to supply your age.');
document.register.age.focus();
return false;
}
if(!document.register.email.value)
{
alert('You need to supply your email.');
document.register.email.focus();
return false;
}
if(!document.register.password.value)
{
alert('You need to supply your password.');
document.register.password.focus();
return false;
}
if(!document.register.nickname.value)
{
alert('You need to supply your nickname.');
document.register.nickname.focus();
return false;
}
register.action = "register.php"
document.register.submit();
//alert("Congrats!");
}
您应该在PHP代码中复制(至少)您使用的代码是Javascript,以便插入数据库。事实上,插入的代码应该更难——Javascript的东西是为了找出常见的问题(打字错误等),并给用户更好的体验。
只要确保Javascript代码没有PHP代码那么苛刻。
同意mplungjan的意见。
你能给我们看一下表单的html代码吗?也许提交输入的id与"submit"不同。
为什么register.action = "register.php"
而不是document.register.action = "register.php"
此外,如果您通过onclick提交按钮或通过onsubmit调用此命令,则删除最后一行document.register.submit();
最后,如果您表单中的任何东西都有name="submit"
,您将无法从脚本调用提交事件
以下是我希望看到的
<script type = "text/javascript">
function validate(theForm){
if(!theForm.fname.value) {
alert('You need to supply your first name.');
theForm.fname.focus();
return false;
}
if(!theForm.lname.value) {
alert('You need to supply your last name.');
theForm.lname.focus();
return false;
}
if(!theForm.age.value) {
alert('You need to supply your age.');
theForm.age.focus();
return false;
}
if(!theForm.email.value) {
alert('You need to supply your email.');
theForm.email.focus();
return false;
}
if(!theForm.password.value) {
alert('You need to supply your password.');
theForm.password.focus();
return false;
}
if(!theForm.nickname.value) {
alert('You need to supply your nickname.');
theForm.nickname.focus();
return false;
}
return true
}
</script>
使用
<form action="register.php" onsubmit="return validate(this)">
相关文章:
- 使用formData使用ajax将图像插入数据库
- 如何访问日期并将其插入数据库?节点.JS&MongoDB
- 使用Mysqli、PHP和AJAX的聊天系统正在将空白消息插入数据库
- AJAX PHP 连接将行插入数据库
- 使用 pur Html 和 Javascript 将文件插入数据库
- 将表单数据插入数据库后无法重定向到 html 页面
- 将整数变量插入数据库
- 单击按钮可多次动态添加选择框、文本框和日期,并将这些值插入数据库
- 动态选择列表AJAX和插入数据库表通过张贴形式
- 无法使用ajax序列化功能将图像插入数据库
- 使用ajax进行内联编辑(将值插入数据库)
- 将捕获的图像插入数据库问题(Cordova/SQlite/JavaScript)
- 如何在 PHP 中将动态表值插入数据库
- 将数据插入数据库express/mongodb应用程序时出错
- 通过API将视频信息插入数据库
- 我想单击一个
- 并获取 id 以通过 POST 将其插入数据库,可以吗?
- 如何将翻译后的文本插入数据库 php
- PHP-插入数据库而不刷新页面(OOP)
- MongoDB在将文档插入数据库之前为空集合
- 将本地存储值插入数据库