I am trying to build a dealer locator module, I have the Control Panel portion built. I am having a hard time building the front end(mod.dealer_locator.php) file.
I have the code below in a template now and it renders the form, I am having trouble figuring out how to handle the posted data, and return a response back to the template in a way that is usable.
{exp:dealer_locator:form}
<input type='text' name='postal_code' />
<input type='submit' name='search' value='submit' />
{/exp:dealer_locator:form}
I am looking for any help or resources I can get at this point, I have gone through the documentation and followed the Fortunes example as far as it could go. So anything is appreciated!
Thanks, Jeremy
Also I have attached an copy of the current mod.dealer_locator.php file hopefully that will provide more insight as to what I am trying to do?
So I have made a little bit of progress, but I still need some help.
When the dealer_locator form is submit it calls the query_dealers function (shown below), this will execute the query and does return results, but I am getting stuck getting that data into the show_results function ( also below ).
I have all of the parts working separately of each other but I cannot seem to figure out how to tie it all together?
I have these two PHP functions in my mod. file.
function query_dealers(){
global $FNS, $DB, $TMPL, $SESS, $IN;
$postal_code = $IN->GBL('postal_code');
$sql = "SELECT * FROM exp_dealer WHERE dealer_postal = ".$postal_code;
$query = $DB->query($sql);
return $FNS->redirect($_POST['RET']);
}
and
function show_results(){
global $FNS, $DB, $TMPL, $IN, $SESS;
/* this is where i show the results in the page and return them to the template */
// //convert each row into a template variable
// foreach($query->result as $row){
// $tagdata = $TMPL->tagdata;
//
// foreach ($TMPL->var_single as $key => $val)
// {
// if (isset($row[$val]))
// {
// $tagdata = $TMPL->swap_var_single($val, $row[$val], $tagdata);
// }
// }
//
// $this->return_data .= $tagdata;
// }
// return $this->return_data;
}
in my templates i have this code:
{exp:dealer_locator:form submit_type="refresh"}
<label for="postal_code">Postal Code</label>
<input type="text" name="postal_code" value="" id="postal_code" />
<input type="submit" name="search_dealers" value="Search" id="search_dealers">
{/exp:dealer_locator:form}
and
{exp:dealer_locator:show_results}
{dealer_name}
{dealer_address}
{dealer_city}
{dealer_phone}
<hr >
{/exp:dealer_locator:show_results}
If you’re redirecting following your submission action, you’ll need some way for the subsequent request to know what’s being shown. The native Search module shows one possible method, saving the results to the database on the submission, and including a hash segment in the URL so the results page can identify what record of results from the database to display.
Another option is more akin to the Weblog module’s dynamic form parameters, where the form submission goes directly to the display page, and the tag itself checks for allowed fields in POST data to determine what to display. Base on what you’re doing, this is probably the approach I’d go for, personally, as it will allow you more flexibility. For instance being able to handle a URL with additional segments, such as a Zip Code, which would also display the results, without having separate methods and actions handling the requests.
Make sense?
Sadly, I don’t think its all making sense to me.
I have changed the form so that it will submit back to itself, and I am trying to handle both the display of the results and the display of the form in the same function.
When the form submits I am able to get the post data, and get the query executed, but there is no template available at all. All I can get to show up is a print_r($query);
I looked through the weblog module but I don’t see anything that appears to be doing anything like what I am trying to do, which is probably due to the fact that I am very new to EE.
Thanks for your reply and your help, if you need more info to better help or understand what I am doing here I would be more than happy to provide you with a link or some more code, what ever helps.
template code:
{exp:dealer_locator:form submit_type="refresh" results_page="site/dealer-locator" }
<label for="postal_code">Postal Code</label>
<input type="text" name="postal_code" value="" id="postal_code" />
<input type="submit" name="search_dealers" value="Search" id="search_dealers">
{/exp:dealer_locator:form}
form function inside the mod file.
function form(){
global $IN, $TMPL;
if(! $IN->GBL('search_dealers')){
$r = $this->return_form();
return $this->return_data = $r;
}
else{
$query = $this->query_dealers();
print_r($query);
// I THOUGHT I WOULD BE ABLE TO DO SOMETHING LIKE THE FOLLOWING?
//convert each row into a template variable
// foreach($query->result as $row){
// $tagdata = $TMPL->tagdata;
//
// foreach ($TMPL->var_single as $key => $val)
// {
// if (isset($row[$val]))
// {
// $tagdata = $TMPL->swap_var_single($val, $row[$val], $tagdata);
// }
// }
//
// $this->return_data .= $tagdata;
// }
// return $this->return_data;
}
}
Thanks again, Jeremy
Thanks for your fast reply, I really appriciate the extra help.
$TMPL->tagdata doesn’t hold anything, the $TMPL object doesn’t even exist the point where the print_r(); does. If I print_r($TMPL->tagdata); this is what I get…
Notice: Trying to get property of non-object in modules/dealer_locator/mod.dealer_locator.php on line 28
Line 28 being the line that print_r() is on…
Again thanks, Jeremy
I am, this is what I have for the form:
$hidden = array(
'ACT' => $FNS->fetch_action_id('Dealer_locator', 'form'),
'URI' => ($IN->URI == '') ? 'index' : $IN->URI,
'RET' => $this->form_action,
'XID' => ( ! isset($_POST['XID'])) ? '' : $_POST['XID'],
);
$arr = array(
'hidden_fields' => $hidden,
'secure'=> TRUE,
'action'=> $this->form_action,
'id'=> 'dealer-locator-search-form'
);
$r = $FNS->form_declaration( $arr );
$r .= stripslashes($tagdata);
$r .= "</form>";
You will not want to use an ACT request unless you want to go down the road of the Search module, where you store results in the db, redirect to a location with an identifier in the URL, and then display. You just want the form to submit directly to a “page” request in EE. “Action” requests are just for “doing”, taking action, and completely bypass the template parser and page rendering routines.
Ok, so I was off from the beginning! thanks I’ll make these changes, I guess I just assumed that I had to use the ACT and Fetch Action Id and all that.
This is, if you could tell my first attempt to extend EE, so far with the exception of this last little issue I really like Expression Engine and I will be pushing to use it for a lot more of our clients, especially if I can become proficient in module/extension development…
Thanks again, and I will make these changes and post the results…
Using an ACT for this particular type of form submission is still acceptable, but it would need to be approached differently. This is the normal way you’d take a form submission if there was quite a bit of complexity with accepting the submission, or when a redirect afterward is appropriate (search module, stand alone entry form, comment submission, etc.) Those type of submissions don’t rely on the user to have tags on the landing page to handle the request. But for this add-on, I think the simpler approach of having the tag itself parse the contents is fine. You just need to make sure that the form action always goes to a page with the necessary tag on it.
Looking forward to seeing the results!
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.