Jump to content

Recommended Posts

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">

Link to comment
https://forums.phpfreaks.com/topic/218385-input-value-documentwrite/
Share on other sites

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. 

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.

@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!). 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.