1

I would like to compact all data from a huge HTML-form with over a 1000 variables to circumvent the max_input_vars limit in PHP versions before 5.3.9.

How can I read all data in the HTML-form with javascript, serialize it (or create json) to put it all in only one hidden field that contains the whole data then?

On the receiving side I would uncompress it with PHP (for example with json_decode)

1
  • Serialize the form using jQuery. Commented Sep 30, 2013 at 18:38

4 Answers 4

1

Just sent a ajax post?

form.html with javascript

<form action="process.php" method="post" id="form">
  <input type="text" name="name">
  <input type="text" name="username">

  <button type="submit" id="sendForm">Send</button>
</form>

<!-- YOUR JAVASCRIPT -->
<script type="text/javacript">
  $('#sendForm').click(function() {

    $.ajax({
      type: 'POST',
      url: $('#form').attr('action'),
      data: $('#form').serialize(),
      success: function(data) {

        // WHATEVER YOU WANT HERE

      }
    });

    return false;
  });
</script>

process.php

<?php
    $name = $_POST['name'];
    // other form fields here
}
Sign up to request clarification or add additional context in comments.

1 Comment

This will work, although the user will stay on the sending page with an ajax solution. If you want to build a fix into an existing framework with a lot of pages, where this has to be applied, my solution below will change the form and send only the serilized data to the original target page. Then you can just change the Header-php-script in your framework to receive and unserialize the data.
0

Serialize it using JQuery. You can then parse the URL string using PHP.

1 Comment

parse_url will not help, I guess you meant parse_str. But that will not help either, (see my answer above)
0

perhaps serialize and JSON.stringify may work together, though I have not tried it.

Comments

0

I created a script that does the job on all post forms automatically:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
// this disables all form elements and creates only one new element that contains all data serialized
$('form[method="post"]').submit(function() {
    var num_form_elements=$(this).find('input, select, textarea').not('[type="submit"]').length;
    var num_elements_already_disabled=$(this).find('input:disabled, select:disabled, textarea:disabled').length;
    enabled=(num_form_elements-num_elements_already_disabled);
    if($('textarea[name="serialized_data"]', this).length > 0) {
        alert("Backbutton is not supported yet!");
        return false;
    }
    if($('textarea[name="serialized_data"]', this).length > 0 || enabled<=0) {
        alert("Reload of the form is not supported yet!");
        return false;
    }
    var data=$(this).serialize();
    $(this).find('input, select, textarea').not('[type="submit"]').attr("disabled", true);
    $(this).append('    <input type="hidden" name="num_form_elements" value="'+num_form_elements+'">'); 
    $(this).append('    <input type="hidden" name="num_elements_already_disabled" value="'+num_elements_already_disabled+'">'); 
    $(this).append('    <textarea style="display:true" name="serialized_data">'+(data)+'</textarea>');
    // maybe in the textarea I have to .replace(/</g,'&lt;') ?

});
</script>

On the receiving side you cannot use the PHP parse_str() function because the max_input_vars directive affects this function too, so you need something else: I took my_parse_str() from https://gist.github.com/rubo77/6821632

<?php
    $params=my_parse_str($_REQUEST['serialized_data']);

    echo count($params,1)." serialized variables:<br>";
    var_export($params);
?>

Example script on https://gist.github.com/rubo77/6815945

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.