1

I am writing my own module and I would like to know how to create a URL from a search form that utilizes $_GET requests so I can pass those arguments into my query.

The Views module is a good example. For example, after you create a view and have exposed filters, your Views URL would look like:

CustomView?field_data_field_year=2012&field_data_field_month=december

Currently my hook_menu looks like:

function papers_pub_search_menu() {
  $items['papers_pub_search'] = array(
      'title' => 'Papers and Publications Search',
      'description' => 'Search Papers and Publications',
      'page callback' => 'papers_pub_search_viewer',
      'page arguments' => array(1,2),
      'access arguments' => array('access papers and pub search'),
      'type' => MENU_CALLBACK,
  );
  return $items;  
}    

My problem is the 'page arguments' => array(1,2). It seems like the format of the URL would be "foo/bar" and it would strictly need 2 arguments. My user may pass 1 or 3 arguments based on what parts of my form fields they fill out.

Another simple example, if I wanted to do this in pure PHP, it would be trivial:

<?php
if($_GET['submit']) {
  $name = $_GET['name'];
  $query = "SELECT name FROM user WHERE name = $name";
  ...
}
?>
<form action="form.php" method="get">
  <input type="text" name="name">
  <br>
  <input type="submit" name="submit" value="submit">
</form>

What I am trying to do is create a form where a user can fill out some fields, and then do some DB query based on what the user has inputted into various text fields and pull down choices once they hit submit.

1 Answer 1

3

If I understood your question correctly, you are mixing the URL arguments and query string.

You can have as many query string arguments as you want and you do not have to define them upfront. Drupal will not limit you. An example of query string is page?arg1=123&arg2=456, just like what views module does for filters. This is what you need for your $_GET form.

What you define in the hook_menu are URL arguments - for example one/two/three. You can get them by using the arg() function, so arg(0) in the example above would return one, arg(1) would return two etc.


UPDATE

So is the URL arguments in hook_menu not needed (i.e. page arguments)?

Yes, you don't need it in this case. You will solve everything by using query string (~question mark in the URL).

Does 'page?arg1=123&arg2=456' not count as an argument, or is that just one big argument (i.e. arg(0))?

That is only one argument. arg(0) would return just page. arg1 and arg2 are query string parameters.

Think of page argument as a part of the base URL for the page.
Query string is what goes after the question mark.

You should use filter_input() instead of simple $_GET as it is much more secure.

See also drupal_get_query_parameters().

6
  • So is the URL arguments in hook_menu not needed (i.e. page arguments)? Does 'page?arg1=123&arg2=456' not count as an argument, or is that just one big argument (i.e. arg(0))?
    – user785179
    Nov 13, 2012 at 20:33
  • Answer is updated. Nov 13, 2012 at 23:11
  • Thank you! So then, how do I construct the URL query? So for example, you noted 'page?arg1=123&arg2=456'. How do I get that URL constructed after my user hits the SUBMIT button? Is there something that needs to happen in hook_form_submit that passes these parameters via the URL? I don't want to pass arguments like 'page/123/456' because then I am bounded by the number of variables by that URL.
    – user785179
    Nov 13, 2012 at 23:37
  • Simplest way is to use $form['#method'] = 'get' while constructing the form. All elements will then be passed to the action URL as query string parameters. Nov 14, 2012 at 10:26
  • I got it to work, however, it appears to be passing along some unnecessary data in the URL, such as form_build_id, form_id, and token. Is there anyway not to pass those after a user hits the SUBMIT button?
    – user785179
    Nov 14, 2012 at 19:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.