ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Passing form variable from one page to another in EE2 (method used to work in EE1)

January 28, 2011 3:07am

Subscribe [3]
  • #1 / Jan 28, 2011 3:07am

    Oxygen Smith

    120 posts

    Hey everyone,

    I’m attempting to set up a site that has a single-field “signup for our email newsletter” form in the header that asks for your email address with a submit button. I’m not using the built in Mailing List module, I’m using a third party mail service that can collect more information than just the email.

    So, when the visitor clicks “submit,” she gets taken to a expanded signup form with extra fields. The email field is already filled in to make this less of a pain for the visitor.

    In EE1 I do this by passing the email string that the user types in through the URL. The original form has the method “link” and the action parameter is set to the URL of the signup page — /signup/index.

    In EE1, clicking “submit” on the original form produces a URL like this and send the user to signup/index:

    <a href="http://mysite.ca/signup/index?email_address=oxygensmith%">http://mysite.ca/signup/index?email_address=oxygensmith%</a> 40gmail.com

    (Note, there’s no space between the % and 40 in the original code, I have to put a space here to make “percent 40” not turn in to an @ sign.)

    On signup/index, PHP (set to output) places the value from the URL into the email field (it cleans up the value for the at sign passed through the URL):

    <?php 
        $querystring = $_SERVER['QUERY_STRING']; 
        $querystring_trimmed = str_replace("email_address=","", $querystring); 
        $querystring_touchedup = str_replace("% 40","@", $querystring_trimmed); 
    ?>

    and the form is embedded via

    {embed="_forms/.mailinglistform" emailadd="<?php echo $querystring_touchedup; ?>"}

    and in the embedded form, the variable is then added as the value of the email field:

    <input type="text" class="title" name="MERGE0" id="MERGE0" size="25" value="{embed:emailadd}">


    However, when I try this on EE2, I don’t even get as far as loading the next page. When EE2 is sent to a URL like

    <a href="http://mysite.ca/signup/index?email_address=oxygensmith%">http://mysite.ca/signup/index?email_address=oxygensmith%</a> 40gmail.com

    ...it throws a 404, sending me to the 404 page.

    Does anyone have an alternate method of passing the variable, or a modification to this method, that will work?

    Thanks!

    Rob

  • #2 / Jan 28, 2011 3:54pm

    Barry Cogan

    291 posts

    HI Rob.

    I made a little example of this.

    Create an embed with the single input form for the email address. This can be attached to any page using {embed="embeds/signup"}. If your code is not going to change, a snippet would do just fine too.

    <form name="input" action="{path=signup/index}" method="post">
    <input type="text" name="email_address" />
    <input type="submit" value="Signup" />
    </form>

    Code: embeds group/signup template

    This code above links you to the signup/index template to get the rest of the user info (see below):

    <h2>Please Complete Form</h2>
    <p><form><br />
    First name: <input type="text" name="firstname" /><br ><br />
    Last name: <input type="text" name="lastname" /><br ><br />
    Email: <input type="text" name="email_address" value="<?php echo $this->EE->input->get_post('email_address'); ?>" ><br />
    </form>

    Code: signup group/index template

    This code shows the extended form with the email address the user typed pulled from the $_POST variables. It will show up so the user does not have to type the email again.

    Using _POST is better as you do not need to expose what you are sending via the URL.

    Will this do the trick for you?

    Best regards,
    Barry

  • #3 / Jan 31, 2011 6:34am

    Oxygen Smith

    120 posts

    Just tried it now. Works much better Barry. Thanks for providing a full code example!

  • #4 / Jan 31, 2011 11:31am

    Barry Cogan

    291 posts

    No Problem! I am delighted its working for you.

    Please feel free to post again with future issues 😊

    Sincerely,
    Barry

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases