0

This is a related question to: Validate Promo Code for one time use PHP/My SQLi

Everything is good and the database is now being updated on correct entries on the Sponsor Code section. I’m able to see the debug in Firebug and everything is in order.

What I’m having trouble with now is linking jquery.validate with the PHP. Don’t know if this even possible? Basically, now that the PHP is returning valid results, I want it to pass them through the validator?

Any tips would be appreciated.

Here’s the jquery function:

function validateSponsorship(){
var $form = $(this);
$("#sponsorship").validate({

    // Specify the validation rules
    rules: {
        name: "required",
        sponsorcode: "required",
        email: {
            required: true,
            email: true
        },   
    },

    // Specify the validation error messages
    messages: {
        name: "Please enter your name",
        sponsorcode: "Please enter your Sponsorship Code",
        email: "Please enter a valid email address"
    },
    submitHandler: function(form) {
        $.ajax({
            url: "../process/sendmailsponsorship.php",
            type: "POST",
            data: $(form).serialize(),
            success: function(response) {
                         $('#formholder').html("Thank you for registering to be a Friend Chips Sponsored Teacher!  We are sending you 10 Free Friend Chips packages as a thank you and so you can start using Friend Chips in your classroom!");
                     }            
             });
     }
   });
}

And the end of the PHP script after it has checked the database:

if (mysqli_affected_rows($link)) {
        // Sponsor code hasn't been used before and is valid
        echo("<script>$.validateSponsorship();</script>");
    } else {
        //show that the query was successful but that the client was not found
        print "This code is not valid or has already been used";
    }
Community
  • 1
  • 1
  • Please do not insert unrelated URLs in your posts. I do not assume that you're trying to spam, but questions on Stack Overflow should be reduced to the information required to reproduce the problem. – lxg Sep 17 '14 at 23:26
  • Sorry, was trying to give a reference. But understood. Thanks. –  Sep 17 '14 at 23:27
  • 1
    It's very unclear what you're asking here. jQuery Validate is client-side and done for a more pleasant user experience. PHP validation is done server-side to protect your database in case the user disables or bypasses the JavaScript. PHP creates the page on the server _before_ it's sent the browser and JavaScript manipulates the page _afterwards_. You can use both technologies, but technically, they don't get "connected" or "linked" to each other. – Sparky Sep 17 '14 at 23:30
  • I guess what I’m asking is how to prevent the form from being submitted if the “sponsorcode” is invalid/used? –  Sep 17 '14 at 23:34
  • It's blocked automatically by the jQuery Validate plugin. Please show the relevant ***rendered*** HTML as seen in the browser source code. – Sparky Sep 17 '14 at 23:43
  • Please see the example on the [tag wiki page](http://stackoverflow.com/tags/jquery-validate/info) to make sure the HTML of your form follows all the guidelines. Then simply make sure your PHP is creating the correct HTML markup. The jQuery Validate plugin only sees the rendered HTML output; it's totally irrelevant that the page was created by PHP. – Sparky Sep 18 '14 at 00:00
  • I think that is my issue. What should the PHP be writing on success and fail of the check? Right now I have return true and return false –  Sep 18 '14 at 00:31
  • I never saw your comment since you did not put my name in it. What are you asking? If you need to use PHP to check the database, then you need to use the `remote` method and you would `echo` `true` or `false` for pass or fail respectively. – Sparky Sep 18 '14 at 22:33

0 Answers0