0

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. Want I want is for it to stay on signup.php as long as some as the fields are not complete.

I think the action has something to do with my problem.

Here is my 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

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.

Shadow
  • 33,525
  • 10
  • 51
  • 64

6 Answers6

3

In the forms, try to add the required attribute. 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

Douglas Hosea
  • 1,002
  • 13
  • 30
  • this is an *additional answer*. Somebody could still disable JS, so PHP validation is still required... – Jonas Wilms Apr 28 '17 at 12:28
  • @Jonasw, i do agree PHP validation is nice if you have to perform more checks and balances before submitting and you are not just looking for empty fields but otherwise why bother. – Douglas Hosea Apr 28 '17 at 12:33
  • empty *password* || *username* || *email* ... just doing JS validation can enable users to mess up your database/login system which isnt good... – Jonas Wilms Apr 28 '17 at 12:53
  • @Jonasw, the question was about a way of informing the user that some fields are empty, not if further validation is required beyond that. Take stock of my previous comment. – Douglas Hosea Apr 28 '17 at 13:00
1

Please add one more statement after echo in signupMysql.php

header("location:signup.php");

If you strictly want the validation to be done through PHP then you can use this code. Also if you want it to be done before the form is actually submitted you can use the code given by Douglas Hosea.

Hope it helps! All the best!

TutorialsBee
  • 101
  • 2
  • 5
  • if you add a way to show the 'fields missing' alert, this could be the best answer... – Jonas Wilms Apr 28 '17 at 12:27
  • Thanks @Jonasw for appreciating, As per the code written in the question, the developer is checking the condition for all the field at once and throwing a common alert dialog. If the requirement is to check what all fields are required, then we need to take a variable and check the condition for each field separately and append the variable with the proper error message. :) – TutorialsBee Apr 28 '17 at 12:32
1

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.

You can stop the form from submitting using JavaScript: How to prevent form from being submitted?

Or you can look into HTML5 form validation and the "required" attribute: Required attribute HTML5 https://www.w3schools.com/tags/att_input_required.asp

Community
  • 1
  • 1
Eeks33
  • 2,245
  • 1
  • 14
  • 17
1

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:

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()
}
1

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

<?php if(isset($_SESSION['messege']) &&  $_SESSION['messege'] != ''){
echo "<script type='text/javascript'>alert('$_SESSION['messege']');</script>";
}?>
Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33
1

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)

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

2oppin
  • 1,941
  • 20
  • 33
  • this is an *additional answer*. Somebody could still disable JS, so PHP validation is still required... Also please have a look at *window.onload* and *document.forms* – Jonas Wilms Apr 28 '17 at 12:31
  • @Jonasw, without javascript "alert" will not work either ) and validation on server is musthave, agree, but mostly for security purpose, not for the "informate user" purpose, also imo it's a good practice to reduce server calls as much as you can. – 2oppin Apr 28 '17 at 12:39