Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So this is probably a bit obtuse, but I made a function to help me do this very thing since I got tired of making a bunch of fixes every time. serializeArray is kind of annoying because it provides a collection of objects, when all I wanted to have PhP reconstruct was an associative array. The function below will go through the serialized array and will build a new object with the appropriate properties only when a value exists.</p> <p>Firstly, the function (it takes the ID of the form in question):</p> <pre><code>function wrapFormValues(form) { form = "#" + form.attr("id") + " :input"; form = $(form).serializeArray(); var dataArray = new Object(); for( index in form) { if(form[index].value) { dataArray[form[index].name] = form[index].value; } } return dataArray; } </code></pre> <p>When constructing my posts I also usually use an object since I usually tag on two or three other values before the form data and I think it looks cleaner than to define it inline, so the final step looks like this:</p> <pre><code>var payload = new Object(); //stringify requires json2.js from http://www.json.org/js.html payload.data = JSON.stringify(data); $.post("page.php", payload, function(reply) { //deal with reply. }); </code></pre> <p>Server-side all you have to do is <code>$payload = json_decode($_POST['data'], true)</code> and you have yourself an associative array where the keys are the names of your form fields. </p> <p>Full disclaimer though, multiple-selects probably won't work here, you would probably only get whichever value was last on the list. This is also created very specifically to suit one of my projects, so you may want to tweak it to suit you. For instance, I use json for all of my replies from the server.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload