1

Why doesn't the next code pass the "games" as an array to the php?

<script type="text/javascript">
    function buildAndSubmitForm(index){
        <?  $args = 't='.$team.'&s='.$season.'&l='.$league.'&f='.$flag;
            $inputs = '';
            foreach($games as $game)
                $inputs .= '<input type="checkbox" name="games[]" id="games[]" value="'.$game.'" />';
        ?>
        var form = '<?='<form name="myForm" id="myForm" action="scr'?>';
        form = form.concat(index);
        form = form.concat('<?='.php?'.$args.'" method="post">'.$inputs.'</form>'?>');
        $('#formDiv').html(form);
        $('#myForm').submit();
    }
</script>
<div style="display: inline">
    <div name="formDiv" id="formDiv" style="display: none;"></div>
    <a href="#" onclick="buildAndSubmitForm('a')">Home Stats</a>
    <a href="#" onclick="buildAndSubmitForm('b')">Visit Stats</a>
    <a href="#" onclick="buildAndSubmitForm('c')">Wins VS Losses</a>
    <a href="#" onclick="buildAndSubmitForm('d')">Home VS Visit</a>
    <a href="#" onclick="buildAndSubmitForm('e')">Overall</a>
</div>

I checked in the firebug, it seems to create a right form up until the submit, then it just doesn't pass the checkbox array.

1
  • 1
    Can you include a sample of the rendered form HTML? Commented May 10, 2011 at 17:49

1 Answer 1

5

You should give your checkbox the attribute checked. Otherwise it will not be submitted. This is specified in the HTML specs as a form-element that is not successful.

Sign up to request clarification or add additional context in comments.

4 Comments

What if I don't want my checkbox to be checked by default, but still want it to be successful?
@joon I'm not sure what you want to accomplish. If you want the value to be sent, even if the checkbox isn't checked you shouldn't use a checkbox. Use an hidden input or something.
I'm having some trouble with a checkbox not sending anything through post even when checked. I don't want to use the checked attribute in my input tag because I don't want it to be checked by default. stackoverflow.com/questions/9774653/…
a good way to do this is to have a hidden input with the same name directly before your checkbox and give that the off value, the checkbox value with overwrite it if checked otherwise the hidden field value will be sent

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.