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

Anonymous’s picture

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!

sankatha’s picture

$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.

multimediavt’s picture

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?

<?php

function drinksearch_block($op = 'list', $delta = '', $edit = array()) {
  
  switch ($op) {
    case 'list':
      $blocks = array();
    $blocks['drinksearch-form'] = array(
        'info'       => t('Recipe search form block'),
        );
    $blocks['drinksearch-results'] = array(
        'info'  => t('Recipe search result block'),
        );
      return $blocks;
    case 'view':
    switch ($delta) {
    case 'drinksearch-form':
      return array(
        'subject' => t('Drink recipe search form'),
        'content' => drinksearch_contents(0),
      );
      break;
    case 'drinksearch-result':
      return array(
        'subject' => t('Search Results'),
        'content' => drinksearch_contents(1),
      );
    }
  }
}


function drinksearch_contents($which_block) {
  switch ($which_block) {
    case 0:
      
      return drupal_get_form('drinksearch_drinkdbsearch_form');

    case 1:
      
      return drinksearch_drinkdbsearch_results();
  }
}



function drinksearch_drinkdbsearch_form() {

$form['terms'] = array(
'#title' => 'Enter Name or Ingredients: ',
'#type' => 'textfield',
'#size' => '80',
'#default_value' => $terms,
);

$form['mode'] = array(
'#type' => 'radios',
'#title' => 'search by ',
'#default_value' => 'name',
'#options' => array('name' => t('name'), 'ingredients' => t('ingredients')),
);

$form['bycolor'] = array(
'#prefix' => '<h3>Narrow Search: </h3>',
'#type' => 'select',
'#title' => 'by color',
'#default_value' => '',
'#options' => array('' => t(''),'Red' => t('Red'),'Blue' => t('Blue'),'Green' => t('Green'),'Yellow' => t('Yellow'),'Orange' => t('Orange'),'Brown' => t('Brown'),'White' => t('White'),'Black' => t('Black'),'Purple' => t('Purple'),'Layered' => t('Layered'),'Clear' => t('Clear')),
);

$form['bytype'] = array(
'#type' => 'select',
'#title' => 'by type',
'#default_value' => '',
'#options' => array('' => t(''),'Blended' => t('Blended'),'Bomb' => t('Bomb'),'Classic' => t('Classic'),'Coffee' => t('Coffee'),'Cordial' => t('Cordial'),'Creamy' => t('Creamy'),'Frozen' => t('Frozen'),'Fruity' => t('Fruity'),'Hot' => t('Hot'),'Margarita' => t('Margarita'),'Martini' => t('Martini'),'Modern' => t('Modern'),'Morning' => t('Morning'),'Non Alcoholic' => t('Non Alcoholic'),'Shooter' => t('Shooter'),'Shot' => t('Shot'),'Sour' => t('Sour'),'Specialty' => t('Specialty'),'Strong' => t('Strong'),'Tiki' => t('Tiki'),'Vintage' => t('Vintage')),
);

$form['byglass'] = array(
'#type' => 'select',
'#title' => 'by glassware',
'#default_value' => '',
'#options' => array('' => t(''),'Beer Can' => t('Beer Can'),'Brandy' => t('Brandy'),'Champagne' => t('Champagne'),'Collins' => t('Collins'),'Coffee' => t('Coffee'),'Hurricane' => t('Hurricane'),'Martini' => t('Martini'),'Pint' => t('Pint'),'Pitcher' => t('Pitcher'),'Poco' => t('Poco'),'Rocks' => t('Rocks'),'Shooter' => t('Shooter'),'Shot' => t('Shot'),'Snifter' => t('Snifter'),'Tall' => t('Tall')),
);

$form['bystrength'] = array(
'#type' => 'select',
'#title' => 'by strength (higher number, stronger drink',
'#default_value' => '',
'#options' => array('' => t(''),'0' => t('0'),'1' => t('1'),'2' => t('2'),'3' => t('3'),'4' => t('4'),'5' => t('5')),
);

$form['submit'] = array(
'#prefix' => '<div style="float: right;">',
'#suffix' => '</div>',
'#type' => 'submit',
'#value' => t('Submit'),
);

return $form;

}


function drinksearch_drinkdbsearch_form_submit($form,&$form_state) {
	
	$terms = $form_state['values']['terms'];
    if($terms){preg_replace(' ',',',$terms); }else{ return;}
    if(!empty($form_state['values']['bycolor'])) $terms .= "," . $form_state['values']['bycolor'];
    if(!empty($form_state['values']['bytype'])) $terms .= "," . $form_state['values']['bytype'];
    if(!empty($form_state['values']['byglass'])) $terms .= "," . $form_state['values']['byglass'];
	addslashes($terms);
	
  return $terms;
}

function drinksearch_drinkdbsearch_results() {
	
	if(empty($terms)) {
		
		return;
    
    }else{
    
    db_set_active('drinks');
	
    $result = db_query("SELECT * FROM recipes WHERE MATCH(name) AGAINST('" . $terms . "')");
	
    //Display section
    
    while($row = db_fetch_array($result))

	{
		
		

		$drinkresults .= "<p><table width='384' cellpadding='6' border='0'>
  <tr>
    <td><h3>" . $row['name'] . "</h3>" . $row['ingredients'] . "</td>
    <td><center>" . $row['glassimgurl'] . "<br />" . $row['glass'] . "</center></td>
  </tr>
  <tr>
    <td colspan='2'>" . $row['instructions'] . "</td>
  </tr>
</table></p>";

	}
	
	return $drinkresults;
	
	db_set_active('default');
    
    }
		
}

	?>
Anonymous’s picture

You've got the following code in drinksearch_drinkdbsearch_results

if(empty($terms)) {
        
        return;
    
    }

$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

function drinksearch_drinkdbsearch_form_submit($form,&$form_state) {
    
    $terms = $form_state['values']['terms'];
    if($terms){preg_replace(' ',',',$terms); }else{ return;}
    if(!empty($form_state['values']['bycolor'])) $terms .= "," . $form_state['values']['bycolor'];
    if(!empty($form_state['values']['bytype'])) $terms .= "," . $form_state['values']['bytype'];
    if(!empty($form_state['values']['byglass'])) $terms .= "," . $form_state['values']['byglass'];
    addslashes($terms);
    
  $_SESSION['drinksearch_terms'] = $terms;
}

function drinksearch_drinkdbsearch_results() {
  $terms = $_SESSION['drinksearch_terms'];
  // Clear the session var if you don't want to display these results on subsequent pages
  unset($_SESSION['drinksearch_terms']);

  // The rest of the function...
}
multimediavt’s picture

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.

Anonymous’s picture

Just replace preg_replace with str_replace, it'll work perfectly in this context

multimediavt’s picture

...still not working....modified code for hard wired attempt to see if I can debug some more while trying to figure this out.

multimediavt’s picture

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?

multimediavt’s picture

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.

<?php

function drinksearch_block($op = 'list', $delta = '', $edit = array()) {
  
  switch ($op) {
    case 'list':
      $blocks = array();
    $blocks['drinksearch-form'] = array(
        'info'       => t('Recipe search form block'),
        );
    $blocks['drinksearch-results'] = array(
        'info'  => t('Recipe search result block'),
        );
      return $blocks;
    case 'view':
    switch ($delta) {
    case 'drinksearch-form':
      return array(
        'subject' => t('Drink recipe search form'),
        'content' => drinksearch_contents(0),
      );
      break;
    case 'drinksearch-result':
      return array(
        'subject' => t('Search Results'),
        'content' => drinksearch_contents(1),
      );
    }
  }
}


function drinksearch_contents($which_block) {
  switch ($which_block) {
    case 0:
      
      return drupal_get_form('drinksearch_drinkdbsearch_form');

    case 1:
      
      return print(drinksearch_drinkdbsearch_results());
  }
}



function drinksearch_drinkdbsearch_form() {
	
$drinkterms = $_SESSION['drinksearch-terms'];

$form['terms'] = array(
'#title' => 'Enter Name or Ingredients: ',
'#type' => 'textfield',
'#size' => '80',
'#default_value' => 'Ace',
);

$form['mode'] = array(
'#type' => 'radios',
'#title' => 'search by ',
'#default_value' => 'name',
'#options' => array('name' => t('name'), 'ingredients' => t('ingredients')),
);

$form['bycolor'] = array(
'#prefix' => '<h3>Narrow Search: </h3>',
'#type' => 'select',
'#title' => 'by color',
'#default_value' => '',
'#options' => array('' => t(''),'Red' => t('Red'),'Blue' => t('Blue'),'Green' => t('Green'),'Yellow' => t('Yellow'),'Orange' => t('Orange'),'Brown' => t('Brown'),'White' => t('White'),'Black' => t('Black'),'Purple' => t('Purple'),'Layered' => t('Layered'),'Clear' => t('Clear')),
);

$form['bytype'] = array(
'#type' => 'select',
'#title' => 'by type',
'#default_value' => '',
'#options' => array('' => t(''),'Blended' => t('Blended'),'Bomb' => t('Bomb'),'Classic' => t('Classic'),'Coffee' => t('Coffee'),'Cordial' => t('Cordial'),'Creamy' => t('Creamy'),'Frozen' => t('Frozen'),'Fruity' => t('Fruity'),'Hot' => t('Hot'),'Margarita' => t('Margarita'),'Martini' => t('Martini'),'Modern' => t('Modern'),'Morning' => t('Morning'),'Non Alcoholic' => t('Non Alcoholic'),'Shooter' => t('Shooter'),'Shot' => t('Shot'),'Sour' => t('Sour'),'Specialty' => t('Specialty'),'Strong' => t('Strong'),'Tiki' => t('Tiki'),'Vintage' => t('Vintage')),
);

$form['byglass'] = array(
'#type' => 'select',
'#title' => 'by glassware',
'#default_value' => '',
'#options' => array('' => t(''),'Beer Can' => t('Beer Can'),'Brandy' => t('Brandy'),'Champagne' => t('Champagne'),'Collins' => t('Collins'),'Coffee' => t('Coffee'),'Hurricane' => t('Hurricane'),'Martini' => t('Martini'),'Pint' => t('Pint'),'Pitcher' => t('Pitcher'),'Poco' => t('Poco'),'Rocks' => t('Rocks'),'Shooter' => t('Shooter'),'Shot' => t('Shot'),'Snifter' => t('Snifter'),'Tall' => t('Tall')),
);

$form['bystrength'] = array(
'#type' => 'select',
'#title' => 'by strength (higher number, stronger drink',
'#default_value' => '',
'#options' => array('' => t(''),'0' => t('0'),'1' => t('1'),'2' => t('2'),'3' => t('3'),'4' => t('4'),'5' => t('5')),
);

$form['submit'] = array(
'#prefix' => '<div style="float: right;">',
'#suffix' => '</div>',
'#type' => 'submit',
'#value' => t('Submit'),
);

return $form;

}


function drinksearch_drinkdbsearch_form_submit($form,&$form_state) {
	
	$drinkterms = $form_state['values']['terms'];
    if($drinkterms){str_replace(' ',',',$drinkterms); }else{ return;}
    if(!empty($form_state['values']['bycolor'])) $drinkterms .= "," . $form_state['values']['bycolor'];
    if(!empty($form_state['values']['bytype'])) $drinkterms .= "," . $form_state['values']['bytype'];
    if(!empty($form_state['values']['byglass'])) $drinkterms .= "," . $form_state['values']['byglass'];
	addslashes($drinkterms);
	
	$_SESSION['drinksearch-terms'] = 'Ace';
	
}

function drinksearch_drinkdbsearch_results() {
	
	$drinkterms = 'Ace';
	$drinkresults = "<p>" . $_SESSION['drinksearch-terms'] . "</p>";
	
	if($drinkterms) {
		
		db_set_active('drinks');
	
    $result = db_query("SELECT * FROM recipes WHERE MATCH(name) AGAINST('" . $drinkterms . "')");
	
    //Display section
    
    while($row = db_fetch_array($result))

	{
		
		

		$drinkresults .= "<p><table width='384' cellpadding='6' border='0'>
  <tr>
    <td><h3>" . $row['name'] . "</h3>" . $row['ingredients'] . "</td>
    <td><center>" . $row['glassimgurl'] . "<br />" . $row['glass'] . "</center></td>
  </tr>
  <tr>
    <td colspan='2'>" . $row['instructions'] . "</td>
  </tr>
</table></p>";

	}
	
	return $drinkresults;
	
	db_set_active('default');
	
    
    }else{
    
    	return;
    
    }
		
}

	?>
ohnobinki’s picture

db_set_active('default');

should be before the return $drinkresults, not after it.

      return print(drinksearch_drinkdbsearch_results());

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_submit by 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 $_GET from your search results function.