• View our reviews on Hot Scripts
  • JS Tutorial

Recent Comments

Powered by Disqus

Back to articles

Make dynamic form using javascript

Simple function to make form with one hidden value and submit this form. It can be used for deleting elements, or changing status from one to another.

It takes 3 parameters val - value to be submitted (usually ID of record in database), name - key of the post variable (like $_POST['name']) and attach - id of any div to attach form, it doesn't really matter which div, because form is hidden.

function makeform(val, name, attach)
    var attachform = document.getElementById(attach);
    var form = document.createElement("form");
    form.setAttribute("method", 'post');
    form.setAttribute("action", "");
    var inp = document.createElement("input");
    inp.setAttribute("type", 'hidden');
    inp.setAttribute("value", val);
    inp.setAttribute("name", name);

You can use it like this:

<a href='javascript:void(0);' onclick='makeform(365, "del_item", "container")'>Delete thing</a>

Then capture post through PHP and delete record 

You may also be interested in:

Powered by

blog comments powered by Disqus