我有一个简单的html表单,它接受用户名和密码并将其注册到数据库中。但是,即使在提交表单之前,页面加载后,它也会立即执行以下php if语句。以下是这段代码。javascript在表单提交前执行
if (($_POST['reg_username']=="") || ($_POST['reg_password']==""))
{ ?>
<script> alert("please enter both email and password"); </script>
<? }
完整的php代码如下。这不是最终的代码,所以我仍然没有加密密码和一切,但我只是测试它。先谢谢你们。
if (($_POST['reg_username']=="") || ($_POST['reg_password']==""))
{ ?>
<script> alert("please enter both email and password"); </script>
<? }
$reg_username=mysql_real_escape_string($_POST['reg_username']);
$reg_username=htmlspecialchars($reg_username);
$reg_password=mysql_real_escape_string($_POST['reg_password']);
$reg_password=htmlspecialchars($reg_password);
if(!empty($reg_username) && !empty($reg_password))
{
if (isset($reg_username) && isset($reg_password))
{
mysql_select_db($db, $dbconnect);
mysql_query("INSERT into students(username,password) VALUES
('$reg_username','$reg_password')");
}
}
?>
的可能重复[回复ference:为什么我的Javascript中的PHP(或其他服务器端)代码无法工作?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side -code-in-my-javascript-not-wor) – Quentin