Trying To Echo Cookie inside of form code

Hi,
Can anybody point me in the right direction…?

I am setting up a Getresponse email opt-in form for my members to use.
I’m trying to use PHP to insert their webform ID’s dynamically.
The webform ID’s start off in the URL (mysite.com/?id=12345) and I can successfully cookie this and out put the value on a different part of the page…

However, the form doesn’t work because I cant get the cookie to echo.
(The form works fine with the webform ID as normal)

Maybe my syntax is wrong or maybe its a known thing that this isn’t possible within a form.
My PHP knowledge is pretty much non-existent!

<form action="https://app.getresponse.com/add_subscriber.html" accept-charset="utf-8" method="post">
Name: <input name="name" type="text">Email: <input name="email" type="text">
<input name="campaign_token" value="<?php echo $_COOKIE['id']; ?>" type="hidden">
<input value="Sign Up!" type="submit">
</form>

If the “id” is in the GET string of the URL then you need to use $_GET[‘id’] instead of $_COOKIE. Unless you stored the id in a cookie the id won’t exist there. It is probably throwing a warning that the key doesn’t exist either, but my guess is php errors aren’t set to show these warnings. Hope this helps.

Thanks Polk, really appreciate the quick reply!
You are a legend! That works now. :slight_smile:
The form now looks like this.

<form action="https://app.getresponse.com/add_subscriber.html" accept-charset="utf-8" method="post">
Name: <input name="name" type="text"/>Email: 
<input name="email" type="text"/>
<input name="campaign_token" value="<?php echo $_GET['id']; ?>" type="hidden"/>
<input value="Sign Up!" type="submit"/>
</form>

Glad I could help.

Generally putting an ID in the URL is not recommended without some validation, because people can hijack it and send data you don’t want. But since it is for a third party app, it should be fine.

Cool,
Yeah the members will save their webform ID in the protected membership area (on a different domain).
When they click save, it will spit out the Landing Page URL for them to use with their ID appended to it…
eg: mysite.com/?id=12345
Then the $_GET[‘id’] will take care of the rest…

I can’t think of any other way of acheiving this without investing in software/programmer. I wouldn’t even know what to google!
I’m all ears if you have any more advice :slight_smile:

Well it could be something as simple as

[php]

<?php $campaign_token = null; if (isset($_GET['id']) && (int) $_GET['id'] > 0) { $campaign_token = $_GET['id']; } [/php] [code] <?php if ($campaign_token): ?>

INSERT FORM HERE

<?php else: ?>

ERROR MESSAGE
Your ID is not valid blah blah.

<?php endif; ?>

[/code]

The above code won’t verify it is a valid ID but it will make sure the ID exists and that the ID is a number above 0

wow thanks!
I’ll give that a try after work.
The only possible issue is that the form ID’s use letters as well…

You can use the ternary operator to do a short if/else to set a default value

[php]$compaign_token = isset($_GET[‘id’]) ? $_GET[‘id’] : null;

// $compaign_token is either whatever $_GET[‘id’] contained, or null if $_GET[‘id’] was not set[/php]

If you want to validate that the token is valid you could create some function that validates it, and call that as well

[php]// Just an example…
function isValidCampaignToken($token)
{
return strlen($token) === 6;
}

$compaign_token = isset($_GET[‘id’]) && isValidCampaignToken($_GET[‘id’]) ? $_GET[‘id’] : null;[/php]

Thanks JimL really appreciate your input.
Sorry to be a pain but how would I incorporate the code into the previous code example here?

It replaces your first code block

[php]<?php

$compaign_token = isset($_GET[‘id’]) ? $_GET[‘id’] : null;

<?php if ($campaign_token): ?>

// INSERT FORM HERE

<?php else: ?>

// ERROR MESSAGE
// Your ID is not valid blah blah.

<?php endif; ?>[/php]

Great!
Thanks again, really appreciate everyones help here. I am a complete chump when it comes to code!

That won’t work. One is inline the other is straight php.

Make sure you close the php tag before you start the html.
[php]

<?php $compaign_token = isset($_GET['id']) ? $_GET['id'] : null; ?> <?php if ($campaign_token): ?>

// INSERT FORM HERE

<?php else: ?>

// ERROR MESSAGE
// Your ID is not valid blah blah.

<?php endif; ?>[/php]

Gotcha, thanks Polk!

This is odd…
My code has stopped working all of a sudden…
The only thing I have recently done is change my host. I’ve just migrated my sites to a new VPS server.
Everything works fine apart from this…

Is there something in the Apache/PHP build that may be stopping this code from being parsed?
Any ideas?

Sponsor our Newsletter | Privacy Policy | Terms of Service