Iurie
Iurie

Reputation: 2248

How to change the value of hidden input field before submitting

I have a Feedburner subscription form with two buttons, one for daily news and one for weekly news. The question is how to change the value of hidden input field with name 'uri' before submitting? My solution doesn't work.

This is what I try to use:

<form id="feedburner" action="https://feedburner.google.com/fb/a/mailverify"
    method="post" target="popupwindow">
    <p>
        <input autocomplete="off" value="Enter your email…"
            onblur="if (this.value == '') {this.value = 'Enter your email…';}"
            onfocus="if (this.value == 'Enter your email…') {this.value = '';}"
            type="text" name="email"/>
        <input type="hidden" name="uri"/>
        <input type="hidden" name="loc" value="en_US"/>
    </p>

    <input type="submit" value="Daily" onsubmit="document.getElementsByName('uri').value = 'androidinfodaily'; window.open('https://feedburner.google.com/fb/a/mailverify?uri=androidinfodaily', 'popupwindow'); return true" checked> 

    <input type="submit" value="Weekly" onsubmit="document.getElementsByName('uri').value = 'androidinfoweekly'; window.open('https://feedburner.google.com/fb/a/mailverify?uri=androidinfoweekly', 'popupwindow'); return true">

</form>

Solved

I have fixed my code and now it works. This is the final variant:

<form id="feedburner" action="https://feedburner.google.com/fb/a/mailverify"
    method="post" target="popupwindow">
    <p>
        <input autocomplete="off" value="Enter your email…"
            onblur="if (this.value == '') {this.value = 'Enter your email…';}"
            onfocus="if (this.value == 'Enter your email…') {this.value = '';}"
            type="text" name="email"/>
        <input type="hidden" name="uri" />
        <input type="hidden" name="loc" value="en_US"/>
    </p>

    <input type="submit" value="Daily" onclick="document.getElementsByName('uri')[0].value = 'androidinfodaily'; window.open('https://feedburner.google.com/fb/a/mailverify?uri=androidinfodaily', 'popupwindow'); return true"> 

    <input type="submit" value="Weekly" onclick="document.getElementsByName('uri')[0].value = 'androidinfoweekly'; window.open('https://feedburner.google.com/fb/a/mailverify?uri=androidinfoweekly', 'popupwindow'); return true">

</form>

Upvotes: 6

Views: 6663

Answers (3)

Armin Bleiholder
Armin Bleiholder

Reputation: 72

do not forget [0] for the first element:

document.getElementsByName('uri')[0].value = 'androidinfodaily';

Upvotes: 2

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

You can have click event handler for button and change type of button to type="button" so that it will not submit the form directly. Inside click handler assign balue to uri input and then submit the form.

HTML:

<form id="feedburner" action="https://feedburner.google.com/fb/a/mailverify"
    method="post" target="popupwindow">
    <p>
        <input autocomplete="off" value="Enter your email…"
            onblur="if (this.value == '') {this.value = 'Enter your email…';}"
            onfocus="if (this.value == 'Enter your email…') {this.value = '';}"
            type="text" name="email"/>
        <input type="hidden" name="uri"/>
        <input type="hidden" name="loc" value="en_US"/>
    </p>

    <input type="button" value="Daily" id="daily"> 

    <input type="button" value="Weekly" id="weekly">

</form>

jQuery:

$(function(){
   $('#daily').click(function(){
      $('input[name="uri"]').val('androidinfodaily');
      window.open('https://feedburner.google.com/fb/a/mailverify?uri=androidinfodaily', 'popupwindow');
      //submit form
      $('#feedburner').submit();
   });

   $('#weekly').click(function(){
    $('input[name="uri"]').val('androidinfodaily');
      window.open('https://feedburner.google.com/fb/a/mailverify?uri=androidinfoweekly', 'popupwindow');
      //submit form
      $('#feedburner').submit();
  });
});

Upvotes: 2

Arun P Johny
Arun P Johny

Reputation: 388436

The submit event is fired on the form element, not on submit button, so use click handlers

Note that submit is fired only on the form element, not the button or submit input. (Forms are submitted, not buttons.)

<form id="feedburner" action="https://feedburner.google.com/fb/a/mailverify"
    method="post" target="popupwindow">
    <p>
        <input autocomplete="off" value="Enter your email…"
            onblur="if (this.value == '') {this.value = 'Enter your email…';}"
            onfocus="if (this.value == 'Enter your email…') {this.value = '';}"
            type="text" name="email"/>
        <input type="hidden" name="uri"/>
        <input type="hidden" name="loc" value="en_US"/>
    </p>

    <input type="submit" value="Daily" onclick="document.getElementsByName('uri')[0].value = 'androidinfodaily'; window.open('https://feedburner.google.com/fb/a/mailverify?uri=androidinfodaily', 'popupwindow'); return true" checked> 

    <input type="submit" value="Weekly" onclick="document.getElementsByName('uri')[0].value = 'androidinfoweekly'; window.open('https://feedburner.google.com/fb/a/mailverify?uri=androidinfoweekly', 'popupwindow'); return true">

</form>

Also it will be better to speprate the script to a function like

<form id="feedburner" action="https://feedburner.google.com/fb/a/mailverify" method="post" target="popupwindow">
    <p>
        <input autocomplete="off" value="Enter your email…" onblur="if (this.value == '') {this.value = 'Enter your email…';}" onfocus="if (this.value == 'Enter your email…') {this.value = '';}" type="text" name="email" />
        <input type="hidden" name="uri" />
        <input type="hidden" name="loc" value="en_US" />
    </p>
    <input type="submit" value="Daily" onclick="return beforeSubmit('androidinfodaily')" checked />
    <input type="submit" value="Weekly" onclick="return beforeSubmit('androidinfoweekly')" />
</form>

then

function beforeSubmit(type) {
    document.getElementsByName('uri')[0].value = type;
    window.open('https://feedburner.google.com/fb/a/mailverify?uri=' + type, 'popupwindow');
    return true
}

Upvotes: 5

Related Questions