Forums

How to Pre-Load a form field based off URL

hicksticks2001 19 Aug, 2008
I have a contact form at:
http://www.losbanostesting.org/index.php?option=com_chronocontact&Itemid=7

Everything works great, however I would like to add some more functionality. In the contact form, I have a subject line where you decide what City Department you want to send the e-mail to.

How do I pre-load that selection based off of the URL? This way, when I am at the Recreation Department section of the web site, they can click on a link and can be taken to the "Contact US" form and already have Recreation pre-loaded.

It seems simple enough.... is it?
GreyHead 19 Aug, 2008
Hi hicksticks2002,

The parameters in the url are available in the $_GET array, you can simply access the parameter and use a little look up table in your form html to pre-load the required info.

In Joomla 1.0.x you can use $_GET['field_name'], in Joomal 1.5.x you need something like:
$field =  JRequest::getVar('field_name','', 'get', 'string', '' );

Bob
hicksticks2001 20 Aug, 2008
Bob, thank you for your response. Your time is very much appreciated. I appologize for my ignorance, but I am going to take this one step further in asking you to explain the steps to me. So understand that the line below is the code I am going to use.


$field =  JRequest::getVar('field_name','', 'get', 'string', '' );


Now this is the HTML for my form field I would like to preload:
<td>Subject:<font color="red">*</font></td>
    <td><label>
      <select name="Subject" id="Subject" tabindex="10" >
      <option value="Select">Select One...</option>
        <option value="jana.sousa@losbanos.org">City Administration</option>
        <option value="melinda.wall@losbanos.org">Billing Department</option>
        <option value="Building_Department@losbanos.org">Building Department</option>
        <option value="Commissions@losbanos.org">Commissions</option>
        <option value="norma.fuentes@losbanos.org">Community Development</option>
        <option value="melinda.wall@jsanet.com">Finance Department</option>
        <option value="marylou.gilardi@losbanos.org">Fire Department</option>
        <option value="helpdesk@losbanos.org">Information Technology</option>
        <option value="lucy.mallonee@losbanos.org">Personnel/HR Department</option>
        <option value="denise.rocha@losbanos.org">Police Department</option>
        <option value="claire.hughes@losbanos.org">Public Works</option>
        <option value="peggy.stanfill@losbanos.org">Recreation</option>
        <option value="hixson@losbanos.org">Web Administration</option>
        <option value="rick.spalding@losbanos.org">General</option>
      </select>
    </label></td>
  </tr>


Now this is my current URL, how can I modify it create variables for my form to get?
http://www.losbanostesting.org/index.php?option=com_chronocontact&Itemid=7

Also, how do I modify the HTML so that your line of code works with that form? sorry for the specific questions, but I have very little experience with this sort of thing.
GreyHead 21 Aug, 2008
Hi hicksticks2001,

First off, it's not good practice to include email addresses on a webpage (with the possible exception of a closed intranet). It's also going to make our task easier if we put the email addresses into the OnSubmit code.

For the html code I'd use something like this:
<select name="subject" id="subject" tabindex="10" >
      <option value="">Select One...</option>
<?php
$dept_array = array('city_admin' => 'City Administration', 'billing' => 'Billing Department', 'building' => 'Building Department', . . ., 'general' => 'General');
foreach ( $dept_array as $key => $value ) {
  echo "<option value='$key' >$value</option>";
}
?>
</select>
<input type="hidden" value="" />

The section in <?php tags just loops through the dept array generating an option for each entry. If the department list ever changes then you can just change the array entries. The hidden entry is just a placeholder for our email address.

The select box will return a value in the $_POST array which is either "" or one of the array keys ("" shouldn't happen but we'll handle it anyhow).

In the Onsubmit box we use a similar code to look up the $_POST value and set the corresponding email address.
<?php
// $subject = $_POST['subject']; // code for Joomla 1.0.x
$subject = JRequest::getVar('subject','', 'post', 'string', '' ); // code for Joomla 1.5.x
$email_array = array('city_admin' => 'jana.sousa', 'billing' => 'melinda.wall', 'building' => 'Building_Department', . . . , 'general' => 'rick.spalding');
if ( isset($subject) && in_array($subject, $email_array) {
  $email = $email_array[$subject]."@losbanos.org";
} else {
  $email = $email_array['general']."@losbanos.org";
}
// $_POST['email'] = $email; // code for Joomla 1.0.x
JRequest::setVar('email', $email, 'string', '' ); // code for Joomla 1.5.x
?>

Here we set $subject equal to the value returned by the form (there are slightly different syntaxes for Joomal 1.0 and 1.5); then look up $subject in an email address array. This has the same array keys as the earlier one but this time the values are the name part of the email address. If there is no matching value then we'll use the 'general' address. We add the domain name to the email address (just makes the array shorter) and put the email address into the 'email' placeholder we created with the hidden field.

You can now use 'email' (no quotes) in the Special Fields tab to direct your email, and use {email} if you need to in the email template.

At least this is how it should work - there is a little bug in some versions of ChronoForms which requires a valid email address in the General tab before you can save your form. If you hit this problem then you can put a dummy address in there like [email]user@example.com[/email] and overwrite it in the OnSubmit beore code with
<?php
$rows[0]->extraemail = $email;
?>

Bob

PS None of this code is tested and so it may need de-bugging.
hicksticks2001 21 Aug, 2008
Thanks Bob. What do I need to make the URL links in order to use the URL to pass the variables off the the scripts in the form? Once again, thank you very much for you time. Thank you!
GreyHead 21 Aug, 2008
Hi hickstick2001,

Just for me to be clear -- You want - for example - the Communuity Development 'Contact Us' menu item to link to this form with the CommDev subject preselected?

Make the CommDev menu link index.php?option=com_chronocontact&Itemid=7&subject=comm_dev

Then add to the code in the loop to set it selected:
<?php
$subject = JRequest::getVar('subject','', 'get', 'string', '' );
$dept_array = array('city_admin' => 'City Administration', 'billing' => 'Billing Department', 'building' => 'Building Department', . . ., 'general' => 'General');
foreach ( $dept_array as $key => $value ) {
  if ( $ key == $subject ) {
    $selected = "selected='selected'";
  } else {
    $selected = "";
  } 
  echo "<option value='$key' $selected >$value</option>";
}
?>


Bob
This topic is locked and no more replies can be posted.