注册页面,验证并将数据发送到php文件

时间:2015-10-03 15:13:55

标签: javascript php html forms

我是网络编程的新手,这可能是一个愚蠢的疑问,但仍然在问。

编辑了我之前的问题。我有Register.html页面和Welcome.php。在Register.html我有一个表单,如果没有输入用户名,则调用alert('输入名称')。否则,请转到Welcome.php页面打印他输入的用户名。

我的问题是,如果我没有输入用户名并仍然提交表单,则不会弹出警告消息。

**This is my Register.html code** 
<!DOCTYPE HTML>
<html>
<head>
<script>
function validation(){

if(document.getElementById('username').value=="")
{
    alert('Enter name');
}

</script>
</head>
<body>

<h1>Enter the details</h1>
<form method="post" action="Welcome.php">
<table >
<tr>
    <td>Username:</td>
    <td><input type="text" name="username" id="username"></td>

</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="pass" id="pass" ></td>
</tr>
<tr>
<td>Confirm Password:</td>
<td><input type="password" name="Repass" id="Repass" ></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" id="email"></td>
</tr>
<tr>
    <td></td>
    <td><input type="button" value="register" onclick="validation();"></td>

</tr>
</table>
</form>
</body>

</html>



**This is my Welcome.php**
<?php
echo "Welcome".$_POST['username'];
?>

1 个答案:

答案 0 :(得分:1)

  

您的回答似乎缺少回答问题所需的大量必需信息。但是根据你目前的问题和个人经验,我可能只是使用PHP来获得你想要的结果。

所以让我们说你的表格类似于这样......

<form action="" method="post" id="register">
    <input type="text" name="username" placeholder="Your username" />
    <input type="email" name="email" placeholder="Your email address" />
    <input type="password" name="password" placeholder="Your password" />
    <input type="submit" value="Join" />
</form>

我在之前的一个项目中尝试过做类似的事情,但最后我做了类似的事......

<?php if($register_success = 1) { ?>
<h1>Welcome <?php echo $_POST['username']; ?></h1>
<p>This is a static registration message with instructions on what to do further...</p>
<?php } else { ?>
<form action="" method="post" id="register">
    <input type="text" name="username" placeholder="Your username" />
    <input type="email" name="email" placeholder="Your email address" />
    <input type="password" name="password" placeholder="Your password" />
    <input type="submit" value="Join" />
</form>
<?php } ?>

并接收这样的输出使用PHP验证并设置$register_success的值,如此...     

//In no errors statement where you would register the user set the value of $register_success to 1

} else {
    //Register the user
    $register_success = 1;
}

我希望这个答案有助于你提出问题。