pc-magas / phpquery

Automatically exported from code.google.com/p/phpquery
0 stars 0 forks source link

Behavior of serialize() does not match jQuery #185

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
The jQuery documentation says that this form:

<form>
  <div><input type="text" name="a" value="1" id="a" /></div>
  <div><input type="text" name="b" value="2" id="b" /></div>
  <div><input type="hidden" name="c" value="3" id="c" /></div>
  <div>
    <textarea name="d" rows="8" cols="40">4</textarea>
  </div>
  <div><select name="e">
    <option value="5" selected="selected">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
  </select></div>
  <div>
    <input type="checkbox" name="f" value="8" id="f" />
  </div>
  <div>
    <input type="submit" name="g" value="Submit" id="g" />
  </div>
</form>

Serializes to this string:

a=1&b=2&c=3&d=4&e=5

But if I run this PHP script:

require('phpQuery/phpQuery/phpQuery.php');

$doc = phpQuery::newDocument(<<<EOM
  <form>
    <div><input type="text" name="a" value="1" id="a" /></div>
    <div><input type="text" name="b" value="2" id="b" /></div>
    <div><input type="hidden" name="c" value="3" id="c" /></div>
    <div>
      <textarea name="d" rows="8" cols="40">4</textarea>
    </div>
    <div><select name="e">
      <option value="5" selected="selected">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
    </select></div>
    <div>
      <input type="checkbox" name="f" value="8" id="f" />
    </div>
    <div>
      <input type="submit" name="g" value="Submit" id="g" />
    </div>
  </form>
EOM
);

echo($doc['form']->serialize());
exit(0);

I get:

0%5Bname%5D=a&0%5Bvalue%5D=1&1%5Bname%5D=b&1%5Bvalue%5D=2&2%5Bname%5D=c&2%5Bvalu
e%5D=3&3%5Bname%5D=g&3%5Bvalue%5D=Submit&4%5Bname%5D=e&4%5Bvalue%5D=5&5%5Bname%5
D=d&5%5Bvalue%5D=4

Examining serializeArray() I see that the code builds a numerically indexed 
array with subarrays that have 'name' and 'value' fields, rather than building 
an associative array with keys and values. That is consistent with what jQuery 
does for serializeArray(), so I guess that's OK. However, serialize() is a 
wrapper around it which just calls param(), which calls http_build_query on the 
whole thing, resulting in the surprising string above. This does not yield a 
result compatible with submitting the original form as a browser would or as 
jQuery would.

To work properly serialize() must loop over the flat array of arrays returned 
by serializeArray() and build a query string using the names and values. Just 
calling http_build_query will not work, http_build_query is expecting an 
associative array.

I'll see if I can hit this and send in a patch.

(I am up to date with the svn trunk of phpQuery.)

Original issue reported on code.google.com by tommybgo...@gmail.com on 1 Jun 2011 at 8:47

GoogleCodeExporter commented 8 years ago
I have attached a patch that corrects the problem. With this patch serialize() 
output is similar to that produced by jQuery and would be valid as a form 
submission request body. The only difference from jQuery now is that submit 
buttons seem to be included, but that's an issue in serializeArray() which I 
did not alter.

Thanks very much for phpQuery which I am making a core component of a 
cross-site functional testing setup.

Original comment by tommybgo...@gmail.com on 1 Jun 2011 at 10:23

Attachments: