简体   繁体   English

警报后,它将停留在同一页面上

[英]After alert it will stay on the same page

I need help on my sign up page. 我需要在注册页面上获得帮助。 I made an alert for the users to see that all the fields are required . 我提醒用户,请注意所有字段都是必填字段。 My problem is every time I click OK on the alert it redirects to signupMysql.php which is a blank page. 我的问题是,每当我在警报上单击“确定”时,它就会重定向到signupMysql.php,这是一个空白页。 Want I want is for it to stay on signup.php as long as some as the fields are not complete. 我想要的是,只要某些字段不完整,它就保留在signup.php上。

I think the action has something to do with my problem. 我认为该行为与我的问题有关。

Here is my signup.php 这是我的signup.php

<form class="register-form" action="signupMysql.php" method="post">
  <p class="message">Don't have an account?</p>
  <input type="text" placeholder="Username" name="username">
  <input type="text" placeholder="Last Name" name="lastname">
  <input type="text" placeholder="First Name" name="firstname">
  <input type="text" placeholder="Contact Number" name="contactnum">
  <input type="text" placeholder="Email address" name="email">
  <input type="password" placeholder="Password" name="password">
  <input type="password" placeholder="Re-enter Password" name="repassword">
  <input type="Submit" value="Sign Up">

</form>

some of the codes of my signupMysql.php 我的signupMysql.php的一些代码

if (empty($username) || empty ($lastname) || empty($firstname) || 
empty($contactnumber) || empty($email) 
|| empty ($password) || empty ($repassword)){

$message = "Fill all fields";
echo "<script type='text/javascript'>alert('$message');</script>";


}

I hope someone could help me. 我希望有人可以帮助我。 Thank you so much in advance. 提前非常感谢您。

In the forms, try to add the required attribute. 在表单中,尝试添加required属性。 For example; 例如;

<input type="text" placeholder="Username" name="username" required/>

This will prevent users from submitting empty values. 这将防止用户提交空值。

Also, alert() will not stop them as it just displays information and does not do anything else 另外, alert()不会停止它们,因为它仅显示信息并且不执行其他任何操作

Please add one more statement after echo in signupMysql.php 请在signupMysql.php中的echo之后再添加一条语句

header("location:signup.php");

If you strictly want the validation to be done through PHP then you can use this code. 如果您严格希望通过PHP进行验证,则可以使用此代码。 Also if you want it to be done before the form is actually submitted you can use the code given by Douglas Hosea. 另外,如果您希望在实际提交表单之前完成此操作,则可以使用Douglas Hosea给出的代码。

Hope it helps! 希望能帮助到你! All the best! 祝一切顺利!

Your problem is that your form is submitting and you are being taken to the next page where you have your validation. 您的问题是您的表单正在提交,您将被带到下一页进行验证。 If you don't want the user to leave the page when the form is not complete, you will need some form validation in the browser using JavaScript and/or HTML5. 如果您不希望用户在表单未完成时离开页面,则需要使用JavaScript和/或HTML5在浏览器中进行某些表单验证。

You can stop the form from submitting using JavaScript: How to prevent form from being submitted? 您可以使用JavaScript 阻止表单提交如何防止表单提交?

Or you can look into HTML5 form validation and the "required" attribute: Required attribute HTML5 https://www.w3schools.com/tags/att_input_required.asp 或者,您可以查看HTML5表单验证和“ required”属性: 必需属性HTML5 https://www.w3schools.com/tags/att_input_required.asp

I think you need something like this: 我认为您需要这样的东西:

<form class="register-form" id="myform" action="signupMysql.php" method="post">
...
  <input type="button"  onclick="subme()" value="Submit Form">
</form>

for the javascript code: 对于javascript代码:

function subme(){
    var inputs = document.getElementsByTagName('input');
    for (var i = 0; i < inputs.length; i += 1) {
        if(inputs[i].value == ''){
             alert("All field must be filled")
             exit()
        }
    }​​​​
    document.getElementById("myForm").submit()
}

Try below code its what you want. 尝试在下面编写所需的代码。

if (empty($username) || empty ($lastname) || empty($firstname) || 
            empty($contactnumber) || empty($email) 
            || empty ($password) || empty ($repassword))
        { 
           $_SESSION['messege'] = "Fill all fields";
           header("location:signup.php");
        }

In your signup.php 在您的signup.php中

<?php if(isset($_SESSION['messege']) &&  $_SESSION['messege'] != ''){
echo "<script type='text/javascript'>alert('$_SESSION['messege']');</script>";
}?>

You can validate form before posting to server. 您可以先验证表单,然后再发布到服务器。

Form "submit" event can be handled by javascript (you can google how to do it with jquery) 表单“提交”事件可以由javascript处理(您可以通过google搜索如何使用jquery)

here is the solution without any libs (add this code right after the form): 这是没有任何库的解决方案(在表单之后添加此代码):

<script>
document.getElementsByTagName('form')[0].addEventListener('submit', function(evt){
    if(document.getElementsByName("email")[0].value == "") {
       alert("You forgot to enter email!");
       evt.preventDefault();
    }
    /// .... put other checks here
})
</script>

Note: evt.preventDefault() will prevent your form from posting 注意:evt.preventDefault()将阻止您的表单发布

Use retrun false to stay on the same page unless its not validate 除非未经验证,否则使用retrun false保留在同一页面上

JavasScript: JavasScript:

<script>
function validation() {
 var isValid = true;
 //perform your validation on form and update isValid variable accordingly.

 if(isValid) {
    window.location = ''; // your desired location
 }

 return false; // always return false to prevent form from submission.
}
</script>

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

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