When using form validate alongside isset $_POST, page refreshes but will not submit

StackOverflow https://stackoverflow.com/questions/23547951

  •  18-07-2023
  •  | 
  •  

سؤال

first off thank you in advance. I am writing a sign up page in PHP and using Jquery Validate to check the form. It is returning errors when the form is filled out incorrectly, but when it is correctly filled out it is just refreshed and not completing the actions I have delegated in the isset $_POST function. Here is what I am dealing with:

PHP If form is not empty

    //Escape high risk symbols

    $pw= mysqli_real_escape_string($con, $_POST['pass']);
    $username= mysqli_real_escape_string($con, $_POST['pass']);
    $email = mysqli_real_escape_string($con, $_POST['email']);
    $sex = mysqli_real_escape_string($con, $_POST['sex']);
    $signedUp = date("Y-m-d H:i:s");
    $hash = password_hash($pw, PASSWORD_DEFAULT);

    echo 'Email: '.$email;
    echo 'Username: '.$username;
    echo 'Sex: '.$sex;
    echo 'Signed Up: '.$signedUp;
}
?>

Here is the Form

    <form method="post" class="form-signin" id="signup" name="signup">
                ...   
    </form>

Here is my validation javascript, it seems that it is not posting

<script src="js/formValidate.js"></script>
<script>
     // When the document is ready
     $(document).ready(function () {
         //validation rules
         $("#signup").validate({
             onkeyup: false,
             onfocusout: false,
             errorElement: "div",
             errorPlacement: function(error, element) {
                 error.appendTo("div#errors");
         },
         rules: {
             email: {
                 required : true,
                 email: true
                 //equalTo: "#example3-field2"
             },
             username: {
                 required: true,
                 minlength: 5
             },
             pass: {
                 required : true,
                 minlength: 5
             },
             cPass: {
                 required : true,
                 equalTo : "#pass"
             },
             sex: {
                 required : true
             },
         },
         messages: {
             email: {
                 required: "You must enter an email address",
                 email: "Enter a valid email address"
                 //equalTo: "Field 1 must be equal to Field 2"
             }, 
             username: {
                 required: "You must choose a username",
                 minlength: "Username must be a minimum of 5 characters"
             }, 
             pass: {
                 required : "You are required to enter a password!",
                 minlength : "Password must be at least 5 characters!"                                          
             },
             cPass : {
                 required : "You are required confirm your password!",
                 equalTo : "Passwords do not match"
             },
             sex : {
                 required : "You are required to choose a sex"
             },
         }, 
        submitHandler: function(form) {
            form.submit();
        }
    });             
});
</script>
هل كانت مفيدة؟

المحلول

I think the problem is that you're trying to see if the FORM is there (signup), you need to validate one of the fields, for example:

if(isset($_POST['email'])){

Since you seem to be trying to validate the form and that index will never be present in $_POST.

نصائح أخرى

You should use something like that to check if your request is a POST.

if($_SERVER['REQUEST_METHOD'] === 'POST') {
   // do your stuff.
}

related questions, Check whether a request is GET or POST

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top