3

I'm trying to pass URL variables to my PHPExcel PHP script, so that i can extract them in the script using the $_GET array.
The call to the PHPExcel script happens when a form (form1) button is pressed, and the variables are based on a second form (filters_form).

{filters_form is a general "filters" form that serves different scripts, for example filtering a JqGrid. meaning to say, it is separated from form1 for a reason. form1 is where i placed a button that calls the PHPExcel script, which also need to use the same filters that the filters_form provide}

Thus, I'm trying to use Jquery to call the PHPExcel script like that:

$(document).ready(function() {
  $('#form1').click(function() {
    var filters_form_serialized = $('#filters_form').serialize();
    var URL = 'PHPExcel_script.php?' + filters_form_serialized;
    $('#form1').attr('action', URL);
    $('#form1').submit();
  });
});

form1 is:

<form id="form1" action="" method="get" target="_blank">
 <button id="form_button" type="button"></button>
</form>

filter_form is:

<form id="filters_form">
 <input name="input1" type="checkbox" value="0" checked="checked" />
 <input name="input2" type="checkbox" value="1" checked="checked" />
</form>

The PHPExcel script indeed gets called, BUT the script doesn't recognize/gets any of the variables in the $_GET array, e.g.:

if(isset($_GET['input1']))
  {echo "TRUE";}
else
  {echo "FALSE";}

returns FALSE.
any thoughts why?
Thanks!

P.S. i've checked using alerts that the URL is encoded correctly; moreover the exact same url with same variables is recognized correctly using other scripts.

Israel
  • 1,184
  • 2
  • 13
  • 26
  • @Isael: Yeah, seen it. Have you tried w/o the serialize? I mean, the form's action is get, so there is no need to do that. – hakre Oct 13 '11 at 12:33
  • @hakere - actually i tried to make things simpler in the question but i basically made it less clear. let me edit it for a minute and things will get clearer. thanks :) – Israel Oct 13 '11 at 12:38
  • @hakre - i've edited the question, i will appreciate if you take another look. thanks! – Israel Oct 13 '11 at 12:50

2 Answers2

2

What you try to achieve might have seemed possible you, but it is not:

The query parameters from the action attribute are dropped by your browser. I think that was not obvious to you.

Luckily, you don't need that.

Instead just set a new location:

$(document).ready(function() {
  $('#form1').click(function() {
    var filters_form_serialized = $('#filters_form').serialize();
    var URL = 'PHPExcel_script.php?' + filters_form_serialized;
    window.location.href = 'http://your.host/and/path/to/' + URL;
  });
});
Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • thank you so much, exactly what i needed! though i'm wondering why the browser drops the query parameters? – Israel Oct 13 '11 at 13:23
  • 1
    @Israel: Because it takes the ones from the form elements. This works for forms with `method="post"`. See [submitting a GET form with query string params and hidden params disappear](http://stackoverflow.com/questions/1116019/submitting-a-get-form-with-query-string-params-and-hidden-params-disappear) – hakre Oct 13 '11 at 13:28
1

I don't understand why don't you simply do:

<form id="the_form" action="PHPExcel_script.php" method="get" target="_blank">
 <input name="input1" type="checkbox" value="0" checked="checked" />
 <input name="input2" type="checkbox" value="1" checked="checked" />
 <button id="form_button" type="button"></button>
</form>

  $('#form_button').click(function() {
    $(this).submit();
  });

which should submit the form correctly. You were setting the action of the form with parameters. No need to serialize anything i think.

Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192