coster128
coster128

Reputation: 322

How to postback to a different form using javascript

I have a form with a textbox in it, I want to submit the value of the textbox to another page after an ajax call is successfully made, how can I achieve this?

This is my ajax method:

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    url: "http://test.net/getData",
    data: "{some:data}",
    success: function (response) {
            document.forms.form1.txtData.value = "value1";
            document.forms.form1.submit();
    },
    error: function (response) {
            alert("error");
    }
});

and then I have my form in page Page1 as:

 <form id="form1" runat="server">
    <input type="hidden" id="txtData" value="" runat="server" />
 </form>

But this will send my data back to the original page (Page1) not to Page2 form.

Upvotes: 1

Views: 733

Answers (3)

Satinder singh
Satinder singh

Reputation: 10208

You can use query string, this will redirect to another page with your textvalue

success: function (response) {
          var value=response.d   
          //  document.forms.form1.txtData.value = "value1";
          //  document.forms.form1.submit();
            window.location = "http://YourPageName.aspx?id="+value;
    }

Code Behind: On page load you can retrieve the value by using Request.QueryString

 if (Request.QueryString["id"] != null)
   {
          string getTxtValue = Request.QueryString["id"].ToString();
  }

Upvotes: 2

gembird
gembird

Reputation: 14053

You can set the 'action' attribute of the form to action name you need and then submit the form, e.g. like this.

success: function (response) {
        var $form = $("#form1");
        $form.attr("action", "your-action-name");
        $form.submit();
},

About HTML action Attribute

Upvotes: 1

Alexey Nis
Alexey Nis

Reputation: 471

You can specify action url in action attribute of form.

Take look on documentation

<form id="form1" runat="server" action="Page2.aspx">
    <input type="hidden" id="txtData" value="" runat="server" />
</form>

Upvotes: 2

Related Questions