2

I have a simple form like this one:

<form ENCTYPE="multipart/form-data" action="upload.php" method="POST">
<p>UPLOAD FILE: <input type="file" name="file1"> <br /></p>
<input type="submit" value="Upload">
<input type="hidden" name="email" id="email" value="">
</form>

I have to set the value of the hidden field to an email like "email@email.com". I can obtain this value from adding a querystring parameter by external iframe like:

<iframe name="email@email.com" src="index.html?email=email@email.com">

Ok. But how can I set value="" to value="email@email.com"? I know have to use javascript, but I dunno how to deal with the var.

Gareth
  • 133,157
  • 36
  • 148
  • 157
user840718
  • 1,563
  • 6
  • 29
  • 54
  • You could also fill it in on the server side if you want - you could get your index.html to use some server-side processing, take the email parameter and insert it into the field when you generate the form. But it's easy enough to do this with JavaScript too. – Rup Jul 09 '12 at 10:46
  • See [this question](http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript) to extract the query string values. Some of the solutions may be a bit over-the-top for what you need but they're robust too. – Rup Jul 09 '12 at 10:51

3 Answers3

3
document.getElementById('Id').value='new value';

answer to comment: try this:

<input type="" name="email" id="email">

<iframe id="frm" name="email@email.com" src="index.html?email=email@email.com">
</iframe>

<script>
document.getElementById('email').value = document.getElementById('frm').name;
</script>

then you can change adopt it. change type to hidden and etc.

loler
  • 2,594
  • 1
  • 20
  • 30
  • 1
    Ok it works but this value is dynamic. I have to extract it from the querystring parameter by external iframe. This is my problem :/ – user840718 Jul 09 '12 at 10:49
2

If your script is executed from the document inside the iframe, you can do this :

var getUrlParameter = function(name, defaultValue) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( document.location.href );
    if( results == null ) return defaultValue;
    else return decodeURIComponent(results[1]);
};

document.getElementById('email').value=getUrlParameter('email');
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

As you've labeled this question as jQuery You can use $('#Id').val('new value'); But don't forget to add jQuery Library.

Vins
  • 8,849
  • 4
  • 33
  • 53