liamloveslearning Posted November 11, 2010 Share Posted November 11, 2010 Hi all, I have an input field which needs to take the value of the window location, Ive tried everything yet cant get it to work, anybody have any ideas? I've tried... <input type="hidden" name="page" id="page" value=""> <input type="hidden" name="page" id="page" value="document.write(window.location);"> <input type="hidden" name="page" id="page" value="window.location"> Quote Link to comment https://forums.phpfreaks.com/topic/218385-input-value-documentwrite/ Share on other sites More sharing options...
.josh Posted November 11, 2010 Share Posted November 11, 2010 Okay so your main problem is that your input field is html and you are trying to put javascript in it. Well you can't mix it like that. You have to use javascript after the fact to grab the element by its id and assign a value to it. However, you will then run into a problem of making sure the element is actually loaded into the DOM before javascript tries to assign a value to it. If you have an existing framework on your page like jquery, you can do something like this (jquery example): <input type="hidden" name="page" id="page" value=""> <script type='text/javascript'> $(document).ready(function() { $('#page').value = window.location; }); </script> If you do not have jquery or similar framework, you can go here to see what kind of coding you have to do to make sure the document is ready for all browsers (lots more code - which is why using jquery or similar is great). A third option that I don't really recommend but it works is to wrap the assignment in a function that checks if it exists. If it does not, keep calling the function until it does: <input type="hidden" name="page" id="page" value=''/> <script type='text/javascript'> function setValue() { if (!document.getElementById('page')) { var t=setTimeout('setValue()',100); return false; } else { document.getElementById('page').value = window.location; return true; } } setValue(); </script> This isn't really advisable though because if for whatever reason it never gets loaded, the function never stops getting called. Quote Link to comment https://forums.phpfreaks.com/topic/218385-input-value-documentwrite/#findComment-1133048 Share on other sites More sharing options...
Adam Posted November 11, 2010 Share Posted November 11, 2010 Why are you trying to use JavaScript for this? PHP would handle this far more efficiently, and would work for every user: <input type="hidden" name="page" id="page" value="<?php echo $_SERVER['PHP_SELF']; ?>"> FYI The reason why your code wouldn't work is because JS needs to be surrounded by <script> tags, and anything within an input's value will be treated as a string. Quote Link to comment https://forums.phpfreaks.com/topic/218385-input-value-documentwrite/#findComment-1133049 Share on other sites More sharing options...
.josh Posted November 11, 2010 Share Posted November 11, 2010 @MrAdam : Yes, using server-side scripting to input the variable would be the easiest thing. You could even get the URL easy enough from php when the user submits the form, without the hidden field at all. But since he's asking for apples, we can only assume he needs an apple, not an orange. He may not have access to the server-side scripting (and it may not even be php!). Quote Link to comment https://forums.phpfreaks.com/topic/218385-input-value-documentwrite/#findComment-1133057 Share on other sites More sharing options...
Adam Posted November 11, 2010 Share Posted November 11, 2010 True enough, but maybe liamloveslearning about oranges..? Quote Link to comment https://forums.phpfreaks.com/topic/218385-input-value-documentwrite/#findComment-1133064 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.