Ok, so I'm *NOT* trying to develop a module if I can avoid it, but it's looking bleak. I am trying to use the PHP input format to create a simple form for searching a secondary database of information that will then display themed results on the same page in another block, but I can't seem to get the form to work and pass info to another submit/display script. Even a simple example isn't working and the documentation here has been less than helpful in trying to figure this out.
Anyway, here's the code I've been playing with and not getting results from. AFAICT the form is getting and storing the data, but I can't seem to be able to do anything with the data once it's processed into the $form_state values! How do you get the info out to do something with it?!?! Am I totally missing something obvious that was in the docs? Is this just not possible? Code below:
<?php
function _drinkdbsearch($form_state) {
global $form;
$form['#submit'] = '_drinkdbsearch_submit';
$form['terms'] = array(
'#title' => 'Enter Name or Ingredients: ',
'#type' => 'textfield',
'#size' => '80'
);
$form['submit'] = array(
'#prefix' => '<div style="float: right;">',
'#suffix' => '</div>',
'#type' => 'submit',
'#value' => t('Search')
);
return $form;
}
print(drupal_get_form('_drinkdbsearch'));
function _drinkdbsearch_submit($form,&$form_state) {
global $form;
print($form_state['values']['terms']);
}
?>
Comments
I'm not sure it's possible,
I'm not sure it's possible, the _drinkdbsearch_submit() function won't exist in code until the block is built which would probably be after the form has been processed, so it'll never be called.
I know it's not the way you want to go but this really is the job of a custom module!
Well if I understood correct..
$form_state is not accesible outside a form. If you want to handle the data in a second form the easiest way is to store the $form_state in a session variable and access it wherever you need to. Hope this helps.
Ok...Module Time...
Alright, I did it. I made me a module, and it's still not working. :-( The submit script is being executed because it is throwing an empty argument error for the preg_replace, but there is something wrong with how I have the search form implemented in the other block OR I'm not using $form_state properly is my best guess at present. I've looked at a few examples and got me this far, but none of the tutorials or examples I've found have helped me figure out what's wrong here.
Any assistance?
You've got the following code
You've got the following code in drinksearch_drinkdbsearch_results
$terms is never declared or passed to the function so it will return every time. You need to store the terms in a session variable in drinksearch_drinkdbsearch_form_submit and pick them up in drinksearch_drinkdbsearch_results
Not where it's barfing
It's barfing not sending the data to the submit script...it errors on preg_replace which means it's not getting data into $terms to begin with...thanks, but that wasn't the problem...yet.
Just replace preg_replace
Just replace preg_replace with str_replace, it'll work perfectly in this context
[...still not working....] Error gone with str_replace...
...still not working....modified code for hard wired attempt to see if I can debug some more while trying to figure this out.
Hey...
Thought. Does the form and the results on the same page in different blocks pose a problem for the $form_state? The block with the form currently loads with a drupal_get_form each time the page loads, even after a submit. Any clue on how to get around that? Set or check the $form_state each time the page is loaded?
Still not working (current code)
Hmmmm...even hard wired it's still not kicking. I did a test and I know the external db search works as just a php input block when hard wired with a term, so something is really wonky here. All the code is there. I am running on 6.22(release).
Right now the blocks are on the same page. The client wants it to look like this, but would one block with both form and results be any better? Still not sure what I am missing. He'p me! Please.
Use a querystring perhaps
should be before the
return $drinkresults, not after it.is very wrong and shouldn't have the
print()call.One option is to redirect the user to a URL with a querystring from within
drinksearch_drinkdbsearch_form_submitby setting$form_state['redirect']. This ensures that page caching can work. Or you could just set the form's method to 'get' with$form['#method'] = 'get';so that the form is directly submitted to a query string and then poke at$_GETfrom your search results function.