user2483081
user2483081

Reputation: 61

Dynamic form passing data (coldfusion and query)

I have a simple message page where people can react on a specific message. The code:

    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
    $(function () {
    $('form').on('submit', function (e) {
      $.ajax({
        type: 'post',
        url: 'save_comment.cfm',
        data: $('form').serialize(),
        success: function () {
          alert('form was submitted');
        }
      });
      e.preventDefault();
    });
    });
    </script>

    <cfquery datasource="#ns#" name="getdata">
     select text, timeline_id
     from timeline
     order by t_datum desc
     </cfquery>

     <cfoutput query="getdata">
     <p>#text#</p>
     <cfform>
     <cfinput type="hidden" value="#timeline_id#" name="#timeline_id#">
     <cfinput type="text" name="comment"><input type="submit">
     </cfform>
     </cfoutput>

Now the problem is that the form is dynamic. So if I submit some comment the value of the hidden form field timeline_id is i.e. 3,4,5,7. Normaly I could send the value like this:

     <cfform action="save_comment.cfm?timeline_id=4>

but the form is submit with query, so without a page reload.

Any idea's on this?

Thanks!

Upvotes: 0

Views: 1148

Answers (2)

Scott Stroz
Scott Stroz

Reputation: 7519

Simply create a hidden form field with the name timeline_id and set it to the value you need.

Such as

<form ....>
    <input type="hidden" name="timeline_id" value="#timeline_id#" />
    More form stuff here.....
</form>

Notice that I removed the cfform and cfinput. You should not use them, they cause more issues than they are worth.

Upvotes: 1

Lance
Lance

Reputation: 3213

Name your form fields something unquie (see below) then you can loop through the field list and match the comments with the ids and it will be a bit easier to put things together.

  <cfoutput query="getdata">
     <p>#text#</p>
     <cfform>
     <cfinput type="hidden" value="#timeline_id#" name="tid_#timeline_id#">
     <cfinput type="text" name="tid_#timeline_id#_comment"><input type="submit">
     </cfform>
  </cfoutput>

Upvotes: 0

Related Questions