Passing values ​​from jQuery to PHP

Here's the situation:

I am using jquery.validate.js to validate my forms, but I need to add a server side validation layer. jQuery Validate uses class names on form elements to determine what it should do - I want to get those class names in PHP so I can do the same server side validations.

I hope to do something like this:

foreach($_POST as $key=>$value) {
     $class=array(...// get this #key element class name list with jQuery
     $error=...// based on the class names, perform specific PHP validations
     if($error=="")
         return true;
     else
         return false;
}

      

Question: can i do this? Can I get the w / jQuery class names and pass them to PHP?

+1


a source to share


5 answers


You can get jQuery to submit class names along with the form - HOWEVER this introduces serious security issues. By seeing that everything sent to your server is indeed done by your users' browsers, your users will be able to change your validation rules and, as such, bypass them, and this will render your goal completely useless. Instead, I suggest the following.

Make a function in php to take the name of the form element and return the corresponding class names. When creating each form element for the form, call this function to get the class names (and then apply jQuery validation). When you read your form in your PHP code, use the same function to get the class names and then you know what validation rules should be applied in the php code on the server.



Hope this made sense.

+2


a source


Never trust the client (browser)!

You should never let anything on the client tell the server how to validate the posted information.



This is a very bad idea.

+2


a source


on the client side:

function validate()
{
    var params = {
       var1: {key:$('#field1').get(0).className, value:$('#field1').get(0).value},
       var2: {key:$('#field2').get(0).className, value:$('#field2').get(0).value},    
       ...
    }
    $.post('http://host/validate_script.php', params, onValidate);
}
function onValidate(data)
{
    alert(data);
}

      

hope this helps ... I'm sure this is an easy way and you can make it more formalized.

+1


a source


serializeArray converts your form data to JSON:

var json = $('#formid').serializeArray();

      

You can usually send the entire JSON string to the server, which can take it from there.

0


a source


As mentioned above, you can use AJAX and JSON to pass values ​​to PHP. However, this will not provide more reliable validation than your regular JS validation (since your PHP will still depend on your JS)

If you decide to use this method, here are some improvements to the script provided earlier by Evgeny Savichev

<script type="text/javascript">
params = {
    elementName : {
        className : $('elementId').attr('class'),
        elementValue : $('elemenetId').val()
    },
    anotherElement : {
        //etc
    }

}

$.post('http://host/validate_script.php', params, onValidate);

function onValidate(data)
{
    alert(data);
}
</script>

      

However, the best solution is to automatically create and validate form elements. Zend Framework has a great class for this. I've included a simplified version of how something might look like if you decide to write your own script.

I hope this can help you.

  • Wim

    $elements = array(
        'email-field' => array('email', 'required'),
        'integer'  => array('integer')
    );
    
    if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
        $error = false;
        foreach($elements as $elementName => $validators) {
            if (array_key_exists($elementName, $_POST)) {
                foreach ($elements[$elementName] as $validator ) {
                    switch($validator) {
                        case 'email':
                            if (filter_input(FILTER_VALIDATE_EMAIL, $elementValue)) {
                                $error = true;
                            }
                            break;
                        case 'integer':
                            // etc
                            break;
                        default :
                            break;
                    }
                }
            } else {
                if ( in_array('required', $validators) ) {
                    $error = true;
                }
            }
        }
        if ( $error ) {
            // etc
        }
    }
    ?>
    
          

0


a source







All Articles