Jump to content

“Pre-set” a form field (using $session?)


yellowled
 Share

Recommended Posts

In a project, I have a lot of links which all lead to the same form. Every link is asscociated with an object which has a unique id. I need to set the value of one field in said form depending on which link has been clicked so that the recipient of the emails sent by the form knows which object they inquiry is related to. Sometimes these objects may be on a single page, sometime multiple links on the same page, so the association needs to be with the link, not with the page.

I know how to do this using JS and cookies, but I'd rather not use that if possible. Is there a way to maybe use $session for this? If so, how do I set a session variable based on “if a link is clicked”? is that even possible?

Link to comment
Share on other sites

Couldn't you just add something like ?link=abc to the link url? (You still could use $session->redirect afterwards to hide it)

Like:

<a href="/a-page/?link=abc">

and on /a-page/:

$session->set('redirect', $input->get->link);
$session->redirect($page->url);
  • Like 1
Link to comment
Share on other sites

and on /a-page/:

$session->set('redirect', $input->get->link);
$session->redirect($page->url);

That does bascially work fine, but the redirect doesn't seem to be a good idea. Chrome gives me a warning that the page has a redirect (Duh!) and refuses to load it (after trying to load for quite some time). So it's a dead end with the redirect.

However, it does work well without the redirect. I'm kind of okay with the extra URL segment there, but of course, if anybody has another idea how to get rid of it …  :)

Link to comment
Share on other sites

Don't really understand the question (or the problem :D ). Why not add a parameter to each link and react on that on the form page?

<a href="form.php?formid=82828">

form page:

$formid = $input->get->formid;

// get the title of the page or whatever
$myValue = $pages->get($formid)->title;

form:

<input type="hidden" value="<?= $myvalue ?>" name="Referrer">

Or do I misunderstand the question?

  • Like 1
Link to comment
Share on other sites

Or do I misunderstand the question?

That part is already done and working like a charm. Nico's code was (I think) supposed to redirect from e.g. /contact/?id=1 to just /contact/ (while still submitting the 1 to the $session variable) to keep the URL nice and clean. But as I tested it, I noticed it throws an error message in the browser because of the redirect.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...