简体   繁体   English

在表单提交之前执行javascript

[英]javascript executing before form submission

I have a simple html form which takes in the username and password and registers it in a database. 我有一个简单的HTML表单,该表单接受用户名和密码并将其注册到数据库中。 However it executes the following php if statement once as soon as the page loads even before the form is submitted. 但是,它会在页面加载后甚至在提交表单之前立即执行以下php if语句。 The following is that piece of code. 以下是这段代码。

if (($_POST['reg_username']=="") || ($_POST['reg_password']==""))
{ ?>
<script> alert("please enter both email and password"); </script>

<? }

The full php code is down below. 完整的php代码在下面。 This is not the final code so I still did not encrypt the passwords and everything but I am just testing it out. 这不是最终的代码,因此我仍然没有对密码和所有内容进行加密,但我只是对其进行测试。 Thanks in advance guys. 在此先感谢大家。

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')");
}
}

?>

Why you have to include those tags inside PHP? 为什么必须在PHP中包含这些标签? Why you just don't do it separately. 为什么您不单独进行操作。 Here's an example with Javascript (jQuery): 这是Javascript(jQuery)的示例:

Note that there might be some typos and syntax errros since I wrote that snippet within minute. 请注意,由于我在几分钟之内编写了该代码段,因此可能会有一些错别字和语法错误。

<script type="text/javascript">

$(document).ready(function() {

("#login_form").submit(function() {

var username = ("#selector").val();
var password = ("#password").val();

if(username == "" && password == "") {

alert("please enter both email and password");

}
}

)};
</script>

Change this: 更改此:

if (($_POST['reg_username']=="") || ($_POST['reg_password']==""))
{ ?>
<script> alert("please enter both email and password"); </script>

<? }

Into this: 变成这个:

if(isset($_POST['reg_username']) || isset($_POST['reg_password'])) 
{
if (($_POST['reg_username']=="") || ($_POST['reg_password']==""))
{ ?>
<script> alert("please enter both email and password"); </script>

<?php }
}
 if(isset($_POST['SubmitButtonName'])){ //check if form was submitted
      unset($_POST['SubmitButtonName']);
      if (($_POST['reg_username']=="") || ($_POST['reg_password']==""))
      { ?>
           <script> alert("please enter both email and password"); </script>
      <? }
 }

Remember to change SubmitButtonName 记住要更改SubmitButtonName

      <script>
     $(document).ready(function(){
     $("#submit").click(function(){
    var register_name=$('#reg_username').val();
var register_password=$('#reg_password').val();

if(register_name =="")
{
alert("please enter your name");
return false;
} else if(register_password =="")
{
alert("please enter your password");
return false;
}
else
{
return true;
}
        });
       });
           </script>
        </head>
        <body>
          <form action="" method="post">
          Register name:<input type="text" name="reg_username" id="reg_username"><br>
       Password: <input type="password" name="reg_password" id="reg_password"><br>

            <input type="submit" id="submit">
         </form>
    </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM