Link to home
Start Free TrialLog in
Avatar of pmac38CDS
pmac38CDS

asked on

Set value of HTML textbox before redirect

I have an HTML page with HTML input textboxes(for example one of them has the name attribute as email). The HTML page uses a form tag with the method set to post and action set to redirect to another url. I am passing values(name,address,email etc) to this HTML page as a query string. I have a javascript function which reads the values passed. I would like to set the value of the HTML textbox before I redirect the HTML page to the url.

Any ideas on how I can achieve this ?
Thanks,
Aditya
ASKER CERTIFIED SOLUTION
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pmac38CDS
pmac38CDS

ASKER

I am able to read the values from the query string fine. However I am unable to set the value of the HTML textbox. When I try to debug the said statement it appears that the html textbox is null.

Aditya
can you paste your html text box?check the id and name of the textbox
<input type="text" value="" name="email">
SOLUTION
Avatar of Pratima
Pratima
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Below is the javascript code I am using
<script language="javascript" type="text/javascript">
    window.setTimeout("pushSubmit()", 1);
    function pushSubmit() {
        var query = window.location.search.substring(1);
        var Whole_Name = getQueryVariable('Whole_Name');
        var cds_name = document.getElementById('cds_name');
        if (cds_name) {
            cds_name.value = Whole_Name;
        }
        document.getElementById('PrePopGateway').submit();
    }
    function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++)
            {
                 var pair = vars[i].split("=");
                    if (pair[0] == variable)
                    {
                        return pair[1];
                    }
            } 
  
    }
</script>

and this is the html textbox 
<input type="text" value="" id = "cds_email" name="cds_email">

When I add a breakpoint to the following line of code and step into it, it returns a null value
var cds_name = document.getElementById('cds_name');

Thanks,
Aditya

Open in new window

put  textbox before script is getting executed..I think you are putting textbox after executing the textbox
<input type="text" value="" id = "cds_email" name="cds_email">


<script language="javascript" type="text/javascript">
    window.setTimeout("pushSubmit()", 1);
    function pushSubmit() {
        var query = window.location.search.substring(1);
        var Whole_Name = getQueryVariable('Whole_Name');
        var cds_name = document.getElementById('cds_name');
        if (cds_name) {
            cds_name.value = Whole_Name;
        }
        document.getElementById('PrePopGateway').submit();
    }
    function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++)
            {
                 var pair = vars[i].split("=");
                    if (pair[0] == variable)
                    {
                        return pair[1];
                    }
            } 
  
    }
</script>

Open in new window

I think you are putting textbox after executing the whole script
 <input type="text" value="" id = "cds_email" name="cds_email">


   // window.setTimeout("pushSubmit()", 1);
    function pushSubmit() {
        var cds_name = document.getElementById('cds_email');
            alert(cds_name)
    window.setTimeout("pushSubmit()", 1);
    function pushSubmit() {
      //  var query = window.location.search.substring(1);
      //  var Whole_Name = getQueryVariable('Whole_Name');
        var cds_name = document.getElementById('cds_name');
		alert(cds_name)
      
       // document.getElementById('PrePopGateway').submit();
    }
    function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++)
            {
                 var pair = vars[i].split("=");
                    if (pair[0] == variable)
                    {
                        return pair[1];
                    }
            } 
  
    }
</script>

<input type="text" value="" id = "cds_name" name="cds_name">

Open in new window

I tried moving the textbox declaration before the javascript methods but it gave me an error.
    window.setTimeout("pushSubmit()", 1);
    function pushSubmit() {
        var query = window.location.search.substring(1);
        var Whole_Name = getQueryVariable('Whole_Name');
        var cds_email = document.getElementById('cds_email');
        if (cds_email) {
            cds_email.value = Whole_Name;
        }
        document.getElementById('PrePopGateway').submit();
    }
    function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++)
            {
                 var pair = vars[i].split("=");
                    if (pair[0] == variable)
                    {
                        return pair[1];
                    }
            } 
  
    }
</script>

<input type="text" value="" id = "cds_email" name="cds_email">

Open in new window

Can you please elaborate the difference between the two? Where do you want me to place the textbox ?
tell me first are your getting

 var cds_email = document.getElementById('cds_email');

are you able to putting the value for this textbox?
problem is you are retreving the wrong textbox id in the pushSubmit method.