1

I am submitting a form which contains a text input and file input to a php page using Jquery and Ajax, but before the submission, I would want to validate on the text input before I submit.

Below is the form page

            <form id="registrationForm"  method="POST"  name="registrationForm" enctype="multipart/form-data">

                <input type="text"  name="firstName" id="firstName" />

                <input type="file" name="file" id="file" />

                <button type="submit"> UPLOAD</button>

            </form>

and the Javascript page

 $('#registrationForm').validate({
        rules: {
            firstName:{
                required: true
            }
        },
        message:{
            firstName: {
                required: "Please Enter Name"
            }
        },


    submitHandler : function(){

      //grab all form data  
      var formData = new FormData(this);

      $.ajax({
        url: 'submitPage.php',
        type: 'POST',
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
          success: function(data) {  
            $('#message').html(data);  
            $('#message').html(data).hide().fadeIn(1500,function(){$('#message')});
            } 
        });

    }
    });

And PHP Page which is the receiving page

        <?php
            $firstName=$_POST['firstName'];
            $file=$_FILES['file']['name'];

            echo $firstName;
            echo $file;

         ?>

This returns null on the php receiving page. I really wish if somebody could help me out. Thanks.

1

1 Answer 1

1
$('#registrationForm').validate({
        rules: {
            firstName:{
                required: true
            }
        },
        message:{
            firstName: {
                required: "Please Enter Name"
            }
        },


    submitHandler : function(){

      //grab all form data  
      var formData = new FormData(document.querySelector("form"));

      $.ajax({
        url: 'submitPage.php',
        type: 'POST',
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
          success: function(data) {  
            $('#message').html(data);  
            $('#message').html(data).hide().fadeIn(1500,function(){$('#message')});
            } 
        });

    }
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.