Very Frankly Speaking

better than your blog.

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 so far

  1. Alex June 26th, 2008 11:08 am

    eval(”$$key = “$value”;”);
    What’s with the eval?
    $$key = (string) $value;
    would work just as well wouldn’t it?

    And are you using register globals?

  2. frankspastic June 26th, 2008 11:19 am

    You’re probably right, the eval() function could probably be done away with….  I think.  I’ll try it out.
    Sorry forgot to point out that i’m pulling in the variables from the POST with something like:

    foreach($_REQUEST as $key => $value){ $$key = $value; }

    That way I don’t have to refer to everything with $_POST[’blahvar’]

  3. rcantorii June 26th, 2008 9:10 pm

    Hi there,

    I have a javascript element and I want to pass it to a MySQL table, can you send me some “ligths” in order to do that by using jQuery?

    My idea is to assign this action to a button in an InfoWindowHtml where I get the attributes that I want to store in the database.

    Next it’s the code of the button:

    $(”#okPoly”).click(function() {
    var newPolygon = me.recreatePolygon(currentPolygon,g.lineColor,g.lineWeight,g.lineOpacity,g.currentcolor,g.fillOpacity,k);
    map.addOverlay(newPolygon);
    g.prmPolygons[k][1] = newPolygon;
    //this is how i think could be…
    $.post(”twbscripts/introducir.php”, {titlePoly: $(”#titlePoly”).val()});
    // $(”#titlePoly”).submit(”twbscripts/introducir.php”, {$titlePoly: $(”#titlePoly”).titlePoly});
    g.prmPolygons[k][2] = $(”#titlePoly”).val();

    Thank you in advance and I will appreciate any help…

    RCantor

  4. arne July 29th, 2008 10:50 am

    Hi, I’m currently writing a similar script, but ran into a problem. The ampersand is used as a divider between form elements, which is OK by itself, but what will happen when a user enters an ampersand into some form element? how can PHP distinguish from the user-entered ampersand and the devider ampersand? maybe the form should be checked for this kind of special characters before serializing?

  5. frankspastic July 29th, 2008 6:00 pm

    arne, i’m not 100% sure, but I think the serialize function turns those special characters such as the ampersand into the ASCII translation. Then between each variable it inserts an ampersand. I THINK.

Leave a reply

Mexico