We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Using PHP in template to GET & Echo variable from URL

How Do I?

sanitizer's avatar
sanitizer
3 posts
7 years ago
sanitizer's avatar sanitizer

Hi there,

Relatively new to EE, whilst ive read about add-ons, I would preferably just hard-code PHP as im used to into my templates.

I am trying to use variables from a URL to then echo them directly onto a form (to avoid users having to repeatedly type info)

i.e. https://www.exampleform.com?applicantFirstName=Chris

to get & echo: <?php $applicantFirstName=$_GET(applicantFirstName)?>

Then in the form field, the flwg to show and display the variable as the value <INPUT id=”applicantFirstName” type=”text” name=”applicantFirstName” value=’<?php echo $applicantFirstName ?>’>

How can the above be used in EE, i am using version 2.4

Clarification much appreciated !!

       
Paul Bailey's avatar
Paul Bailey
63 posts
7 years ago
Paul Bailey's avatar Paul Bailey

You’re pretty close. To grab the parameter (you might want to check that it exists first, using array_key_exists):

<?php $applicantFirstName = $_GET[‘applicantFirstName’]; ?>

Then to use it to populate the form:

<input type=”text” id=”applicantFirstName” name=”applicantFirstName” value=”<?php echo $applicantFirstName ?>”>

(Better to use double quotes.)

You should be able to put the code pretty much anywhere in your template. If what you have isn’t working, make sure that you’ve enabled PHP in the template. Go to the template preferences and check that “Allow PHP?” is set to yes.

       
sanitizer's avatar
sanitizer
3 posts
7 years ago
sanitizer's avatar sanitizer

Hi Paul, thanks for the reply.

I have tried your code but still no luck, I also tried using array_key_exists, and it does not (confused).. so theres a problem when im trying to use _GET method? any clues? I also have PHP active and at the moment it is on INPUT setting, is that how it should be?

Many thanks

       
Paul Bailey's avatar
Paul Bailey
63 posts
7 years ago
Paul Bailey's avatar Paul Bailey

If you’re not seeing the parameter in the $_GET array, this isn’t a PHP problem.

First thing: check what’s coming through in the $_GET array. Put this line right at the top of the template, and it will dump the contents of the $_GET array, so you can see what’s in it:

<?php print_r($_GET); ?>

If you don’t see the parameter in there, something might be going wrong with the URL you’re passing in. Could you post the exact URL you’re using?

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.