PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Hidden field use tip and a question

  1. I thought others might find this tip useful too. I just started using gravity forms to collect customer support requests and I often need to know the user's IP address and browser user agent string to help in troubleshooting. Those are easy enough to insert into a hidden field but I wanted to take it further to streamline some of the steps I usually take even more.

    So instead of just inserting {ip} in a hidden field, I put this:
    http://www.maxmind.com/app/locate_ip?ips={ip}

    That creates a URL I can just click to use MaxMind's geo IP lookup. So besides just the IP I can get an approximate geographical location and usually some info about their ISP.

    Here's the question part. I'd like to to the same thing with the user agent string and send it over to http://useragentstring.com, which does a nice breakdown of browser features. The problem is, if I do something like this:
    http://useragentstring.com/index.php?Analyze={user_agent}

    it doesn't work because there are spaces in the string. I'm assuming this could be done with a hook but I could use a pointer in the right direction. Thanks.

    Posted 14 years ago on Saturday February 27, 2010 | Permalink
  2. Just noticed that I left out that doing a PHP urlencode() on the user_agent field should do the trick. I'm just not quite sure how to put the pieces together.

    Posted 14 years ago on Sunday February 28, 2010 | Permalink
  3. This could be done with something like the pre-populate hook and manually passing the user agent information using PHP and then receiving data back from useragentstrong.com and then populating the hidden field with it. It would require some knowledge of PHP to pull off. How familiar are you with PHP?

    Posted 14 years ago on Monday March 1, 2010 | Permalink
  4. I don't even need to retrieve data from the site. I just want to construct a URL I can click later when needed.

    If you can show me a sample of a hook that populates a hidden field I can probably put the rest together.

    Posted 14 years ago on Monday March 1, 2010 | Permalink
  5. Okay, I found a way to make it work. I created my hidden field and set Gravity Forms to insert {user_agent} into that field. Then I added some code to my functions.php to rewrite that field:

    add_action("gform_pre_submission", "pre_submission_handler");
    function pre_submission_handler($form_meta){
    	global $_POST;
    	// form id 1 is our main contact form
    	if($form_meta["id"] != '1'){
    		return;
    	}
    	//http://useragentstring.com/index.php?uas={user_agent}
    	$_POST["input_7"] = 'http://useragentstring.com/index.php?uas=' . urlencode($_POST["input_7"]);
    }

    I wasn't quite sure how to create a field on the fly or I may have just grabbed the user agent from PHP and built the field in the script instead.

    Posted 14 years ago on Wednesday March 3, 2010 | Permalink