0

I have the following code, with which i try to dynamically pass my arguments between pages

<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.'" checked="checked" />';
        ?>
        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'.concat(index)).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>

The problem is that when i click at a certain speed (click on a link, then as it is getting submitted (like a half a second - second later) I click another link), my form gets submitted empty to the second link I've clicked. Why is this happening?

EDIT: Tryed to do the following instead:

<div style="display: inline">
    <a href="<?php echo "scra.php?t=$team&s=$season&l=$league&f=$flag"; foreach($games as $game) {echo "&game=$game"; }?>" >Home Stats</a>
    <a href="<?php echo "scrb.php?t=$team&s=$season&l=$league&f=$flag"; foreach($games as $game) {echo "&game=$game"; }?>" >Visit Stats</a>
    <a href="<?php echo "scrc.php?t=$team&s=$season&l=$league&f=$flag"; foreach($games as $game) {echo "&game=$game"; }?>" >Wins VS Losses</a>
    <a href="<?php echo "scrd.php?t=$team&s=$season&l=$league&f=$flag"; foreach($games as $game) {echo "&game=$game"; }?>" >Home VS Visit</a>
    <a href="<?php echo "scre.php?t=$team&s=$season&l=$league&f=$flag"; foreach($games as $game) {echo "&game=$game"; }?>" >Overall</a>
</div>

EDIT2: None of the solutions work yet.

Vadiklk
  • 3,404
  • 4
  • 25
  • 44

3 Answers3

0

Why are you using Javascript to concatenate text that was generated via PHP? Why not simply do:

<?php

$form = <<<EOL
<form name="myForm" id="myForm" action="scr.php?{$args}" method="post">{$inputs}</form>
EOL;

?>

<script ....>

var form = <?php echo json_encode($form) ?>;
$('#formDiv').html(form);

etc...

As well, if ANYTHING in your $args or $inputs variables in PHP contain quotes (single or double), most likely you're generating invalid HTML which is breaking your <form> tag.

Marc B
  • 340,537
  • 37
  • 382
  • 468
  • I am concatenating an index variable there. Look carefully. – Vadiklk May 10 '11 at 19:12
  • 1
    @Vadik: Yes, I see. But you're using Javascript to generate a form tag based on data filled in by PHP. Why not have PHP generate the whole thing to start with? It's like using a hammer to hit a screwdriver to turn a screw. – Marc B May 10 '11 at 19:13
0

Why don't you use query string (GET parameter instead of POST)?

If it's because of the checkbox (checked games), you can simply pass em with different query string (&games=baseball&games=football...). Your variable will be an array.

This way you won't even need a form.

Kraz
  • 6,281
  • 5
  • 38
  • 64
  • Didn't work, I am updating my question, tell me where am I wrong. – Vadiklk May 10 '11 at 19:17
  • Another thing, isn't the url limited? – Vadiklk May 10 '11 at 19:18
  • 1. What didn't work? 2. Yep, but it's pretty high iirc. 3. Did you consider disabling your links after an onClick, so that multiple clicks is not possible? – Kraz May 10 '11 at 19:21
  • if you use jquery... catch your click event with `$('a').onclick(function(){})` , have a var or something else checking if the form is submitting, and return false if you dont want it to work.... or just add a global var to your current script and check its value (`var isSubmitting = false; function buildAndSubmit(){ if (isSubmitting==true){return;}; isSubmitting==true; [...]`) – Kraz May 10 '11 at 19:30
0

If you want to prevent double-clicks, you need to disable the link after it's been clicked. Here is a previous SO question that provides an jQuery solution to disabling links:

jQuery disable a link

Community
  • 1
  • 1
AJ.
  • 25,364
  • 15
  • 77
  • 87