简体   繁体   English

onClick()执行功能?

[英]onClick() to perform function?

Is it possible for in a form of username and password to perform JS validation with the onClick()? 是否可以使用用户名和密码的形式通过onClick()执行JS验证? Here is the code: 这是代码:

<form action="signup.php" method="post" name="formInput" onclick="validateForm()">
    Username: <br> <input type="text" name="username" placeholder="Username"><br>
    Password  <br> <input type="password" name="pass" placeholder="Password"><br>
    <input type="submit" name="submit">
</form>

I remember doing it before but cannot remember where to call the actual onClick() in the form creation or in the button submission? 我记得以前做过,但不记得在表单创建或按钮提交中在哪里调用实际的onClick()?

Thanks for any suggestions. 谢谢你的任何建议。

<script>
function validateForm() {
   // if valid return true else false
}
</script>

<form action="signup.php" method="post" name="formInput" onsubmit="return validateForm()">
    Username: <br> <input type="text" name="username" placeholder="Username"><br>
    Password  <br> <input type="password" name="pass" placeholder="Password"><br>
    <input type="submit" name="submit">
</form>

As soon as the submit-button is clicked, signup.php gets executed and your validateForm() is ignored. 单击提交按钮后, signup.php将立即执行,您的validateForm()被忽略。 I do not know why you are trying to validate the data with JavaScript instead of inside the signup.php itself. 我不知道为什么您要尝试使用JavaScript而不是signup.php本身来验证数据。 Furthermore it is almost certainly a huge security-flaw to do the validation on the front-end. 此外,几乎肯定可以在前端进行验证是一个巨大的安全漏洞。

What I suggest you do is to forget about validating in JavaScript, and do it in PHP instead. 我建议您做的是忘记在JavaScript中进行验证,而在PHP中进行验证。 If you post your validateForm() code we can help you translating it into PHP (and probably improve it as well). 如果您发布validateForm()代码,我们可以帮助您将其转换为PHP(也可能会对其进行改进)。

Edit: You have stated that your validation is to make sure the input fields are not empty. 编辑:您已声明您的验证是确保输入字段不为空。 So here we go: 所以我们走了:

<?php
session_start();

$username = $_POST['username'];
$pass = $_POST['pass'];

if (strlen($username) < 1 || strlen($pass) < 1) {
    header(...); //redirect back to the home-page
    $_SESSION['message'] = "Username and password must be filled out.";
}
... //rest of your signup.php
?>

and change the extension of your HTML-page to .php, then add this to the top: 并将HTML页面的扩展名更改为.php,然后将其添加到顶部:

<?php
session_start();

echo("<script> alert('" . $_SESSION['message'] . "'); </script>");
?>

This seems like overkill for such a simple task, and probably is. 对于这样一个简单的任务,这似乎有点过头了,也许是。 However you will want to check for other things than the emptiness of the fields, and that has to be done in this way. 但是,您将需要检查除字段为空以外的其他内容,并且必须以这种方式完成。

However, if you do not care and want a simple way to do it, you can probably disable the submit-button with JavaScript somehow until both fields are filled. 但是,如果您不在乎并且想要一种简单的方法,则可以使用JavaScript禁用提交按钮,直到两个字段都填满为止。 Check if both fields are filled with something like onChange = validateForm() function. 检查两个字段是否都填充了onChange = validateForm()函数之类的内容。

解:

<form action="signup.php" method="post" name="formInput" onsubmit="return validateForm()"> Username: <br> <input type="text" name="username" placeholder="Username"><br> Password <br> <input type="password" name="pass" placeholder="Password"><br> <input type="submit" name="submit" onsubmit="validateForm()"> </form>

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

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