Home › Forums › JavaScript › How to pass URL of current page to form field on linked to page?
- This topic is empty.
-
AuthorPosts
-
August 4, 2014 at 12:59 pm #177581
Matt
ParticipantI have site that has a call to action link at the bottom of every page and I want to trick which page clicks to this come from. I want to store the URL of the page which elicited the CTA click in a hidden form field.
This then gets submitted, along with the other form fields, to my CRM database.
So my thoughts are that I will pass the URL of current page to a hidden form field on form using Javascript.
My JS skills are not great. Can someone point me in the right direction?
August 4, 2014 at 1:06 pm #177586Alen
ParticipantDo you use Google Analytics?
There is this: https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide
So call to action is a link, and once clicked you pass the information needed to another page where form exists, or is the form part of call to action?
August 4, 2014 at 1:24 pm #177595Matt
ParticipantThanks for your response Alen. I do use GA and I agree this is what I would use if it was my site. But this is actually for a client and they are requesting it this way.
The call to action is a link to a contact form page. The client wants a variable containing either the URL or some other unique page identifier passed into a hidden form field on the page.
When this get submitted, it goes into their CRM. They want to be able to see all the data in one place which I guess is why they don’t want to use GA.
I had this working using Gravity Forms and I was just inserting the page url as a parameter into the link via PHP. Gravity Forms has a built-in feature that allows populating a form field’s value with a parameter sent in the URL.
But the client didn’t want to use Gravity Forms. So now I need to figure out how to do it without it.
August 4, 2014 at 1:30 pm #177599Matt
ParticipantSoronbe, thanks for your reply to my question. Would you mind pointing me towards some more context in which to use the
window.location.href
function?Please excuse my questions, my JS chops are not very strong.
I would imagine that I store
window.location.href
in a variable, but then how do I pass this variable to the next page? Do I store it in the URL? From there, how do I insert it into the form field?August 4, 2014 at 3:07 pm #177632Matt
ParticipantThank you Soronbe, that is very helpful.
It appears that code passes in the URL of the current page the form is on. I want to pass in the URL of the page that links to the page which the form is on.
I have a link to the form page on multiple pages of the site. When a user clicks on one the links, I want to pass the URL of the page the user is on when they click the link to the page with form.
Edit 1: I already have the parameter being passed through which PHP. How can I read the value of the
last-page
parameter from the JS code?Here’s an example URL: http://localhost/cc/company/request-demo/?last-page=http://localhost/cc/
Edit 2: I think I may have found it:
var last_page = getUrlVars()["last-page"];
Edit 3: I think I’m closer! I am using the following code but it’s not quite working. It’s giving me a Syntax Error: Unexpected token ILLEGAL
<script type="text/javascript"> document.form[0].00N9000000ApJSm.value = "<?php echo $last-page; ?>"; </script>
August 4, 2014 at 3:58 pm #177638Alen
ParticipantWe can’t access local files on your computer (link you posted).
Also, if you are using PHP why are you relaying on JavaScript. If I’m understanding you correctly, I would approach it with something like:
Page with Call to Action (let’s call it index.php)
<?php echo '<a href="form.php?last-page='.$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"].'">Call to Action</a>'; ?>
Page with the form (let’s call it form.php)
<input type="hidden" value="<?php echo $_SERVER["QUERY_STRING"]; ?>">
You’ll get this result
<input type="hidden" value="last-page=example.com/index.php">
August 4, 2014 at 4:19 pm #177641Matt
ParticipantThanks Alen, I wasn’t sharing that link for you to visit, but as an example of how I was passing the parameter.
I have been working on it in PHP, but the problem is that WordPress doesn’t let you execute PHP within a page. I tried a couple of the plugins that let you execute PHP in a page but they are all throwing errors just installing them.
I guess I need to make a new page template for this?
<?php echo '<input id=\"00N9000000ApJSm\" maxlength=\"255\" name=\"00N9000000ApJSm\" size=\"20\" type=\"text\" value=\"'.$last-page.'\"/>'; ?>
BTW, why doesn’t the Block Code function work very well?
August 4, 2014 at 5:17 pm #177648__
ParticipantIt’s giving me a Syntax Error: Unexpected token ILLEGAL
document.form[0].00N9000000ApJSm.value
Property names can’t start with numbers.
You could use the index accessor syntax, like so:document.form[0]['00N9000000ApJSm'].value
…but it would make more sense to choose a valid name, or add a prefix (e.g.,
_00N900
…).I have been working on it in PHP, but the problem is that WordPress doesn’t let you execute PHP within a page. I tried a couple of the plugins that let you execute PHP in a page but they are all throwing errors just installing them.
This is a limitation of WP that you need to come to terms with, either by abandoning such attempts, learning how to write WP plugins, or abandoning WP itself.
Do not use WP plugins that allow you to enter/run arbitrary PHP code. The whole idea is a basic security flaw. Uninstall them and make sure that the plugin scripts are actually removed from your server.
BTW, why doesn’t the Block Code function work very well?
Are you talking about the button here on the forum? It is just a shortcut for markdown. For block code, your text needs to look like this (pretend the apostrophes are backtics):
'codecodecode codecodecode codecodecode '
That’s backtic + all your code + new line + backtic + new line
August 5, 2014 at 9:46 am #177721Matt
ParticipantThank you, all of your answers really helped me. I got it completed by making a page template that contained the form.
I am using
$_SERVER["QUERY_STRING"];
to pass the parameter into the hidden field. But it’s passing both the variable name and the value into the field.Is there a way just to get the value?
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.