好吧,我不得不重新解释我的问题,我的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!");
}
答案 0 :(得分:0)
为什么
register.action = "register.php"
而不是
document.register.action = "register.php"
??
如果你通过提交按钮或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)">
答案 1 :(得分:0)
您应该复制(至少)您在PHP代码中使用Javascript的代码以插入数据库。事实上,插入的代码应该更加强硬 - Javascript的东西是接收通常的嫌疑人(错别字等)并为用户提供更好的体验。
确保Javascript代码不如PHP代码那么苛刻。
答案 2 :(得分:0)
同意mplungjan。
你能告诉我们表单的HTML代码吗?也许提交输入的ID不同于“提交”。