I am trying to pass the name of a product to a contact form using the token %get[product]. It works fine of course until I have a special character like an apostrophe in the product name. The product name is G'zOne. I have tried a few things to get the name passed in the query string, but nothing inserts it into the form correctly.

If I just use $options['query'] = 'product='.$title;, the url reads 'electronic-products-contact-form?product=G'zOne Type-S Mobile Phone', and only the first G shows up in the field.

If I use $options['query'] = 'product='.urlencode($title); to create the url, then the url reads 'electronic-products-contact-form?product=G%26%23039%3BzOne+Type-S+Mobile+Phone' and G'zOne Type-S Mobile Phone shows up in the form field. The apostrophe doesn't get decoded.

Is there a way to make this work? Do I need to use something other than urlencode to make the url, or is there a way to make sure it get's propery decoded when it's fed to the form.

Thanks for your help. I did search for this before posting my issue.

Comments

quicksketch’s picture

Try using drupal_urlencode() instead, you might have better luck with apostrophes.

nrackleff’s picture

Thanks for the suggestion. I just tired that and it didn't fix the problem either.
I used $options['query'] = 'product='.drupal_urlencode($title);
The url now reads electronic-products-contact-form?product=G%2526%2523039%3BzOne%20Type-S%20Mobile%20Phone
The form field displays 'G%26%23039;zOne Type-S Mobile Phone'.

Here is the full code I am using to create the link to the form from my product node:

$conNode = node_load($node->field_prod_contacts[$i]['nid']);
$options['query'] = 'product='.drupal_urlencode($title);
print (l($conNode->title,'node/'.$conNode->nid, $options)."\n");

Any ideas?

quicksketch’s picture

Hmm, well now thing look like they're getting double-encoded. Maybe you could try using an array for the $options['query'] value, and just let Drupal do it's thing with the encoding.

<?php $options['query'] = array('product' => $title); ?>

nrackleff’s picture

I just changed it to:

$conNode = node_load($node->field_prod_contacts[$i]['nid']);
$options['query'] = array('product' => $title);
print (l($conNode->title,'node/'.$conNode->nid, $options)."\n");

The url is now: electronic-products-contact-form?product=G%2526%2523039%3BzOne%20Type-V%20Mobile%20Phone

The form field displays: G%26%23039;zOne Type-V Mobile Phone

This is weird. Am I the first person to have encountered this?

Thanks for all your help on this.

quicksketch’s picture

Could you provide some more information, on this? Seems like the code you have is fine, maybe $title is already encoded? Or possibly the code inserting it into a field is encoding it again. Posting the code that is responsible for $title and filling in the node field might shed some light on this, since I don't think there's anything wrong with the code you've posted so far.

nrackleff’s picture

Thanks again for your help. I'll try to give you as much info as possible.
- I have used the CCK to create a content type called 'product'.
- One of the CCK fields in 'product' is a node reference to a webform.
- I have created node-product.tpl.php to display my product rather than using the Drupal default node display.
- When I create a link from the product page to the webform, I am attempting to pass the $title field from the product node through the query string to the contact form.
- The contact form then uses the token %get[product] to take that element from the query string and fill in a text field.

Here is the code used to create the link to the form:

//check to see if we have any contact forms to link to.
if ($node->field_prod_contacts[0]['nid'] > 0)
{
	$start = "<div class=\"BoxPatternB Column2 FirstItem contact-info\">\n<div class=\"Inner\">";
	$end = "</div>\n</div>\n";
	print ($start);	
	for ($i = 0; $i < count($node->field_prod_contacts); $i++ ) 
	{
		$conNode = node_load($node->field_prod_contacts[$i]['nid']);
		$options['query'] = array('product' => $title);
		print (l($conNode->title,'node/'.$conNode->nid, $options)."<br />\n");
	}
	print ($end);
}

You may be on to something as far as $title already being encoded. The basic drupal page template that lists variables refers to $title as "the (sanitized) title of the node". Maybe I need to start with a more unsanitized version of the title. I'll try to figure out how to do that.

nrackleff’s picture

Success!
You were right. It was double encoded. $title was already encoded. I change it to $node->title and it works great now!

Thanks so much for helping me work through this. I have only been working with Drupal for a few months and I learn new things every day and I love Drupal. I am also very impressed with how helpful everyone is. I look forward to creating a module of my own and becoming a part of this helpful and talented group.

Nancy

quicksketch’s picture

Status: Active » Fixed

Oh fantastic. You're welcome and thanks for the followup.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.