Very Frankly Speaking

better than your blog.

Archive for the 'javascript' Category

What I like about Javascript frameworks

The thing I like about Javascript frameworks so far is that I don’t seem to be having the same kind of browser compatibility issues I used to have when I used to write most of my Javascript from scratch.  All of the browser incompatibilities have been abstracted to me so I don’t have to worry about whether or not things work in IE6, IE7, Firefox..etc FOR THE MOST PART.

No comments

Handling jQuery serialize() on the backend with PHP

My goal for this one situation is to validate all my form data with my backend PHP code through AJAX. If all the checks on my form don’t go through successfully, the page will not go to the next page specified in the HTML form’s “action” attribute.

On the Javascript side I use the serialize() function to take all my form data and pass it through to action.php.

$("#submit-form").submit( function(){ var data = $(this).serialize(); $.post( "action.php", { action: "ajax_verify_form", data: data }, function(response){ alert(response); return false; } ); return false; } );

On the PHP side:

if($action == "ajax_verify_form"){ $fields = explode("&",$data); foreach($fields as $field){ $field_key_value = explode("=",$field); $key = urldecode($field_key_value[0]); $value = urldecode($field_key_value[1]); eval("$$key = "$value";"); } }

Now I have all the same variables from the client side on the PHP side. Now you can do whatever form-checking you need with PHP.  If you have an error, echo the error so the callback function on the jquery.post() function will catch it.

5 comments