ajax fill form, fill form dynamically with JQuery and Ajax

Well, this is what I did.

For each of the generated respond buttons, I set the onClick event to pass the fields I want to be pre-populated in my form to the JS function RespondMessage()

I also made a form at the bottom of the page that I used CSS to hide. This form contains as many hidden fields as I want pre-populated with data, and as many spans as I want to display information.

When I call the RespondMessage function, I manually set the hidden fields in the form with the parameters from which I called the function. After that I populate my span with the information I want to display (eg I set recipient name for this message).

Once everything is done, the form can be displayed. So after setting the fields in the form, I just pass the form from display:none to display:block and do a lot of css tricks to make them smooth and nice.

So now that you have the pre-filled form, you just need another function to make the Ajax call to avoid reloading the page after each response, and you'll call it with the onClick event of the form's submit button.

I hope this process helps others.

I'm not quite sure if this is the best way, I found it to be pretty dirty and exploitable, so you need more server side checks than usual, but it does the job.

I don't provide the code as it's mostly really specific to the application you're setting up.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324325881&siteId=291194637