简体   繁体   中英

jQuery pop-up with php form validation

i have a registration page

<form class="register" action="regist.php" method="post" >
                    <h3>Register</h3>
                    <div class="column">
                        <div>
                            <label>First Name:</label>
                            <input type="text" name="f_name"/>
                            <span ></span>
                        </div>
                        <div>
                            <label>Last Name:</label>
                            <input type="text" name="l_name"/>
                            <span class="error">This is an error</span>
                        </div>
                        <div>
                            <label>mobile:</label>
                            <input type="text" name="mobile"/>
                            <span class="error">This is an error</span>
                        </div>
                    </div>
                    <div class="column">
                        <div>
                            <label>Username:</label>
                            <input type="text"name="user"/>
                            <span >This is an error</span>
                        </div>
                        <div>
                            <label>Email:</label>
                            <input type="text" name="email"/>
                            <span class="error">This is an error</span>
                        </div>
                        <div>
                            <label>Password:</label>
                            <input type="password" name="pass"/>
                            <span class="error">This is an error</span>
                        </div>
                    </div>
                    <div class="bottom">
                        <div class="remember">

                        </div>
                        <input type="submit" value="Register" name="submet" />
                        <a href="index.html" rel="login" class="linkform">You have an account already? Log in here</a>
                        <div class="clear"></div>
                    </div>
                </form>

that use jquery bpopup

   $(function() {

        // Binding a click event
        // From jQuery v.1.7.0 use .on() instead of .bind()
        $('#my-button').bind('click', function(e) {

            // Prevents the default action to be triggered. 
            e.preventDefault();

            // Triggering bPopup when click event is fired
            $('#element_to_pop_up').bPopup();

        });

    }

so when some one click register the form will pop up every thing work fine the problem is that i cant do validation to the form because when i click register after filling the form the pop up will disappear so i cant make the validation on the same form

i tried 1 to make another pop up that show the error but the data wont pass to the new popup 2 to make the action $_SERVER['self_request'] can some one help me withe this please i have a project for my college i nee to do it in 3 dayes

what i need is a validation for the form in the same page or another popup before send it to the MySQL script

Make an php page ( here called validation.php ) that uses the GET function to get the info...

Then set some ID's on the input fields...

And then make an function like this:

function checkAll(){
    var firstName = document.getElementById("f_name").value();
    var lastName = document.getElementById("l_name").value();
    $.get( "validation.php?f_name=" + firstName + "&l_name=" + lastName, function( response ) {
        // console.log( response ); // server response
        response = response.trim();
        if(response == 1){
            alert("Everything is alright");
        } else{
            alert(response);
            return false;
        }
    });
}

And change your form to this:

<form class="register" action="regist.php" method="post" onSubmit="checkAll();">

Now the key is that in the validation.php ,if everything works fine then you can echo value 1, and when something is not alright, you echo the error code ( or any code, and then with an switch clause you can dynamically add the error to your form).

The return false prevents the form from submitting...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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