Arnau
Arnau

Reputation: 9

How to post form, from a dynamic element

I have a dynamic table that fills with a href links that each one has data element with their own value. There is also a form (not generated dinamically) which has an input type hidden, which will be set by link data value.

I can post the form with the hidden input value with the correct data, and the action page is loading fine, but can't receive any post data. It seems that dinamically elements can post form but data is not being sent.

   jQuery(document).ready(function() {        
      jQuery(document).on("click", ".linksollicituddetall", function() {
        var intIdSollicitud = jQuery(this).data("idsol");

        if(intIdSollicitud.toString().length > 0) {
            jQuery("#idsol").val(intIdSollicitud);
            jQuery("#frmDetallSollicitud").submit();    
        }
       });
   });

 <body>

<form id="frmDetallSollicitud" method="POST" action="DetallSollicitud.asp">
    <input type="hidden" name="idsol" id="idsol" value="-1">
</form>

</body>

What I want to achieve is when clicking on the link, the input type hidden fills with the data of the link, and then submit the form. In the next page (action URL) receive the post data.

Upvotes: 0

Views: 76

Answers (1)

Arnau
Arnau

Reputation: 9

Well, the problem was, that the link was launching the href before sending the form, so the problem was not preventingDefault.

The correct code is:

jQuery(document).on("click", ".linksollicituddetall", function(e) {
        e.preventDefault();
        var intIdSollicitud = jQuery(this).data("idsol");

        if(intIdSollicitud.toString().length > 0) {
            jQuery("#idsol").val(intIdSollicitud);
            jQuery("#frmDetallSollicitud").submit();    
        }
    });

Upvotes: 1

Related Questions